如何使用PHP将透明PNG与图像合并?

sub*_*ero 20 php merge png resize image

情况是这样的:我有一个50x50的小图片.我还有一个小的50x50透明图片,其中包含一个50x50 pic的帧,所以我基本上想把透明的png 放在图像的顶部并合并这两个,这将导致最终的第三张图片看起来像这样:http ://img245.imageshack.us/i/50x50n.png

注意:我不想仅使用HTML(我通过编写一个将透明png放在原始图像顶部的javascript插件来实现这一点).

谢谢.

zan*_*win 29

您可以使用PHP GD2库将两个图像合并在一起.

例:

<?php
 # If you don't know the type of image you are using as your originals.
 $image = imagecreatefromstring(file_get_contents($your_original_image));
 $frame = imagecreatefromstring(file_get_contents($your_frame_image));

 # If you know your originals are of type PNG.
 $image = imagecreatefrompng($your_original_image);
 $frame = imagecreatefrompng($your_frame_image);

 imagecopymerge($image, $frame, 0, 0, 0, 0, 50, 50, 100);

 # Save the image to a file
 imagepng($image, '/path/to/save/image.png');

 # Output straight to the browser.
 imagepng($image);
?>
Run Code Online (Sandbox Code Playgroud)


小智 8

如果要在图像上保持PNG帧透明度,请在imagealphablending($frame,true);之前添加imagecopymerge().

  • 图像仍然透明,透明区域为白色. (3认同)