PHP+GD:imagecopymerge 不保留 PNG 透明度

发布于 2024-09-12 12:47:35 字数 1005 浏览 4 评论 0原文

我有两个 PNG 文件,“red.png”和“blue.png”;它们大部分都是透明的,但在不同的地方有一些红色或蓝色斑点的像素。

我想制作一个将两者合并的 PHP 脚本;它应该像这样简单:

$original = getPNG('red.png');
$overlay = getPNG('blue.png');

imagecopymerge($original, $overlay, 0,0, 0,0, imagesx($original), imagesy($original),   100);
header('Content-Type: image/png');
imagepng($original);

当我运行这个脚本时,我得到的只是蓝点——透明度丢失了。我发现我应该添加这些:(

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

在原始和覆盖层上?)这似乎没有任何帮助。

我在 PHP.net 上看到了一些解决方法,类似于:

$throwAway = imagecreatefrompng($filename);
imagealphablending($throwAway, false);
imagesavealpha($throwAway, true);
$dstImage = imagecreatetruecolor(imagesx($throwAway), imagesy($throwAway));
imagecopyresampled($dstImage, $throwAway,0,0,0,0, imagesx($throwAway),     imagesy($throwAway),          imagesx($throwAway), imagesy($throwAway));

,它应该将 PNG 转换为“真彩色”图像并保留透明度。看起来确实是这样,但现在我看到的只是黑色背景上的蓝色。

我该怎么办?!

I have two PNG files, "red.png" and "blue.png"; they are both mostly transparent, but there is a few pixels of red or blue splotches in various places.

I want to make a PHP script that merges the two; it should be as simple as something like:

$original = getPNG('red.png');
$overlay = getPNG('blue.png');

imagecopymerge($original, $overlay, 0,0, 0,0, imagesx($original), imagesy($original),   100);
header('Content-Type: image/png');
imagepng($original);

When I run this script, all I get is the blue dots -- with the transparency lost. I saw that I should add these:

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

(on both the original and the overlay?) And that doesn't seem to help any.

I saw a few workarounds on PHP.net, something to the tune of:

$throwAway = imagecreatefrompng($filename);
imagealphablending($throwAway, false);
imagesavealpha($throwAway, true);
$dstImage = imagecreatetruecolor(imagesx($throwAway), imagesy($throwAway));
imagecopyresampled($dstImage, $throwAway,0,0,0,0, imagesx($throwAway),     imagesy($throwAway),          imagesx($throwAway), imagesy($throwAway));

, which should convert the PNG to a "truecolor" image and retain transparency. It does seem to do so, but now all I see is blue on a black background.

What do I do?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

佞臣 2024-09-19 12:47:35

这对我来说非常适合:

$img1 = imagecreatefrompng('red.png');
$img2 = imagecreatefrompng('blue.png');

$x1 = imagesx($img1);
$y1 = imagesy($img1);
$x2 = imagesx($img2);
$y2 = imagesy($img2);

imagecopyresampled(
    $img1, $img2,
    0, 0, 0, 0,
    $x1, $y1,
    $x2, $y2);

imagepng($img1, 'merged.png', 0);

PHP 版本 5.3.2
GD版本2.0
libPNG 版本 1.2.42

您是否尝试将图像保存到文件并检查?

This works perfectly for me:

$img1 = imagecreatefrompng('red.png');
$img2 = imagecreatefrompng('blue.png');

$x1 = imagesx($img1);
$y1 = imagesy($img1);
$x2 = imagesx($img2);
$y2 = imagesy($img2);

imagecopyresampled(
    $img1, $img2,
    0, 0, 0, 0,
    $x1, $y1,
    $x2, $y2);

imagepng($img1, 'merged.png', 0);

PHP Version 5.3.2
GD Version 2.0
libPNG Version 1.2.42

Have you tried saving the image to a file and checking that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文