dongpu1881 2011-09-01 10:59
浏览 237
已采纳

将多个透明PNG合并为一个

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;
}
  • 写回答

1条回答 默认 最新

  • duanjie2701 2011-09-01 11:01
    关注

    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路