如何使用 PHP 将透明 PNG 与图像合并? [英] How to merge transparent PNG with image using PHP?

查看:29
本文介绍了如何使用 PHP 将透明 PNG 与图像合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 top of 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

注意:我不想只使用 HTML 来做到这一点(我通过编写一个 javascript 插件将透明 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).

谢谢.

推荐答案

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

You can merge the two images together using the PHP GD2 library.

示例:

<?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);
?>

这篇关于如何使用 PHP 将透明 PNG 与图像合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆