4

I've been looking for PHP code to apply a Gaussian blur to images.

What I've done was like this:

<?php
$image = imagecreatefromjpeg('new.jpg'); 
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
imagejpeg($image, 'blur.jpeg');
imagedestroy($image);
?>

However the effect is very weak, and if I repeat the blur effect, it takes a very long time to process and the end result is still not that good.

I also used Timthumb , I always liked its simplicity, but it crops the image by default and its blurring effect is very weak.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ahmed Moness
  • 237
  • 2
  • 4
  • 11
  • 1
    Try the answer here: http://stackoverflow.com/questions/7245710/php-gd-better-gaussian-blur – Erik Nedwidek Jan 20 '13 at 19:20
  • Command-line or PHP module _ImageMagick_ can do this - any decent algorithm should be able to take a blur value. Did you do a search for this, and if so what did it turn up? – halfer Jan 20 '13 at 19:24
  • @ErikNedwidek , it feels a bit complicated to make this calcualtions as I want to connect the blur value to a slider so the user can easily change the blur effect. Also it needs the same strategy .. repeat the code to get a stronger effect – Ahmed Moness Jan 20 '13 at 19:30
  • @halfer I think ImageMagick is not present on all servers, so that can be a source of pain – Ahmed Moness Jan 20 '13 at 19:32

3 Answers3

7

You can use ImageMagic

Original Image

enter image description here

Run via exec

convert a.png -blur 0x3 a_blur.png

Output

OR Run

convert a.png -blur 0x8 a_blur.png

enter image description here

Baba
  • 94,024
  • 28
  • 166
  • 217
  • 1
    wow ! the effect looks great ! after some searching, I found that it's present also as a plugin for Wordpress ! I guess I will take a closer look at it ..Thank u – Ahmed Moness Jan 20 '13 at 20:04
7

It is possible also without ImageMagic lib;

header('Content-Type: image/png');

$blurs = 10;
$image = imagecreatefrompng('blur.png');
for ($i = 0; $i < $blurs; $i++) {
    imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
}
imagepng($image, 'blur10.png');
imagedestroy($image);

After 10 blur applied;

enter image description here

Kerem
  • 11,377
  • 5
  • 59
  • 58
  • 2
    Thank u, but this method takes a longer time to be done & increasing the blur value to higher degrees makes it distorted, not well blurred like Baba's third example. – Ahmed Moness Jan 21 '13 at 11:40
  • @kerem: I used ur code and it work nice..but I am unable to save it in image folder.could u plz provide code to save generated blur code? – bittu Jan 26 '16 at 08:51
  • got solution to my query.I used imagepng("image name","target"); to save image. – bittu Jan 26 '16 at 13:29
0

Image optimization is very heavy process so personally if i have these kind of task in PHP then i use this PHP Image Library Called PhpThumb it can create blur images without any code you just need to call it's script via url and provide parameters according to its docs check it's demo.

Touqeer Shafi
  • 5,084
  • 3
  • 28
  • 45