关于php使用gd库合并图像 图像模糊处理方法

1,111 阅读1分钟

使用GD库 创建透明图像时 多图像合并时 内图边缘模糊解决方案

最近开发了一个项目,功能是图像,文字生成一个简单的logo,比较简单我用的是php的GD库图像处理进行实现的,开发的时候遇到一个问题透明背景时 图像边缘会模糊有一道黑线效果如下图

代码如下:

  // 创建图像实例 

 $filename = $choose_image['url']; 

 $info = getimagesize($filename);

 $width = $info[0]; $height = $info[1]; 

 $new_height = (200/$info[0])*$info[1]; //获取图片的后缀 

 $type = image_type_to_extension($info[2], false); //拼接图片资源句柄函数

 $func = 'imagecreatefrom'.$type; //创建图片资源句柄 

 $image = $func($filename); 

 $im = imagecreatetruecolor(300, 300); 

 imagecopyresampled($im, $image, 50, 0,0,0,200,$new_height,$width,$height); $red=imagecolorallocate($im,255,255,255);

 imagefill($im,0,0,$red); 

 imagecolortransparent($im,$red); 

 //设置文件路径

 $ml = 'uploads/picture'.'/'.date('Ymd', time());

//目录名称

 $url =$ml.'/'.time();

//存储的路径 

 imagepng($im, $url . image_type_to_extension(IMAGETYPE_PNG),9);

解决之后 图像边缘清晰 不会出现模糊情况,方案是在新建一个画布,把logo图像放在新的画布上然后两个对两个画布进行合并imagecopyresampled(),效果如下

代码如下:

// 创建图像实例 

 $filename = $choose_image['url']; 

 $info = getimagesize($filename); 

 $width = $info[0]; 

 $height = $info[1]; 

 $info_im = imagecreatetruecolor($width, $height); 

 $info_color = imagecolorallocate($info_im, 255, 255, 255);

//白色背景 

 imagefill($info_im, 0, 0, $info_color); 

 $new_height = (200/$info[0])*$info[1]; 

 //获取图片的后缀 

 $type = image_type_to_extension($info[2], false); 

 //拼接图片资源句柄函数 

 $func = 'imagecreatefrom'.$type; 

 //创建图片资源句柄 

 $image = $func($filename); 

 imagecopyresampled($info_im, $image, 0, 0,0,0,$width,$height,$width,$height); 

 $im = imagecreatetruecolor(300, 300); 

 imagecopyresampled($im, $info_im, 50, 0,0,0,200,$new_height,$width,$height); $red=imagecolorallocate($im,255,255,255); 

 imagefill($im,0,0,$red); 

 imagecolortransparent($im,$red);

这是我发的第一次发文章 内容也比较基础,但是这个问题应该很少人会碰到比较偏,在这里记录下 希望能帮到别人