繁体   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