PHP 4/5 Image Blur


It’s not something that people require very often, but occasionally the need does arise and you have to ask: how do I blur an image in PHP? Here is some PHP ImageBlur code that works on PHP4 and 5.

What we end up with is a small fast function that can take a source image and create something like the following:


No Blur

PHP5 ImageConvolution

No Blur

PHP4 myImageBlur

In PHP 5 you can just use ImageConvolution and the code example right out of the manual:

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

That’s a nice Gaussian blur in PHP5 for you. But what about your nasty old PHP4 servers? I have the answer. Actually the answer is probably “use ImageMagick and a system call”, but if you want a pure PHP/GD solution, read on.

Using a crafty mix of voodoo and ImageCopyMerge, you can create a PHP 4 blur effect that is virtually identical to the PHP5 method, as you can see by comparing the results of myImageBlur with ImageConvolution above. In tests on my local server, my method takes on average 0.43 seconds to blur a 1024*768 image, while ImageConvolution takes 0.40 – so it’s basically the same, and half a second to blur a desktop sized image is pretty decent – on a live server it’ll be even faster!

There’s some more comparisons and the source code after the break:

On the PHP ImageConvolution manual page, there’s a nice example of how it looks with text, so here’s a comparison of the two techniques:


ImageConvolution

myImageBlur

And here’s how it looks with a hedgehog :)


No Blur

PHP5 ImageConvolution

No Blur

PHP4 myImageBlur

That’s all there is to it really. Drop this function into your PHP library and off you go blurring images with PHP. You can use myImageBlur whether you’re on PHP4 or 5 and it will apply the best function; using ImageConvolution if available, else using my own voodoo-ninja PHP code. It runs pretty quickly too.

function myImageBlur($im) {
        // https://puremango.co.uk/2009/04/php-4-and-5-image-blur/
        if(function_exists('imageconvolution')) {
                $gaussian = array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0));
                imageconvolution($im, $gaussian, 16, 0);
        } else {
                // w00t. my very own blur function!
                $width = imagesx($im);
                $height = imagesy($im);

                // the higher, the more blurred (no impact on speed)
                // however, values>2 don't look very good. Sorry.
                $distance = 1;

                $temp_im = ImageCreateTrueColor($width,$height);
                ImageCopy($temp_im,$im,0,0,0,0,$width,$height);
                /*
                we *could* use this: 
                ImageCopy($temp_im,$im,0,0,0,$distance,$width,$height);
                instead, but that leads to an anomally at the top of the image.         
                */

                // blur by merging with itself at different x/y offsets:
                $pct = 70; // based on empirical tests, 70% gives the most blur.
                ImageCopyMerge($temp_im, $im, 0, 0, 0, $distance, $width-$distance, $height-$distance, $pct);
                ImageCopyMerge($im, $temp_im, 0, 0, $distance, 0, $width-$distance, $height, $pct);
                ImageCopyMerge($temp_im, $im, 0, $distance, 0, 0, $width, $height, $pct);
                ImageCopyMerge($im, $temp_im, $distance, 0, 0, 0, $width, $height, $pct);

                // remove temp image
                ImageDestroy($temp_im);
        }
}

// usage:
$im = ImageCreateFromPNG('/path/to/file.png');

myImageBlur($im);

header('Content-type: image/png');
ImagePng($im);
ImageDestroy($im);

Most of the code on puremango is GPL, but this function is 100% free (as in, no strings attached) , but I do appreciate links and blog posts :D


Related Posts:

, , , , , ,

  1. #1 by Drew Douglass on May 22, 2009 - 1:28 pm

    Hey,

    This is great stuff, I am a big fan of working with images and php, it’s pretty neat and creative how you pulled off the blur effect in php4 by offsetting the x/y axis. Nice work.

    One quick note, I think you could save some time and lines of code with php 5 blur by using the ‘imagefilter’ function and calling ‘IMG_FILTER_GAUSSIAN_BLUR’


    imagefilter($src_img, IMG_FILTER_GAUSSIAN_BLUR);

    I do think you get more precision and options using the convolution technique though. Again, wonderful article, you’ve got a new subscriber :)

    • #2 by user24 on May 22, 2009 - 10:55 pm

      Thanks for the suggestion – noted.

      But I think I’ll leave the code as it is at the moment, just to introduce people to the ImageConvolution function if nothing else.

      Glad to hear you enjoyed it :0)

      • #3 by Drew Douglass on May 23, 2009 - 1:23 am

        I think you should absolutely leave it there, it’s a great example of ImageConvolution. Keep up the great work!

  2. #4 by Michael Minter on July 29, 2010 - 8:33 pm

    What’s the direction I need to take to make the image in question less blurry?

  3. #5 by Thor on February 4, 2011 - 3:55 pm

    Theres a way to move the blur radious? (more blur or less blur)?

Comments are closed.