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

2022-08-30 19:49:26

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

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

谢谢。


答案 1

您可以使用 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);
?>

答案 2

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


推荐