将多个透明 PNG 合并为一个

发布于 2024-12-02 14:11:10 字数 1096 浏览 5 评论 0原文

我正在用弯曲的边框制作广告。

以下是广告示例:http://imageshack.us/f/20/4e5f5fe94b327new60seciq.png/

我正在尝试复制在 Photoshop 中完成的操作,将一个放在另一个之上。这是我正在使用的代码:

// create destination canvas
$dest_img = imagecreatetruecolor(176, 75);

// Make the background transparent
$black = imagecolorallocate($dest_img, 0, 0, 0);
imagecolortransparent($dest_img, $black);

imageAlphaBlending($dest_img, false);
imageSaveAlpha($dest_img, true);

// copy ad into destination
imagecopy($dest_img, $ad_image, 0, 0, 0, 0, 176, 75);

// copy frame onto first half of image
imagecopy($dest_img, $curve_image, 0, 0, 0, 0, 88, 75);

发生的情况是最后一个副本(框架)优先,我没有看到广告,而是看到了一个透明的块。这是 GD 正在做的事情的放大图像:

http://imageshack.us/f/ 684/unled1to.png/

我希望有一个简单的解决方案可以让下层保持可见 - 如果没有,我想我将不得不编写一个函数并逐像素进行比较......

if (bottom_px == trans && top_px == trans) {
    dest_px = trans;
}
else {
    dest_px = top_px;
}

I am framing ads with a curved border.

Here is a sample ad: http://imageshack.us/f/20/4e5f5fe94b327new60seciq.png/

I am trying to replicate what would be done in Photoshop, place one on top of the other. Here is the code I'm using:

// create destination canvas
$dest_img = imagecreatetruecolor(176, 75);

// Make the background transparent
$black = imagecolorallocate($dest_img, 0, 0, 0);
imagecolortransparent($dest_img, $black);

imageAlphaBlending($dest_img, false);
imageSaveAlpha($dest_img, true);

// copy ad into destination
imagecopy($dest_img, $ad_image, 0, 0, 0, 0, 176, 75);

// copy frame onto first half of image
imagecopy($dest_img, $curve_image, 0, 0, 0, 0, 88, 75);

What is happening is that the last copy to take place (the frame) is taking priority and instead of seeing the ad, im getting a transparent block. Here is a blown up image of what GD is doing:

http://imageshack.us/f/684/unled1to.png/

I'm hoping there is a simple solution to get the lower layer to remain visible - if not I think I will have to write a function and go pixel by pixel and compare...

if (bottom_px == trans && top_px == trans) {
    dest_px = trans;
}
else {
    dest_px = top_px;
}

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

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

发布评论

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

评论(1

一人独醉 2024-12-09 14:11:10

imagealphablending 设置为 true。手册中强调了:

在混合模式下,提供给所有绘图函数(例如 imagesetpixel())的颜色的 Alpha 通道分量确定应允许透出多少底层颜色。结果,gd 自动将该点的现有颜色与绘图颜色混合,并将结果存储在图像中。生成的像素是不透明的。 在非混合模式下,绘图颜色将按其 Alpha 通道信息逐字复制,替换目标像素。在调色板图像上绘图时,混合模式不可用。

另外,您实际上并没有将背景着色为透明。您只是告诉 $black 是透明的。相反,应将 imagefillimagecolorallocatealpha 结合使用:

imagefill($dest_img, 0, 0, imagecolorallocatealpha($dest_img, 0, 0, 0, 127));

Set imagealphablending to true. From the manual, emphasis added:

In blending mode, the alpha channel component of the color supplied to all drawing function, such as imagesetpixel() determines how much of the underlying color should be allowed to shine through. As a result, gd automatically blends the existing color at that point with the drawing color, and stores the result in the image. The resulting pixel is opaque. In non-blending mode, the drawing color is copied literally with its alpha channel information, replacing the destination pixel. Blending mode is not available when drawing on palette images.

Also, you are not actually coloring the background transparent. You are just telling that $black is transparent. Instead, use imagefill with imagecolorallocatealpha:

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