1 Star 0 Fork 0

ica / ps_0

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
GAUSSIAN_BLUR.php 2.78 KB
一键复制 编辑 原始数据 按行查看 历史
Moe-boshi 提交于 2018-07-11 17:04 . 20180711 -1
<?php
/**
* Created by PhpStorm.
* User: clkj1
* Date: 2018/7/11
* Time: 15:14
*/
//I needed an especially strong blur effect today and had a hard time achieving adequate results with the built-in IMG_FILTER_GAUSSIAN_BLUR filter. In order to achieve the strength of the blur I required I had to repeat the filter up to 100 times, which took way too long to be acceptable.
//
//After a bit of searching, I found this answer to be quite a good solution to this problem: http://stackoverflow.com/a/20264482
//
//Based on that technique, I wrote the following generic function to achieve a very strong blur in a reasonable amount of processing:
/**
* Strong Blur
*
* @param resource $gdImageResource
* @param int $blurFactor optional
* This is the strength of the blur
* 0 = no blur, 3 = default, anything over 5 is extremely blurred
* @return GD image resource
* @author Martijn Frazer, idea based on http://stackoverflow.com/a/20264482
*/
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;
}
$image = imagecreatefromjpeg('realPs/images/Screenshot3.jpg');
$image = blur($image, 1);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/ica520/ps_0.git
git@gitee.com:ica520/ps_0.git
ica520
ps_0
ps_0
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891