简体   繁体   English

PHP GD在JPG上叠加透明PNG

[英]PHP GD Overlay transparent PNG on top of JPG

I am trying to use PHP GD to overlay a frame onto an image. 我正在尝试使用PHP GD将框架覆盖到图像上。 Here is an example of my source images and what I am trying to achieve... 这是我的源图像以及我要实现的目标的示例。

The frame image is a transparent png, my code looks like this... 框架图像是透明的png,我的代码如下所示:

$dest = imagecreatefromjpeg('image.jpg');
$src = imagecreatefrompng('frame.png');

imagecopymerge($dest, $src, 0, 0, 0, 0, 300, 300, 50);

header('Content-Type: image/jpeg');

imagejpeg($dest, 'output.jpg');

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

My output image looks just like the middle one with no frame. 我的输出图像看起来就像没有框架的中间图像。 Can anyone spot anything obvious I am doing wrong? 谁能发现我做错的任何明显事情?

You should take a closer look at the documentation of imagecopymerge(). 您应该仔细查看imagecopymerge()的文档。 imagecopymerge() expects exactly 9 parameters, 10 are given in your script. imagecopymerge()期望恰好有9个参数,脚本中给出了10个。

Edit: imagecopymerge() can not handle alpha channels itself. 编辑:imagecopymerge()无法本身处理Alpha通道。 You have to add some extra lines of code. 您必须添加一些额外的代码行。 Here is what it should look like: 它应该是这样的:

$src = imagecreatefromjpeg('image.jpg');
$dest = imagecreatefrompng('frame.png');

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

$insert_x = imagesx($src); 
$insert_y = imagesy($src);

$white = imagecolorallocatealpha($dest, 255, 255, 255, 127); 
imagecolortransparent($dest, $white);
imagecopymerge($src, $dest, 0, 0, 0, 0, $insert_x, $insert_y, 100); 

header('Content-Type: image/jpeg');
imagejpeg($src);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM