本文实例讲述了PHP实现生成模糊图片的方法。分享给大家供大家参考,具体如下:
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) {
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 gaussian_blur("./1.jpg",null,null,3);
?>
页:
[1]