合并jpg的png顶部,并使用php保持透明度 [英] Merge a png ontop of a jpg and retain transparency with php

查看:88
本文介绍了合并jpg的png顶部,并使用php保持透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PNG,我正在尝试将其合并到JPG上.使用以下代码

I have a PNG and I'm trying to merge it on top of a JPG. With the following code

$dest = imagecreatefromjpeg("example.jpg");
$src = imagecreatefrompng("example.png");

imagealphablending($dest, false);
imagesavealpha($dest, true);

imagealphablending($src, true);

imagecopymerge($dest, $src, $src2x, $src2y, 0, 0, $src2w, $src2h, 100);

header('Content-Type: image/png');
imagepng($dest, "user/".$imei."/".$picCount."_m");

imagedestroy($dest);
imagedestroy($src);

结果如下

我还尝试了来自类似问题的建议,该建议说使用"imagecopyresampled"代表"imagecopymerge",但是当我做到了,圣诞老人的帽子根本没有出现.

I also tried a suggestion from a similar question which said to use 'imagecopyresampled' isntead of 'imagecopymerge' but when I did that the santa hat didn't show up at all.

为了使圣诞老人帽在合并时保持透明,我需要更改什么?

What do I need to change to make the santa hat keep it's transparency when merged?

推荐答案

解决方案都需要使用"imagecopyresampled".还要从发布的源代码中删除第4行和第5行.

Solution required both using 'imagecopyresampled'. As well a removing lines 4 and 5 from the posted source code.

imagealphablending($dest, false);
imagesavealpha($dest, true);

这是完整的工作版本

$dest = imagecreatefromjpeg("example.jpg");
$src = imagecreatefrompng("example.png");

imagecopyresampled($dest, $src, $src2x, $src2y, 0, 0, $src2w, $src2h, $src2w, $src2h); 

header('Content-Type: image/png');
imagejpeg($dest, "user/".$imei."/".$picCount."_m.jpeg");

imagedestroy($dest);
imagedestroy($src);

这篇关于合并jpg的png顶部,并使用php保持透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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