PHP/GD : Better Gaussian blur
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.
PHP/GD Gaussian Blur Effect
You can copy a specific part of the image into a new image, apply the blur on the new image and copy the result back.
Sort of like this:
$image2 = imagecreate($width, $height);
imagecopy ( $image2 , $image , 0 , 0 , $x , $y , $width , $height);
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
imagecopy ($image, $image2, $x, $y, 0, 0, $width, $height);
How to achieve a blur effect in PHP?
You can use ImageMagic
Original Image
Run via exec
convert a.png -blur 0x3 a_blur.png
OR Run
convert a.png -blur 0x8 a_blur.png
php - Best way to blur images
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:
header('Content-Type: image/jpeg');
$file = '1.jpg';
$image = imagecreatefromjpeg($file);
/* Get original image size */
list($w, $h) = getimagesize($file);
/* Create array with width and height of down sized images */
$size = array('sm'=>array('w'=>intval($w/4), 'h'=>intval($h/4)),
'md'=>array('w'=>intval($w/2), 'h'=>intval($h/2))
);
/* Scale by 25% and apply Gaussian blur */
$sm = imagecreatetruecolor($size['sm']['w'],$size['sm']['h']);
imagecopyresampled($sm, $image, 0, 0, 0, 0, $size['sm']['w'], $size['sm']['h'], $w, $h);
for ($x=1; $x <=40; $x++){
imagefilter($sm, IMG_FILTER_GAUSSIAN_BLUR, 999);
}
imagefilter($sm, IMG_FILTER_SMOOTH,99);
imagefilter($sm, IMG_FILTER_BRIGHTNESS, 10);
/* Scale result by 200% and blur again */
$md = imagecreatetruecolor($size['md']['w'], $size['md']['h']);
imagecopyresampled($md, $sm, 0, 0, 0, 0, $size['md']['w'], $size['md']['h'], $size['sm']['w'], $size['sm']['h']);
imagedestroy($sm);
for ($x=1; $x <=25; $x++){
imagefilter($md, IMG_FILTER_GAUSSIAN_BLUR, 999);
}
imagefilter($md, IMG_FILTER_SMOOTH,99);
imagefilter($md, IMG_FILTER_BRIGHTNESS, 10);
/* Scale result back to original size */
imagecopyresampled($image, $md, 0, 0, 0, 0, $w, $h, $size['md']['w'], $size['md']['h']);
imagedestroy($md);
// Apply filters of upsized image if you wish, but probably not needed
//imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
//imagefilter($image, IMG_FILTER_SMOOTH,99);
//imagefilter($image, IMG_FILTER_BRIGHTNESS, 10);
imagejpeg($image);
imagedestroy($image);
I have borrowed the downsizing idea and some of the code form this answer
I have tested the solution with your image, and the result:
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.
How do you programmatically add soft focus to an image
The go-to library (for PHP, Python …) is ImageMagick. Among other, this allows blurring.
Adapting the basic usage example:
<?php
header('Content-type: image/jpeg');
$image = new Imagick('image.jpg');
$image->blurImage(5, 3);
echo $image;
?>
Related Topics
Execute a PHP Script from Another PHP Script
Adding Multiple Items to Woocommerce Cart at Once
PHP Get the Last 3 Elements of an Associative Array While Preserving the Keys
PHP Date Time Current Time Add Minutes
Fatal Error: Call to a Member Function Query() on Null
Replacing Variables in a String
How to Prefix a Positive Number with Plus Sign in PHP
How to Use Two Submit Buttons, and Differentiate Between Which One Was Used to Submit the Form
Returning First X Items from Array
Www to Non-Www Redirect with PHP
How to Check for a Specific Type of Object in PHP
Base Table or View Not Found: 1146 Table Laravel 5