douyu1656
2013-05-07 06:35
浏览 345
已采纳

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

I need to merge two pictures, my code is like this:

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


the frist picture is :

enter image description here

second is : enter image description here

final result not transparent

enter image description here

Please help!!!Thanks!

new version of code:

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

and the result is : enter image description here

图片转代码服务由CSDN问答提供 功能建议

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

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


frist图片是:

第二个是: “在此输入图像描述”

最终结果不透明

请帮助!!!谢谢!

新版代码:< / p>

  $ image = imagecreatefromjpeg(“images / big.jpg”);  
 $ image1 = imagecreatefrompng(“small_image / 8.png”); 
 $ size = getimagesize(“small_image / 8.png”); 
 $ background = imagecolorallocate($ image,0,0,0); \  nimagecolortransparent($ 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); 
 <  / code>  
 
 

,结果是:

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyi5817 2013-05-09 05:43
    已采纳

    Ok..i found a solution now..

    $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);
    
    点赞 评论

相关推荐 更多相似问题