欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年12月21日 11:22:03   作者:a771948524  
這篇文章主要介紹了PHP實(shí)現(xiàn)生成模糊圖片的方法,涉及php高斯算法實(shí)現(xiàn)圖片模糊功能相關(guān)實(shí)現(xià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) 超過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基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

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

相關(guān)文章

  • 修改Laravel5.3中的路由文件與路徑

    修改Laravel5.3中的路由文件與路徑

    本文先是回顧了Laravel5.2中路由的修改,然后給大家用實(shí)例代碼介紹了如何修改Laravel5.3中的路由,有需要的小伙伴們可以參考學(xué)習(xí)。
    2016-08-08
  • PHP簡單選擇排序(Simple Selection Sort)算法學(xué)習(xí)

    PHP簡單選擇排序(Simple Selection Sort)算法學(xué)習(xí)

    這篇文章主要為大家詳細(xì)介紹了PHP簡單選擇排序(Simple Selection Sort)算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • PHP和JS之間的數(shù)據(jù)交互并處理

    PHP和JS之間的數(shù)據(jù)交互并處理

    最近在開發(fā)Web應(yīng)用程序時(shí),經(jīng)常會涉及到PHP與JS之間的數(shù)據(jù)傳遞,特別是傳遞復(fù)雜的數(shù)據(jù)結(jié)構(gòu),如數(shù)組。本文主要介紹如何使用PHP傳遞數(shù)組給JS,并在JS中使用這些數(shù)據(jù)
    2023-04-04
  • php實(shí)現(xiàn)的返回?cái)?shù)據(jù)格式化類實(shí)例

    php實(shí)現(xiàn)的返回?cái)?shù)據(jù)格式化類實(shí)例

    這篇文章主要介紹了php實(shí)現(xiàn)的返回?cái)?shù)據(jù)格式化類及其應(yīng)用實(shí)例,包括針對XML、JSON等的格式化,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-09-09
  • php Notice: Undefined index 錯誤提示解決方法

    php Notice: Undefined index 錯誤提示解決方法

    字面意思就是未定義的索引,一般情況下是因?yàn)槌绦蜷_發(fā)作者判斷不嚴(yán)謹(jǐn)導(dǎo)致。一般不會影響程序的運(yùn)行,具體的解決方法可以參考下。
    2010-08-08
  • PHP 下載文件時(shí)自動添加bom頭的方法實(shí)例

    PHP 下載文件時(shí)自動添加bom頭的方法實(shí)例

    這篇文章主要介紹了PHP 下載文件時(shí)自動添加bom頭的方法實(shí)例,有需要的朋友可以參考一下
    2014-01-01
  • PHP編輯器PhpStrom運(yùn)行緩慢問題

    PHP編輯器PhpStrom運(yùn)行緩慢問題

    本文主要介紹了解決PHP編輯器PhpStrom運(yùn)行緩慢問題的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧
    2017-02-02
  • php基于Snoopy解析網(wǎng)頁html的方法

    php基于Snoopy解析網(wǎng)頁html的方法

    這篇文章主要介紹了php基于Snoopy解析網(wǎng)頁html的方法,實(shí)例分析了使用Snoopy類解析html文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • phplot生成圖片類用法詳解

    phplot生成圖片類用法詳解

    這篇文章主要介紹了phplot生成圖片類用法,較為詳細(xì)的分析了phplot生成圖片類常用函數(shù)的用法及生成圖片的完整實(shí)例,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • php $_SERVER["REQUEST_URI"]獲取值的通用解決方法

    php $_SERVER["REQUEST_URI"]獲取值的通用解決方法

    在 PHP 眾多預(yù)定義服務(wù)器變量中,$_SERVER["REQUEST_URI"] 算是經(jīng)常用到的,但是這個(gè)變量只有 apache 才支持,因此,我們需要一個(gè)更加通用的方式來獲取 REQUEST_URI 的值,本文就是結(jié)束這一問題的解決方案。
    2010-06-06

最新評論