2013-08-07 53 views
0

我想在將圖片上傳到我的網站後立即爲圖片添加水印,但似乎水印一直以黑色物體形式出現,沒有任何細節。我相信該腳本有點工作,因爲如果不是,我可能不會看到任何水印或腳本會失敗。PHP GD水印腳本會產生空白水印

這是我的腳本至今:

$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像素:

enter image description here

我曾嘗試與水印圖像本身播放。我第一次嘗試使用'save for web'並選擇'PNG-24'來保存photoshop水印(使用透明bg)。這不起作用,所以我把它保存爲一個正常的PNG(沒有'保存爲網絡'),它仍然失敗。

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

+0

我記得閱讀[somewhere](http://stackoverflow.com/questions/11680734/php-watermark-png-transparency-alpha)有一些關於PNG-24的GD問題和PNG-8會更好的問候以wattermarks。 –

回答

1
$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/en/function.imagesavealpha.php

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

你還輸出一個JPEG,爲什麼?留在PNG和你的形象將支持透明水印,現在它不!

+0

隨着實際嘗試,我知道你是對的。發佈問題後,彈出一篇相關文章,並有人說同樣的事情。我只是測試了一個PNG-8水印並且它工作正常,雖然在邊緣看起來很粗糙。我會去改變'imagecopymerge',除了PNG-24之外。謝謝。 – TheCarver

+0

很好聽,我在答案中增加了一些內容,爲什麼用imagejpeg代替imagepng? –

+0

我的用戶上傳了一個JPEG文件,上傳腳本將它保存爲JPEG文件,所以我想我只是試圖保持它一樣。如果我要使用PNG,我必須在應用程序的其餘部分中更改一小段代碼。 – TheCarver

0

SO和php.net上也有很多解決方案。這是one(不創建新的圖像本身)

// 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); 

編輯

水印應保存阿爾法channek(透明度)。自CS2以來的Photoshop(保存爲網頁)應該做到這一點,GIMP的作品也很棒。

+0

感謝您的時間,但我並不是真的在尋找新的腳本,我只是想知道爲什麼會發生這種情況並修復了我的腳本。 – TheCarver

+0

你寫了'我不確定它是腳本還是圖片',所以我推測你的腳本(沒有進一步分析)它的工作不正常。我們無法測試圖像是否正常,因爲您沒有附加它。 – kwarunek