使用 GD 与 PHP 合并图像

发布于 2024-11-18 04:58:20 字数 934 浏览 5 评论 0原文

我正在努力从另外两张图片中创建一张 PNG 图像。

图像 A 和 B 具有相同的尺寸,均为 200x400 像素。最终图像相同。

我正在使用 PHP 的 GD 库。

所以我的想法是从原始 PNG-8 创建一个 PNG-24,然后使用颜色透明度,最后将第二个图像复制到 这个新的 PNG-24。无论如何,当从 PNG-24 到具有颜色透明度的 PNG-8 时,问题出现在第一步中:

这是为了获取原始 PNG-8 及其尺寸:

$png8 = imagecreatefrompng($imageUrl);
$size = getimagesize($imageUrl);

现在我创建一个新的 PNG 并用绿色填充它的背景(图像中不存在):

$png24 = imagecreatetruecolor($size[0], $size[1]);
$transparentIndex = imagecolorallocate($png24, 0x66, 0xff, 0x66);
imagefill($png24, 0, 0, $transparentIndex);

这是为了使绿色透明:

imagecolortransparent($png24, $transparentIndex);     

然后我将 png8 复制到 PNG-24 中:

imagecopy($png24, $png8, 0, 0, 0, 0, $size[0], $size[1]);

所以这是问题:原始的 PNG-8 看起来不错,但它的形状周围有一个绿色边框在原始图像内。确实很难解释。看起来绿色背景的某些部分留在了剩余的 PNG 中。

我能做些什么?

预先感谢

最好的问候,

费尔南多

i'm working on creating one PNG image from two others.

Image A and B have the same dimensions, they are both 200x400px. The final image the same.

I'm using the GD library with PHP.

So my idea was to create a PNG-24 from my original PNG-8, then use color transparency and finally copy the second image into
this new PNG-24. The problem appears in the first step anyway, when going from PNG-24 to PNG-8 with color transparency:

This is to get the original PNG-8 and it's dimensions:

$png8 = imagecreatefrompng($imageUrl);
$size = getimagesize($imageUrl);

Now i create a new PNG and fill it's background with a green color (not present in the images):

$png24 = imagecreatetruecolor($size[0], $size[1]);
$transparentIndex = imagecolorallocate($png24, 0x66, 0xff, 0x66);
imagefill($png24, 0, 0, $transparentIndex);

This is for making the green color transparent:

imagecolortransparent($png24, $transparentIndex);     

Then i copy the png8 into the PNG-24:

imagecopy($png24, $png8, 0, 0, 0, 0, $size[0], $size[1]);

So here's the problem: the original PNG-8 looks good, but it has a green border surrounding the shape within the original image. It's difficult to explain really. Seems like some part of the green background is left in the remaining PNG.

What can i do?

thanks in advance

best regards,

Fernando

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

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

发布评论

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

评论(1

回梦 2024-11-25 04:58:20

我之前遇到过一些关于 png 透明度的问题,并且能够用这种模式解决它们:

// allocate original image to copy stuff to
$img = imagecreatetruecolor(200, 100);

// create second image
$bg = imagecreatefrompng('bg.png');

// copy image onto it using imagecopyresampled
imagecopyresampled($img, $bg, 0, 0, 0, 0, 200, 100, 200, 100);
imagedestroy($bg);

// create third image
// do same routine
$fg = imagecreatefrompng('fg.png');
imagecopyresampled($img, $fg, 50, 50, 0, 0, 50, 50, 50, 50);
imagedestroy($fg);

// output image
imagepng($img);
imagedestroy($img);

我认为我的和你的之间的唯一区别是 imagecopy()imagecopyresampled() 。我似乎记得有问题,尽管那是很久以前的事了。您可以在此处查看我使用此模式的图像示例: http://www.ipnow.org/images/1/bggrad/bg4/yes/TRANSIT.TTF/8B0000/custombrowserimage.jpg (我分配了一个空白图像,复制背景图像中,复制带有透明度的叠加层)

I had some problems with png transparency before and was able to solve them with this pattern:

// allocate original image to copy stuff to
$img = imagecreatetruecolor(200, 100);

// create second image
$bg = imagecreatefrompng('bg.png');

// copy image onto it using imagecopyresampled
imagecopyresampled($img, $bg, 0, 0, 0, 0, 200, 100, 200, 100);
imagedestroy($bg);

// create third image
// do same routine
$fg = imagecreatefrompng('fg.png');
imagecopyresampled($img, $fg, 50, 50, 0, 0, 50, 50, 50, 50);
imagedestroy($fg);

// output image
imagepng($img);
imagedestroy($img);

I think the only difference between mine and yours is imagecopy() vs. imagecopyresampled(). I seem to remember having problems with that though it was quite a while ago. You can see an example of an image I use this pattern on here: http://www.ipnow.org/images/1/bggrad/bg4/yes/TRANSIST.TTF/8B0000/custombrowserimage.jpg (I allocate a blank image, copy the background image in, copy the overlay with transparency in)

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