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

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

  • 写回答

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序