2011-08-30 6 views
14
내가 GD 라이브러리 이미지를 흐리게 할

는 GD가 제공 불행히도 GAUSSIAN_BLUR 효과는 충분하지 않습니다와 나는 더 blurrish이PHP/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

당신은 회선 시도 할 수 있도록 http://aishack.in/tutorials/image-convolution-examples/

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

: 당신이에 다른 회선 필터를 찾을 수 있습니다

$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]] 

입니다 일부터 위의 코드는 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이 기능에 대한 추가 정보

좋은 똑똑한 '패션 흐림, (2,1,2) (1,2,1)이다 (1,2,1)

편집 : 당신이 이상의 어떤 필터를 실행할 수 있습니다 as stated below 한 번 결과 출력에 효과를 향상시킵니다.

+0

하지 가까운 내가 원하는하지만 덕분에, 다른 어떤 방법보다 그것을 흐리게 무엇? 편집 : 미안, 예제를보고, 당신의 편집을 읽지 못했습니다 감사합니다! – SAFAD

+1

이이 행렬처럼 보입니다 : array (2.0, 3.0, 2.0), array (3.0, 6.0, 3.0), 배열 (2.0, 3.0, 2.0)은이 함수가 읽을 수있는 가장 큰 행렬입니다. 더 좋고 더 강한 흐림, 어떤 생각? – 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