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

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题