2011-08-30 87 views
14

我想模糊与GD库图像,不幸的GD给人的GAUSSIAN_BLUR效果是不够的,我想要的东西更加blurrishPHP/GD:更好的高斯模糊

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

我想要的东西像这样或至少靠近它。 image from //tutorial9.s3.amazonaws.com/uploads/2008/06/lens-blur.jpg

回答

13

您可以尝试卷积:

$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是一个矩阵,所以数学是

[[1, 2, 1], 
[2, 4, 2], 
[1, 2, 1]] 

你可以找到其他的卷积过滤器:http://aishack.in/tutorials/image-convolution-examples/

imageconvolution(<image element>, <convolution matrix>, <divisor (sum of convolution matrix)>, <color offset>); 

所以从第e代码以上1+2+1+2+4+2+1+2+1 = 16矩阵的总和。 http://www.php.net/manual/en/function.imageconvolution.php#97921是得到除数的和的 的巧妙技巧。

检查出http://php.net/manual/en/function.imageconvolution.php欲了解更多关于此功能的信息。

好醇”时尚模糊是(1,2,1),(2,1,2),(1,2,1)

编辑: as stated below可以运行任何过滤超过一旦产生的输出也提高了效果。

+0

附近没有什么,我想,但感谢,任何其他方式更模糊呢??编辑:对不起,没有阅读你的编辑,查看例子,谢谢! – SAFAD

+1

似乎是这样的矩阵:数组(2.0,3.0,2.0),数组(3.0,6.0,3.0),数组(2.0,3.0,2.0)是这个函数可以读取的最大矩阵,我读过5x5矩阵可以做的更好更强的模糊,任何想法? – SAFAD

+0

使用函数多次给了我想要的结果,但它不是最好的解决方案*当然* – SAFAD

1

不确定imagefilter参数是否有帮助,但检查出来。

或者,只需将图像过滤器应用于其结果几次?

20

在遇到同样的问题后,我几次应用相同的过滤器,并且每次都应用到之前“imagefilter”调用的结果资源。我找到了你想要的'更模糊'的效果。

如:

for ($x=1; $x<=15; $x++) 
    imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR); 
+1

这应该是正确的答案 – younes0

+1

我已经使用它我自己,所以我可以向你保证它按预期工作。 –

+8

我已经使用过这个,但是这是难以忍受的缓慢。 –

1

试试这个:

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

我有一个基于this解决方案下面的代码很不错的成绩:

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); 
    } 

当你经过多次模糊申请顺利它提供了非常好的模糊效果。您可以在代码中使用以下数字:25, 10, -7

参见:How to measure the speed of code written in PHP