0

I'm trying to add a watermark to images right after uploading them to my website, but it seems the watermark keeps coming out as a black object with no details. I believe the script is working a little bit because if it wasn't, I probably wouldn't see any kind of watermark or the script would fail.

This is my script so far:

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

This is what it's producing. The shape of the watermark is correct, as the watermark is 325x37 pixels:

enter image description here

I have tried playing with the watermark image itself. My first attempt was to save the photoshop watermark (with transparent bg) using 'save for web' and selected 'PNG-24'. This didn't work, so I then saved it as a normal PNG (without 'save for web') and it still fails.

I'm not sure whether it's the script or the image! Can somebody please share some knowledge with me and help fix this issue?

4

2 回答 2

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,您的图像将支持透明水印,但现在不支持!

于 2013-08-07T17:10:10.903 回答
0

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 通道(透明度)保存。Photoshop 因为 CS2(保存为网络)应该可以做这件事,GIMP 也很好用。

于 2013-08-07T17:03:13.657 回答