前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php 处理png图片白色背景色改为透明色的实例代码

php 处理png图片白色背景色改为透明色的实例代码

作者头像
砸漏
发布2020-10-21 15:31:52
2K0
发布2020-10-21 15:31:52
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本恩蓝脚本

先看下面一段代码,php 处理png图片白色背景色改为透明色

function pngMerge($o_pic,$out_pic){
 $begin_r = 255;
 $begin_g = 250;
 $begin_b = 250;
 list($src_w, $src_h) = getimagesize($o_pic);// 获取原图像信息 宽高
 $src_im = imagecreatefrompng($o_pic); //读取png图片
 print_r($src_im);
 imagesavealpha($src_im,true);//这里很重要 意思是不要丢了$src_im图像的透明色
 $src_white = imagecolorallocatealpha($src_im, 255, 255, 255,127); // 创建一副白色透明的画布
 for ($x = 0; $x < $src_w; $x++) {
  for ($y = 0; $y < $src_h; $y++) {
    $rgb = imagecolorat($src_im, $x, $y);
    $r = ($rgb    16) & 0xFF;
    $g = ($rgb    8) & 0xFF;
    $b = $rgb & 0xFF;
    if($r==255 && $g==255 && $b == 255){
    imagefill($src_im,$x, $y, $src_white); //填充某个点的颜色
    imagecolortransparent($src_im, $src_white); //将原图颜色替换为透明色
    }
    if (!($r <= $begin_r && $g <= $begin_g && $b <= $begin_b)) {
     imagefill($src_im, $x, $y, $src_white);//替换成白色
     imagecolortransparent($src_im, $src_white); //将原图颜色替换为透明色
    }
  }
 }
 $target_im = imagecreatetruecolor($src_w, $src_h);//新图
 imagealphablending($target_im,false);//这里很重要,意思是不合并颜色,直接用$target_im图像颜色替换,包括透明色;
 imagesavealpha($target_im,true);//这里很重要,意思是不要丢了$target_im图像的透明色;
 $tag_white = imagecolorallocatealpha($target_im, 255, 255, 255,127);//把生成新图的白色改为透明色 存为tag_white
 imagefill($target_im, 0, 0, $tag_white);//在目标新图填充空白色
 imagecolortransparent($target_im, $tag_white);//替换成透明色
 imagecopymerge($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, 100);//合并原图和新生成的透明图
 imagepng($target_im,$out_pic);
 return $out_pic;
}
$o_pic = '1.png';
$name = pngMerge($o_pic,'aaaa.png');
print_r($name);

补充:用PHP的GD库把图片的背景替换成透明背景

之前写个功能用PHP把图片的背景弄成透明,之留下文字(黑色的),我也在百度上找,也试过别人的代码。大多数代码的思路都是这样:

生成新的画布,读取源图片每个坐标的颜色,不符合要求的用imagecolortransparent()函数将该颜色替换成透明的。

$o_pic = '1.jpg';
//要处理的色阶起始值
$begin_r = 215;
$begin_g = 215;
$begin_b = 215;
list($src_w,$src_h,$src_type) = getimagesize($o_pic);// 获取原图像信息
$file_ext = get_ext($o_pic);//获取扩展名
$target_im = imagecreatetruecolor($src_w,$src_h);//新图
if($file_ext == 'jpg') //转换JPG 开始
{
  $src_im = ImageCreateFromJPEG($o_pic);
  imagecopymerge($target_im,$src_im,0,0,0,0,$src_w,$src_h,100);
  for($x = 0; $x < $src_w; $x++)
  {
    for($y = 0; $y < $src_h; $y++)
    {
      $rgb = imagecolorat($src_im, $x, $y);
      $r = ($rgb    16) & 0xFF;
      $g = ($rgb    8) & 0xFF;
      $b = $rgb & 0xFF;
      if($r   $begin_r && $g   $begin_g && $b   $begin_b ){  
        imagecolortransparent($target_im, imagecolorallocate($target_im,$r, $g, $b));        
      }
    }
  }
}

但是用了这个思路,图片的背景一直都不能便透明,改了好多次。 后来发现只有最后一次imagecolortransparent()有效果,前面都都被覆盖了。

把思路改了下,把不要的颜色先统一转换成白色,最后再将白色替换成透明

$begin_r = 98;
$begin_g = 98;
$begin_b = 98;
list($src_w, $src_h) = getimagesize($o_pic);// 获取原图像信息
$src_im = imagecreatefromjpeg($o_pic);
//imagecopymerge($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, 100);
//imagecopyresampled($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, $src_w, $src_h);
$i = 0;
$src_white = imagecolorallocate($src_im, 255, 255, 255);
for ($x = 0; $x < $src_w; $x++) {
  for ($y = 0; $y < $src_h; $y++) {
   $rgb = imagecolorat($src_im, $x, $y);
   $r = ($rgb    16) & 0xFF;
   $g = ($rgb    8) & 0xFF;
   $b = $rgb & 0xFF;
   if($r==255 && $g==255 && $b == 255){
     $i ++;
     continue;
   }
   if (!($r <= $begin_r && $g <= $begin_g && $b <= $begin_b)) {
     imagefill($src_im, $x, $y, $src_white);//替换成白色
   }
  }
}
$target_im = imagecreatetruecolor($src_w, $src_h);//新图
$tag_white = imagecolorallocate($target_im, 255, 255, 255);
imagefill($target_im, 0, 0, $tag_white);
imagecolortransparent($target_im, $tag_white);
imagecopymerge($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, 100);

总结

以上所述是小编给大家介绍的php 处理png图片白色背景色改为透明色的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对ZaLou.Cn网站的支持!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档