GD:使 PNG 的白色背景透明
我有一个带有透明背景的PNG水印图像。但它会随机生成白色背景而不是保持透明。
// Watermark
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png');
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png');
// Combinde watermark image with image already generated in $dst
imagecopy($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, $mheight);
解决方案是添加:
imagealphablending($dst, true);
imagesavealpha($dst, true);
完整代码:
// Watermark
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png');
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png');
imagealphablending($dst, true);
imagesavealpha($dst, true);
// Combinde watermark image with image already generated in $dst
imagecopy($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth,
I have a PNG watermark image with transparent background. But randomly it generates a white background instead of staying transparent.
// Watermark
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png');
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png');
// Combinde watermark image with image already generated in $dst
imagecopy($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, $mheight);
Solution is to add:
imagealphablending($dst, true);
imagesavealpha($dst, true);
Complete code:
// Watermark
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png');
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png');
imagealphablending($dst, true);
imagesavealpha($dst, true);
// Combinde watermark image with image already generated in $dst
imagecopy($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Alpha 通道保存
$dst
,而不是$watermark
。Save
$dst
with an alpha channel, not$watermark
.我有同样的问题,但为了让它工作,我从代码中注释掉了这两行:
所以我的代码看起来像这样:
现在两个图像合并并保留了透明度,用这两行它生成一个随机的白色背景,希望这可以帮助其他遇到同样问题的人
I had the same issue, but for me to get it to work I commented out these two lines from my code:
so my code looked like this:
now the two images merged and preserved the transparency, with those two lines it was generating a random white background, hope this helps others with the same issue
尝试 imagecopymerge 而不是 imagecopy
编辑:尝试以下代码:
Try imagecopymerge instead of imagecopy
EDIT: try this code: