微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

本文实例讲述了PHP实现生成模糊图片方法分享给大家供大家参考,具体如下:

rush:PHP;"> 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); } /** * h3 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,$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,$originalWidth,$originalHeight,$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,3); ?>

原图效果

生成模糊图片后的效果

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》、《》及《

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐