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

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

问题描述

有人有可以合并两个PNG图像的脚本吗?

满足以下条件:

  • 两张图片都有透明区域
  • 第二张图片的不透明度必须为 50%(它覆盖在第一张图片上)

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

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

  • 这就是它应该的样子(左)和我的代码的结果(右)

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

解决方案

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

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

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

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

With the following conditions:

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

Here is what I tried to do but without luck:

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

?>

Edit:

  • First Image (left) and Second Image (right)

  • This is how it should be (left) and the result of my code (right)

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

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天全站免登陆