php 处理图片白色背景色改为透明色
作者:钓赛通
发布时间:2022-02-15
点击数:
php 处理图片白色背景色改为透明色的实例代码 外加滤镜
public 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 = imagecreatefromjpeg($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 >> 10) & 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);//新图
$bg = imagecolorallocate($target_im, 0, 120, 0);
for ($i = 255; $i >= 0; $i--) {
$color = imagecolorallocate($target_im, $i, $i, $i);
imagefilledrectangle($target_im, 0, $i, 255, 1, $color);
}
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);// 替换成透明色
ImageFilter($src_im,IMG_FILTER_COLORIZE,0,120,120); // 改变原来字体颜色
imagecopymerge($target_im, $src_im, 0, 0, 0, 0, $src_w, $src_h, 100);//合并原图和新生成的透明图
imagepng($target_im, $out_pic);
return $out_pic;
}$o_pic = public_path() . '/upload/bg1.jpg'; $name = $this->pngMerge($o_pic, public_path() . '/upload/aaaa.png'); echo '<div><img src="http://crm.xidakeji.test/public/upload/bg1.jpg"></div>'; echo '<div><img src="http://crm.xidakeji.test/public/upload/aaaa.png"></div>'; exit;
效果图片:

没有实现最终的效果,把电商侠三个字,变成从左到右的渐变色
