文章主要介绍了php 处理png图片白色背景色改为透明色的实例代码,文中通过实例代码给大家介绍了用PHP的GD库把图片的背景替换成透明背景,需要的朋友参考下

先看下面一段代码,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图片白色背景色改为透明色的实例代码,希望对大家有所帮助。