14

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

<?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. image from //tutorial9.s3.amazonaws.com/uploads/2008/06/lens-blur.jpg

6 Answers 6

20

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.

e.g.:

for ($x=1; $x<=15; $x++)
   imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
3
  • 1
    I've used it myself, so I can assure you it works as expected. Dec 15, 2012 at 12:33
  • 11
    I have used this, but this is unbearably slow. Oct 19, 2013 at 3:10
  • 1
    it's slow but I still use it
    – CBeTJlu4ok
    Dec 9, 2013 at 18:12
13

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

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

you can find other convolution filters at: 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#97921 is a neat trick for getting the sum of the divisor.

check out http://php.net/manual/en/function.imageconvolution.php for more info on this function.

good ol' fashion blur is (1,2,1),(2,1,2),(1,2,1)

EDIT: as stated below you can run any filter more than once on the resulting output to also enhance the effect.

5
  • not near to what i want but thanks, any other way to blur it more ?? EDIT : sorry didn't read your edit, looking into the examples, thanks !
    – SAFAD
    Aug 30, 2011 at 15:41
  • 1
    seems like this matrix : array(2.0, 3.0, 2.0), array(3.0, 6.0, 3.0), array(2.0, 3.0, 2.0) is the biggest matrix this function can read, i've read that 5x5 matrices can do better and stronger blur, any ideas ?
    – SAFAD
    Aug 30, 2011 at 15:56
  • using the function several times gave the result i want, yet it is not the best solution out there for sure
    – SAFAD
    Aug 30, 2011 at 16:04
  • not sure why PHP wouldn't allow bigger matrices, might be a GD limitation I know IMagick takes 5x5's ... If you have adobe photoshop using filter>custom lets you play with convolution.
    – JKirchartz
    Aug 30, 2011 at 16:15
  • 1
    ImageMagick isn't a good option compared to GD,GD is almost installed in every server, unlike ImageMagick....Any options to evade the function repitition ??
    – SAFAD
    Aug 30, 2011 at 16:28
2

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

I have very good result with following code based on this solution:

    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.

See also: How to measure the speed of code written in PHP

0
1

Not sure if the imagefilter arguments help but check them out.

Alternatively, simply apply the image filter to it's result a couple of times???

0
1

For better speed use https://www.php.net/manual/tr/function.imagick-gaussianblurimage.php

Note: You need to install Imagick php extention (follow install instructions from here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.