php 如何使用PHP将透明PNG与图像合并?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1394061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to merge transparent PNG with image using PHP?
提问by subZero
The situation is this: I have a small 50x50 pic. I also have a small 50x50 transparent picture which contains a frame for the 50x50 pic, so I basically want to put the transparent png on topof the image and merge those two which would lead to a final third picture that looks something like this: http://img245.imageshack.us/i/50x50n.png
情况是这样的:我有一张 50x50 的小图片。我还有一张 50x50 的小透明图片,其中包含 50x50 图片的框架,所以我基本上想将透明 png放在图像的顶部并将这两个合并,这将导致最后的第三张图片看起来像这样:http ://img245.imageshack.us/i/50x50n.png
Note: I don't want to do this using HTML only (I achieved this by writing a javascript plugin that put the transparent png on top of the original image).
注意:我不想只使用 HTML 来做到这一点(我通过编写一个将透明 png 放在原始图像顶部的 javascript 插件来实现这一点)。
Thanks.
谢谢。
回答by zanbaldwin
You can merge the two images together using the PHP GD2 library.
您可以使用 PHP GD2 库将两个图像合并在一起。
Example:
例子:
<?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);
?>
回答by Raf
Add imagealphablending($frame,true);before imagecopymerge()if you want to keep PNG frame transparancy over the image.
如果要在图像上保持 PNG 帧透明度,请在imagealphablending($frame,true);之前添加imagecopymerge()。
回答by Alex Gyoshev
You can do it using ImageMagick :: Composite. The first user contributed note should be enough to grasp the concept.
您可以使用ImageMagick :: Composite来做到这一点。第一个用户贡献的注释应该足以掌握这个概念。

