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


Posted in PHP onDecember 21, 2017

本文实例讲述了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 相关文章推荐
在PHP里得到前天和昨天的日期的代码
Aug 16 PHP
Zend Framework中的简单工厂模式 图文
Jul 10 PHP
浅析关于PHP位运算的简单权限设计
Jun 30 PHP
php获取文件夹路径内的图片以及分页显示示例
Mar 11 PHP
smarty中英文多编码字符截取乱码问题解决方法
Oct 28 PHP
PHP基于imap获取邮件实例
Nov 11 PHP
PHP解密Unicode及Escape加密字符串
May 17 PHP
php实现多城市切换特效
Aug 09 PHP
PHP函数nl2br()与自定义函数nl2p()换行用法分析
Apr 02 PHP
PHP读取文本文件并逐行输出该行使用最多的字符与对应次数的方法
Nov 25 PHP
php实现mysql连接池效果实现代码
Jan 25 PHP
php给数组赋值的实例方法
Sep 26 PHP
PHPCrawl爬虫库实现抓取酷狗歌单的方法示例
Dec 21 #PHP
在云虚拟主机部署thinkphp5项目的步骤详解
Dec 21 #PHP
php获取微信共享收货地址的方法
Dec 21 #PHP
php实现socket推送技术的示例
Dec 20 #PHP
PHP实现模拟http请求的方法分析
Dec 20 #PHP
php封装db类连接sqlite3数据库的方法实例
Dec 19 #PHP
PHP性能分析工具xhprof的安装使用与注意事项
Dec 19 #PHP
You might like
一些被忽视的PHP函数(简单整理)
2010/04/30 PHP
PHP中使用CURL获取页面title例子
2015/01/07 PHP
PHP处理postfix邮件内容的方法
2015/06/16 PHP
Zend Framework教程之路由功能Zend_Controller_Router详解
2016/03/07 PHP
PHP实现仿百度文库,豆丁在线文档效果(word,excel,ppt转flash)
2016/03/10 PHP
让你的PHP7更快之Hugepage用法分析
2016/05/31 PHP
js实现点击获取验证码倒计时效果
2021/01/28 Javascript
js 弹出虚拟键盘修改密码的简单实例
2016/10/10 Javascript
Bootstrap风格的WPF样式
2016/12/07 Javascript
JS正则表达式修饰符中multiline(/m)用法分析
2016/12/27 Javascript
JavaScript之排序函数_动力节点Java学院整理
2017/06/30 Javascript
红黑树的插入详解及Javascript实现方法示例
2018/03/26 Javascript
node中的cookie的具体使用
2018/09/13 Javascript
详解key在Vue列表渲染时究竟起到了什么作用
2019/04/20 Javascript
Vue $emit()不能触发父组件方法的原因及解决
2020/07/28 Javascript
Python中使用socket发送HTTP请求数据接收不完整问题解决方法
2015/02/04 Python
深入Python函数编程的一些特性
2015/04/13 Python
Python的装饰器模式与面向切面编程详解
2015/06/21 Python
python通过socket查询whois的方法
2015/07/18 Python
Python编写登陆接口的方法
2017/07/10 Python
python 环境变量和import模块导入方法(详解)
2017/07/11 Python
Python实现二维数组按照某行或列排序的方法【numpy lexsort】
2017/09/22 Python
超简单使用Python换脸实例
2019/03/27 Python
pyqt5 实现工具栏文字图片同时显示
2019/06/13 Python
python 输入字符串生成所有有效的IP地址(LeetCode 93号题)
2020/10/15 Python
Maisons du Monde德国:法国家具和装饰的市场领导者
2019/07/26 全球购物
在购买印度民族服饰:Soch
2020/09/15 全球购物
巴西购物网站:Onofre Agora
2020/06/08 全球购物
印刷工程专业应届生求职信
2013/09/29 职场文书
同事打架检讨书
2014/02/04 职场文书
《颐和园》教学反思
2014/02/26 职场文书
车间质检员岗位职责
2015/04/08 职场文书
如何用JavaScipt测网速
2021/05/09 Javascript
刚学完怎么用Python实现定时任务,转头就跑去撩妹!
2021/06/05 Python
新手入门Mysql--sql执行过程
2021/06/20 MySQL
Java使用jmeter进行压力测试
2021/07/09 Java/Android