使用PHP GD库合并两个PNG图像 [英] Merge two PNG images with PHP GD library

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

问题描述

是否有人有可以合并两张PNG图像的脚本?

Does anybody have a script which can merge two PNG images?

符合以下条件:


  • 两个图像都有透明区域

  • 第二个图像必须具有50%的不透明度(它覆盖在第一个图像上

  • Both images have transparent areas
  • The second image must have 50% opacity (it is overlaid over the first image)

这是我尝试做但没有运气的事情:

<?php

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
    $cut = imagecreatetruecolor($src_w, $src_h);
    imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 
    imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 
    imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
}

$image1 = imagecreatefrompng('a.png'); //300 x 300
$image2 = imagecreatefrompng('b.png'); //150 x 150

$merged_image = imagecreatetruecolor(300, 300);
imagealphablending($merged_image, false);
imagesavealpha($merged_image, true);

imagecopy($merged_image, $image1, 0, 0, 0, 0, 300, 300);
imagecopymerge_alpha($merged_image, $image2, 0, 0, 0, 0, 150, 150, 50);

header('Content-Type: image/png');
imagepng($merged_image);

?>

修改:


  • 第一张图片(左)和第二张图片(右)


  • 这是它应该如何(左)和我的代码的结果(右)


  • dqhendricks提出的解决方案的结果

  • The result of the solution proposed by dqhendricks

推荐答案

$image1 = imagecreatefrompng('a.png'); //300 x 300
$image2 = imagecreatefrompng('b.png'); //150 x 150
imagecopymerge($image1, $image2, 0, 0, 75, 75, 150, 150, 50);

这应该就是你所需要的。 $ image1应保存合并后的图像,其中image2已覆盖50%不透明度。最后一个参数是合并副本的alpha。

this should be all you need. $image1 should hold the merged image where image2 has been overlayed with 50% opacity. the last argument is the alpha of the merged copy.

http://php.net/manual/en/function.imagecopymerge.php

这篇关于使用PHP GD库合并两个PNG图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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