简体   繁体   English

PHP GD水印脚本产生空白水印

[英]PHP GD watermark script produces blank watermark

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: 水印的形状正确,因为水印为325x37像素:

在此处输入图片说明

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'. 我的第一个尝试是使用“保存为网络”并选择“ PNG-24”来保存photoshop水印(带有透明bg)。 This didn't work, so I then saved it as a normal PNG (without 'save for web') and it still fails. 这没有用,所以我然后将其另存为普通的PNG(没有“为网络保存”),但仍然失败。

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? 有人可以与我分享一些知识并帮助解决此问题吗?

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

There are things: 有一些东西:

  1. imagecopymerge does not allow transparency for PNG-24 imagecopymerge不允许PNG-24透明

(Source: https://drupal.org/node/80369 ) (来源: https : //drupal.org/node/80369

  1. imagesavealpha + imagealphablending allow to save transparency imagesavealpha + imagealphablending可以保存透明度

(Source: http://php.net/manual/en/function.imagesavealpha.php ) (来源: http : //php.net/manual/zh/function.imagesavealpha.php

I hope it fixes your problem. 我希望它能解决您的问题。

Also you output a JPEG, why? 您还输出JPEG,为什么? Stay with PNG and your image will support transparent watermarks, right now it does not! 坚持使用PNG,您的图片将支持透明水印,但现在不支持!

There are many solution on SO and php.net as well. 在SO和php.net上也有许多解决方案。 This is one (without creating new image itself) 这是一个 (本身不创建新图像)

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

edit 编辑

Watermark should be saved with alpha channek (transparency). 水印应使用alpha channek(透明胶片)保存。 Photoshop since CS2 (save for web) should do the thing, also GIMP works great. 由于CS2(适用于网络保存)应该可以做Photoshop,所以GIMP效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM