您好,欢迎访问一九零五行业门户网

PHP实现生成模糊图片的方法示例讲解

这篇文章主要介绍了php实现生成模糊图片的方法,涉及php高斯算法实现图片模糊功能相关实现技巧,需要的朋友可以参考下
本文实例讲述了php实现生成模糊图片的方法。分享给大家供大家参考,具体如下:
<?phpclass image_blur{/** * 图片高斯模糊(适用于png/jpg/gif格式) * @param $srcimg 原图片 * @param $savepath 保存路径 * @param $savename 保存名字 * @param $positon 模糊程度 * *基于martijn frazer代码的扩充, 感谢 martijn frazer */ public function gaussian_blur($srcimg,$savepath=null,$savename=null,$blurfactor=3){ $gdimageresource=$this->image_create_from_ext($srcimg); $srcimgobj=$this->blur($gdimageresource,$blurfactor); $temp = pathinfo($srcimg); $name = $temp['basename']; $path = $temp['dirname']; $exte = $temp['extension']; $savename = $savename ? $savename : $name; $savepath = $savepath ? $savepath : $path; $savefile = $savepath .'/'. $savename; $srcinfo = @getimagesize($srcimg); switch ($srcinfo[2]) { case 1: imagegif($srcimgobj, $savefile); break; case 2: imagejpeg($srcimgobj, $savefile); break; case 3: imagepng($srcimgobj, $savefile); break; default: return '保存失败'; //保存失败 } return $savefile; imagedestroy($srcimgobj); } /** * strong blur * * @param $gdimageresource 图片资源 * @param $blurfactor 可选择的模糊程度 * 可选择的模糊程度 0使用 3默认 超过5时 极其模糊 * @return gd image 图片资源类型 * @author martijn frazer, idea based on http://stackoverflow.com/a/20264482 */ private function blur($gdimageresource, $blurfactor = 3) { // blurfactor has to be an integer $blurfactor = round($blurfactor); $originalwidth = imagesx($gdimageresource); $originalheight = imagesy($gdimageresource); $smallestwidth = ceil($originalwidth * pow(0.5, $blurfactor)); $smallestheight = ceil($originalheight * pow(0.5, $blurfactor)); // for the first run, the previous image is the original input $previmage = $gdimageresource; $prevwidth = $originalwidth; $prevheight = $originalheight; // scale way down and gradually scale back up, blurring all the way for($i = 0; $i < $blurfactor; $i += 1) { // determine dimensions of next image $nextwidth = $smallestwidth * pow(2, $i); $nextheight = $smallestheight * pow(2, $i); // resize previous image to next size $nextimage = imagecreatetruecolor($nextwidth, $nextheight); imagecopyresized($nextimage, $previmage, 0, 0, 0, 0, $nextwidth, $nextheight, $prevwidth, $prevheight); // apply blur filter imagefilter($nextimage, img_filter_gaussian_blur); // now the new image becomes the previous image for the next step $previmage = $nextimage; $prevwidth = $nextwidth; $prevheight = $nextheight; } // scale back to original size and blur one more time imagecopyresized($gdimageresource, $nextimage, 0, 0, 0, 0, $originalwidth, $originalheight, $nextwidth, $nextheight); imagefilter($gdimageresource, img_filter_gaussian_blur); // clean up imagedestroy($previmage); // return result return $gdimageresource; } private function image_create_from_ext($imgfile) { $info = getimagesize($imgfile); $im = null; switch ($info[2]) { case 1: $im=imagecreatefromgif($imgfile); break; case 2: $im=imagecreatefromjpeg($imgfile); break; case 3: $im=imagecreatefrompng($imgfile); break; } return $im; }}$image_blur = new image_blur();$image_blur->gaussian_blur("./1.jpg",null,null,3);?>
原图效果:
生成模糊图片后的效果:
您可能感兴趣的文章:laravel 5.5基于内置的auth模块实现前后台登陆的详解
php二维数组实现去除重复项的方法
thinkphp5行为使用方法的汇总
以上就是php实现生成模糊图片的方法示例讲解的详细内容。
其它类似信息

推荐信息