PHP imagecopymerge源图像的背景更改透明为黑色

时间:2013-05-07 06:35:49

标签: php

我需要合并两张图片, 我的代码是这样的:

$image = imagecreatefromjpeg("images/big.jpg"); 
$image1 = imagecreatefrompng("small_image/8.png");
$size = getimagesize("small_image/8.png");


imagecopymerge($image, $image1, 400, 30, 0, 0, $size[0], $size[1], 100);
header('Content-Type: image/gif');
imagegif($image);
imagedestroy($image1);


第一张照片是:

enter image description here

第二是: enter image description here

最终结果不透明

enter image description here

请帮忙!!!谢谢!

新版代码:

$image = imagecreatefromjpeg("images/big.jpg"); 
$image1 = imagecreatefrompng("small_image/8.png");
$size = getimagesize("small_image/8.png");
$background = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $background);
imagealphablending($image1, false);
imagesavealpha($image1, true);


imagecopymerge($image, $image1, 400, 30, 0, 0, $size[0], $size[1], 100);

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


imagegif($image);
imagedestroy($image1);

结果是:     enter image description here

1 个答案:

答案 0 :(得分:1)

好的..我现在找到了一个解决方案..

$image = imagecreatefromjpeg("images/show01.jpg"); //
$image1 = imagecreatefrompng("small_image/1.png");//
$image3 = imagecreatefromgif("images/carBg.gif");//
$size = getimagesize("small_image/1.png");
$overlay = imagecreatetruecolor(80, 80);

$white = imagecolorallocate($overlay, 229, 229, 229);
imagefilledrectangle($overlay, 0, 0, 80, 80, $white);

imagecolortransparent($overlay,$white);  
     imagecopy($overlay, $image1, (80-$size[0])/2, (80-$size[1])/2, 0, 0,   $size[0],$size[1]);
imagecopymerge($image3, $overlay, 0, 0, 0, 0, 80, 80, 100);


imagecopymerge($image, $image3, 280, 30, 0, 0, 80, 80, 100);

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

// and finally, output the result
imagepng($image);
imagedestroy($image);