欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-11-20 08:24:48
本文实例讲述了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://*.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文件操作总结》、《php数组(array)操作技巧大全》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php面向对象程序设计入门教程》、《php网络编程技巧总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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