您好,登錄后才能下訂單哦!
這篇文章主要介紹了php實現(xiàn)圖片模糊的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
php實現(xiàn)圖片模糊的方法:首先創(chuàng)建一個PHP示例文件;然后定義一個“image_blur”類;接著創(chuàng)建“gaussian_blur”和“blur”方法;最后通過“$image_blur->gaussian_blur();”方法實現(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時 極其模糊 * @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); ?>
原圖效果:
生成模糊圖片后的效果:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享php實現(xiàn)圖片模糊的方法內(nèi)容對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,遇到問題就找億速云,詳細(xì)的解決方法等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。