最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

php如何实现图片模糊

来源:懂视网 责编:小采 时间:2020-11-02 18:27:58
文档

php如何实现图片模糊

php如何实现图片模糊:php实现图片模糊的方法:首先创建一个PHP示例文件;然后定义一个image_blur类;接着创建gaussian_blur和blur方法;最后通过$image_blur->gaussian_blur();方法实现图片模糊。推荐:《PHP视频教程》本文实例讲述了PHP实现生成模糊图
推荐度:
导读php如何实现图片模糊:php实现图片模糊的方法:首先创建一个PHP示例文件;然后定义一个image_blur类;接着创建gaussian_blur和blur方法;最后通过$image_blur->gaussian_blur();方法实现图片模糊。推荐:《PHP视频教程》本文实例讲述了PHP实现生成模糊图

php实现图片模糊的方法:首先创建一个PHP示例文件;然后定义一个“image_blur”类;接着创建“gaussian_blur”和“blur”方法;最后通过“$image_blur->gaussian_blur();”方法实现图片模糊。

推荐:《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如何实现图片模糊

php如何实现图片模糊:php实现图片模糊的方法:首先创建一个PHP示例文件;然后定义一个image_blur类;接着创建gaussian_blur和blur方法;最后通过$image_blur->gaussian_blur();方法实现图片模糊。推荐:《PHP视频教程》本文实例讲述了PHP实现生成模糊图
推荐度:
标签: 图片 php 实现
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top