簡體   English   中英

PHP GD水印腳本產生空白水印

[英]PHP GD watermark script produces blank watermark

在將圖像上傳到我的網站后,我正嘗試在圖像上添加水印,但是似乎水印一直作為黑色物體出現,沒有任何細節。 我相信該腳本可以正常工作,因為如果沒有,我可能不會看到任何水印,否則腳本將失敗。

到目前為止,這是我的腳本:

$watermark = imagecreatefrompng('preview-watermark.png');  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);        
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg($portfolio_preview_dir.'/'.$file);  
$size = getimagesize($portfolio_preview_dir.'/'.$file);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
imagejpeg($image, $portfolio_preview_dir.'/'.$file);  
imagedestroy($image); 
imagedestroy($watermark);

這就是它所產生的。 水印的形狀正確,因為水印為325x37像素:

在此處輸入圖片說明

我嘗試過玩水印圖像本身。 我的第一個嘗試是使用“保存為網絡”並選擇“ PNG-24”來保存photoshop水印(帶有透明bg)。 這沒有用,所以我然后將其另存為普通的PNG(沒有“為網絡保存”),但仍然失敗。

我不確定是腳本還是圖片! 有人可以與我分享一些知識並幫助解決此問題嗎?

$watermark = imagecreatefrompng('preview-watermark.png');
imagealphablending($watermark , false);
imagesavealpha($watermark , true);
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);        
$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg($portfolio_preview_dir.'/'.$file);  
$size = getimagesize($portfolio_preview_dir.'/'.$file);  
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;  
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);  
imagejpeg($image, $portfolio_preview_dir.'/'.$file);  
imagedestroy($image); 
imagedestroy($watermark);

有一些東西:

  1. imagecopymerge不允許PNG-24透明

(來源: https : //drupal.org/node/80369

  1. imagesavealpha + imagealphablending可以保存透明度

(來源: http : //php.net/manual/zh/function.imagesavealpha.php

我希望它能解決您的問題。

您還輸出JPEG,為什么? 堅持使用PNG,您的圖片將支持透明水印,但現在不支持!

在SO和php.net上也有許多解決方案。 這是一個 (本身不創建新圖像)

// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);

// Copy the stamp image onto our photo using the margin offsets and the photo 
// width to calculate positioning of the stamp. 
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));

// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

編輯

水印應使用alpha channek(透明膠片)保存。 由於CS2(適用於網絡保存)應該可以做Photoshop,所以GIMP效果很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM