2011-08-30 81 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