PHP如何模糊圖片案例

來源:互聯網
上載者:User
本篇文章主要介紹PHP如何?產生模糊圖片,感興趣的朋友參考下,希望對大家有所協助。

代碼如下:

<?phpclass 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);?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.