Posts Tagged ImageCopyMerge

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:
Read the rest of this entry »

, , , , , ,

6 Comments