本篇内容介绍了“php怎么把png图片白色背景色改为透明色”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
先看下面一段代码,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图片白色背景色改为透明色”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。