SAFAD SAFAD - 1 year ago 300
PHP Question

PHP/GD : Better Gaussian blur

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');
echo 'fail';


I want something like this :
or at least near it.

Answer Source

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:

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. is a neat trick for getting the sum of the divisor.

check out 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.