PHP/GD:更好的高斯模糊
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7245710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP/GD : Better Gaussian blur
提问by SAFAD
I want to blur an image with GD library, unfortunately the GAUSSIAN_BLUR effect that GD gives isn't enough and i want something being more blurrish
我想用 GD 库模糊图像,不幸的是 GD 提供的 GAUSSIAN_BLUR 效果还不够,我想要一些更模糊的东西
<?php $im = imagecreatefrompng($_GET['image']);
if($im && imagefilter($im, IMG_FILTER_GAUSSIAN_BLUR))
{
header('Content-Type: image/png');
imagepng($im);
}
else
{
echo 'fail';
}
imagedestroy($im);
I want something like this or at least near it.
我想要这样的东西,或者至少接近它。
采纳答案by JKirchartz
You can try convolution:
你可以试试卷积:
$gaussian = array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0));
imageconvolution($image, $gaussian, 16, 0);
$gaussian
is a matrix, so mathematically it's
$gaussian
是一个矩阵,所以数学上它是
[[1, 2, 1],
[2, 4, 2],
[1, 2, 1]]
you can find other convolution filters at: http://aishack.in/tutorials/image-convolution-examples/
您可以在以下位置找到其他卷积滤波器:http: //aishack.in/tutorials/image-convolution-examples/
imageconvolution( <image element>, <convolution matrix>, <divisor (sum of convolution matrix)>, <color offset>);
so from the code above 1+2+1+2+4+2+1+2+1 = 16
the sum of the matrix. http://www.php.net/manual/en/function.imageconvolution.php#97921is a neat trick for getting the
sum of the divisor.
所以从上面的代码1+2+1+2+4+2+1+2+1 = 16
得到矩阵的总和。http://www.php.net/manual/en/function.imageconvolution.php#97921是获得除数总和的巧妙技巧。
check out http://php.net/manual/en/function.imageconvolution.phpfor more info on this function.
查看http://php.net/manual/en/function.imageconvolution.php了解有关此功能的更多信息。
good ol' fashion blur is (1,2,1),(2,1,2),(1,2,1)
好的时尚模糊是 (1,2,1),(2,1,2),(1,2,1)
EDIT:as stated belowyou can run any filter more than once on the resulting output to also enhance the effect.
编辑:如下所述,您可以对结果输出多次运行任何过滤器,以增强效果。
回答by Andrei Oniga
After coming across the same problem, I applied the same filter a few times, and each time to the resulting resource of the previous "imagefilter" call. I got the 'more blurry' effect you're looking for.
遇到同样的问题后,我应用了几次相同的过滤器,每次都应用到上一次“imagefilter”调用的结果资源上。我得到了你正在寻找的“更模糊”的效果。
e.g.:
例如:
for ($x=1; $x<=15; $x++)
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
回答by Jasom Dotnet
I have very good result with following code based on thissolution:
基于此解决方案,我使用以下代码获得了非常好的结果:
for ($i = 0; $i < 25; $i++) {
if ($i % 10 == 0) {//each 10th time apply 'IMG_FILTER_SMOOTH' with 'level of smoothness' set to -7
imagefilter($tmp_dst_image, IMG_FILTER_SMOOTH, -7);
}
imagefilter($tmp_dst_image, IMG_FILTER_GAUSSIAN_BLUR);
}
When you apply smooth after several blurs it delivers very good blurry effect. You can experiment with the following number in the code: 25, 10, -7
.
当您在几次模糊后应用平滑时,它会提供非常好的模糊效果。您可以在代码中使用以下数字进行试验:25, 10, -7
.
See also:How to measure the speed of code written in PHP
另请参阅:如何衡量用 PHP 编写的代码的速度
回答by hakiko
For better speed use https://www.php.net/manual/tr/function.imagick-gaussianblurimage.php
为了获得更好的速度,请使用https://www.php.net/manual/tr/function.imagick-gaussianblurimage.php
Note: You need to install Imagick
php extention (follow install instructions from here
注意:您需要安装Imagick
php extension(按照此处的安装说明进行操作)
example
例子
header('Content-type: image/jpeg');
$image = new Imagick('https://i.dr.com.tr/cache/600x600-0/originals/0000000671636-1.jpg');
// Use gaussianBlurImage function
$image->gaussianBlurImage(7, 5);
// Display the output image
echo $image;
回答by Brian
Not sure if the imagefilter arguments help but check them out.
不确定 imagefilter 参数是否有帮助,但请查看它们。
Alternatively, simply apply the image filter to it's result a couple of times???
或者,只需将图像过滤器应用于其结果几次???
回答by hero-in.com
try this:
尝试这个:
<?php
//
//fastblur function from image hosting and processing site http://hero-in.com
//
function blur($img, $radius=10)
{
if ($radius>100) $radius=100; //max radius
if ($radius<0) $radius=0; //nin radius
$radius=$radius*4;
$alphaStep=round(100/$radius)*1.7;
$width=imagesx($img);
$height=imagesy($img);
$beginX=floor($radius/2);
$beginY=floor($radius/2);
//make clean imahe sample for multiply
$cleanImageSample=imagecreatetruecolor($width, $height);
imagecopy($cleanImageSample, $img, 0, 0, 0, 0, $width, $height);
//make h blur
for($i = 1; $i < $radius+1; $i++)
{
$xPoint=($beginX*-1)+$i-1;
imagecopymerge($img, $cleanImageSample, $xPoint, 0, 0, 0, $width, $height, $alphaStep);
}
//make v blur
imagecopy($cleanImageSample, $img, 0, 0, 0, 0, $width, $height);
for($i = 1; $i < $radius+1; $i++)
{
$yPoint=($beginY*-1)+$i-1;
imagecopymerge($img, $cleanImageSample, 0, $yPoint, 0, 0, $width, $height, $alphaStep);
}
//finish
return $img;
imagedestroy($cleanImageSample);
}
//example
$im = ImageCreateFromJpeg('image.jpg');
$im = blur($im,10);
imagejpeg($im)
imagedestroy($im);
?>