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);?>

原图效果:

生成模糊图片后的效果:

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《php文件操作总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php面向对象程序设计入门教程》、《PHP网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值