GD处理图片
简介:
GD处理图片
function out_png($from_pic,$save_path=""){
$begin_r = 204;
$begin_g = 204;
$begin_b = 204;
$src_im = imagecreatefromstring(file_get_contents($from_pic)); //读取png图片
$src_w = imagesx($src_im);$src_h=imagesy($src_im);
imagesavealpha($src_im,true);//这里很重要 意思是不要丢了$src_im图像的透明色
$src_white = imagecolorallocatealpha($src_im, 255, 255, 255,0); // 创建一副白色透明的画布
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>200 && $r<205) && ($g>200 && $g<205) && ($b>200 && $b<205)){
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);
imagesavealpha($target_im,true);//这里很重要,意思是不要丢了$target_im图像的透明色;
$tag_white = imagecolorallocatealpha($target_im, 255, 255, 255,0);//把生成新图的白色改为透明色 存为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);//合并原图
if($save_path){
$res = imagepng($src_im,$save_path);
return $res;
}
else
{
imagepng($src_im);
}
}
上一篇: laravel 项目从github仓库拉取加载类库
下一篇: PHP7.X安装memcache扩展