GD:使 PNG 的白色背景透明

发布于 2024-10-01 18:41:02 字数 924 浏览 2 评论 0原文

我有一个带有透明背景的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔岸观火 2024-10-08 18:41:02

使用 Alpha 通道保存 $dst,而不是 $watermark

// Watermark
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png');
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png');

imagealphablending($dst, false);
imagesavealpha($dst, true);

// Combinde watermark image with image already generated in $dst
imagecopy($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, $mheight);

Save $dst with an alpha channel, not $watermark.

// Watermark
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png');
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png');

imagealphablending($dst, false);
imagesavealpha($dst, true);

// Combinde watermark image with image already generated in $dst
imagecopy($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, $mheight);
痴意少年 2024-10-08 18:41:02

我有同样的问题,但为了让它工作,我从代码中注释掉了这两行:

imagesavealpha($image_1, true);
imagesavealpha($image_2, true);

所以我的代码看起来像这样:

$image_1 = imagecreatefrompng("example26_".$acct.".png");
$image_2 = imagecreatefrompng('example27.png');
imagealphablending($image_1, true);
imagealphablending($image_2, true);
//imagesavealpha($image_1, true);
//imagesavealpha($image_2, true);
imagecopy($image_1, $image_2, 0, 0, 0, 0, 1350, 250);
header("Content-Type: image/png");
imagepng($image_1);

现在两个图像合并并保留了透明度,用这两行它生成一个随机的白色背景,希望这可以帮助其他遇到同样问题的人

I had the same issue, but for me to get it to work I commented out these two lines from my code:

imagesavealpha($image_1, true);
imagesavealpha($image_2, true);

so my code looked like this:

$image_1 = imagecreatefrompng("example26_".$acct.".png");
$image_2 = imagecreatefrompng('example27.png');
imagealphablending($image_1, true);
imagealphablending($image_2, true);
//imagesavealpha($image_1, true);
//imagesavealpha($image_2, true);
imagecopy($image_1, $image_2, 0, 0, 0, 0, 1350, 250);
header("Content-Type: image/png");
imagepng($image_1);

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

鸢与 2024-10-08 18:41:02

尝试 imagecopymerge 而不是 imagecopy

编辑:尝试以下代码:

header('Content-type: image/jpeg');
$dst = imagecreatefromjpeg($image_path);
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png');
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png');
imagecopymerge($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, $mheight, 100);
imagejpeg($dst,'',90);
imagedestroy($dst);

Try imagecopymerge instead of imagecopy

EDIT: try this code:

header('Content-type: image/jpeg');
$dst = imagecreatefromjpeg($image_path);
$watermark = imagecreatefrompng($docRoot . '/images/misc/watermark.png');
list($mwidth, $mheight) = getimagesize($docRoot . '/images/misc/watermark.png');
imagecopymerge($dst, $watermark, $tnWidth-$mwidth-5, $tnHeight-$mheight-5, 0, 0, $mwidth, $mheight, 100);
imagejpeg($dst,'',90);
imagedestroy($dst);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文