濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > PHP實(shí)現(xiàn)生成模糊圖片的方法示例

PHP實(shí)現(xiàn)生成模糊圖片的方法示例

熱門(mén)標(biāo)簽:外呼系統(tǒng)人工客服 網(wǎng)絡(luò)電話400申請(qǐng) 福建高頻外呼防封系統(tǒng)哪家好 隨州銷(xiāo)售電銷(xiāo)機(jī)器人公司 周口網(wǎng)絡(luò)回?fù)芡夂粝到y(tǒng) 400電話申請(qǐng)辦理 商丘外呼系統(tǒng)好處 百度地圖標(biāo)注類型是酒店 全國(guó)各省地圖標(biāo)注點(diǎn)

本文實(shí)例講述了PHP實(shí)現(xiàn)生成模糊圖片的方法。分享給大家供大家參考,具體如下:

?php
class image_blur{
/**
  * 圖片高斯模糊(適用于png/jpg/gif格式)
  * @param $srcImg 原圖片
  * @param $savepath 保存路徑
  * @param $savename 保存名字
  * @param $positon 模糊程度
  *
  *基于Martijn Frazer代碼的擴(kuò)充, 感謝 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默認(rèn) 超過(guò)5時(shí) 極其模糊
 * @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);
?>

原圖效果:

生成模糊圖片后的效果:

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《php文件操作總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語(yǔ)法入門(mén)教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

您可能感興趣的文章:
  • PHP基于phpqrcode類生成二維碼的方法詳解
  • php實(shí)現(xiàn)生成帶二維碼圖片并強(qiáng)制下載功能
  • PHP實(shí)現(xiàn)可添加水印與生成縮略圖的圖片處理工具類
  • thinkphp5上傳圖片及生成縮略圖公共方法(分享)
  • php生成縮略圖質(zhì)量較差解決方法代碼示例
  • thinkPHP實(shí)現(xiàn)上傳圖片及生成縮略圖功能示例
  • 使用PHP生成二維碼的兩種方法(帶logo圖像)
  • PHP生成圖片驗(yàn)證碼、點(diǎn)擊切換實(shí)例
  • php實(shí)現(xiàn)上傳圖片生成縮略圖示例
  • php生成圖形驗(yàn)證碼幾種方法小結(jié)
  • PHP實(shí)現(xiàn)生成推廣海報(bào)的方法詳解

標(biāo)簽:樂(lè)山 海南 六安 佛山 南寧 定西 十堰 迪慶

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實(shí)現(xiàn)生成模糊圖片的方法示例》,本文關(guān)鍵詞  PHP,實(shí)現(xiàn),生成,模糊,圖片,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP實(shí)現(xiàn)生成模糊圖片的方法示例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于PHP實(shí)現(xiàn)生成模糊圖片的方法示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    娄烦县| 道真| 监利县| 个旧市| 纳雍县| 工布江达县| 三河市| 曲松县| 邵阳县| 方山县| 景洪市| 余江县| 水富县| 广灵县| 汽车| 宣恩县| 普格县| 合阳县| 洛隆县| 景德镇市| 张家口市| 赣州市| 郧西县| 云浮市| 兴宁市| 民县| 宁化县| 龙江县| 祁连县| 聂荣县| 肇庆市| 安徽省| 彩票| 滨州市| 穆棱市| 伊宁县| 原平市| 安徽省| 大余县| 北海市| 甘德县|