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

PHP如何模糊图片案例

本篇文章主要介绍php如何实现生成模糊图片,感兴趣的朋友参考下,希望对大家有所帮助。
代码如下:
<?php class 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); ?>
相关推荐:
如何实现php代码处理图片
用python处理图片之打开\显示\保存图像的方法
用python处理图片实现图像中的像素访问
以上就是php如何模糊图片案例的详细内容。
其它类似信息

推荐信息