Blur an image in PHP

Blur an image in PHP

Demo

Sometimes you may needed to blur an image or parts of an image. PHP has several methods to do this which all yield different results.This article shows the desired output using imagefilter method.
imagefilter
— Applies a filter to an image

imagefilter() applies the given filter filtertype on the image.

Try to scale down the image and apply the loop with gaussian blur on the resized image. That way you will save some performance on large images:

Get original image size

Create array with width and height of down sized images

Scale by 25% and apply Gaussian blur

Scale result by 200% and blur again

Scale result back to original size
imagecopyresampled($image, $md, 0, 0, 0, 0, $w, $h, $size[‘md’][‘w’], $size[‘md’][‘h’]);
imagedestroy($md);

the result:

Demo

You can elaborate and increase/decrease the blur by changing the number of loops for the small image. If you change for ($x=1; $x <=40; $x++){ to for ($x=1; $x <=75; $x++){ you will get more blur. The downside to this solution is that you will get som visible pixelation because of the resizing going on. The upside is better performance, less server load and execution time compared to if you would apply the blur loop on the original sized image.

You may also like

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *