8

I'm trying to blur first image like third one,But I cant do it ! :(

 header('Content-Type: image/jpeg');
$image = imagecreatefromjpeg('1.jpg');
for ($x=1; $x<=40; $x++){
    imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR,999);
} 
    imagefilter($image, IMG_FILTER_SMOOTH,99);
    imagefilter($image, IMG_FILTER_BRIGHTNESS, 10);
imagejpeg($image);
imagedestroy($image);

The Images

danial dezfooli
  • 788
  • 2
  • 9
  • 27

5 Answers5

17

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:

enter image description here

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.

Community
  • 1
  • 1
Michael Krikorev
  • 2,126
  • 1
  • 18
  • 25
3

i also faced this problem in my project that time i use some long code but i think that, that code is not proper and create my own small code, here is that code

header("content-type: image/png");
$image = imagecreatefromjpeg("abc.jpg");
for ($x=1; $x<=50; $x++)
{
 imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
}
imagepng($image);
imagedestroy($image);
Mehul Velani
  • 691
  • 9
  • 13
0

A note on the IMG_FILTER_SMOOTH steps in Michael K's answer:

imagefilter($sm, IMG_FILTER_SMOOTH,99);

According to PHP:imagefilter

IMG_FILTER_SMOOTH Applies a 9-cell convolution matrix where center pixel has the weight arg1 and others weight of 1.0. The result is normalized by dividing the sum with arg1 + 8.0 (sum of the matrix). any float is accepted, large value (in practice: 2048 or more) = no change

So the arg1 in imagefilter() weighs the center pixel of the 3x3-kernel in relation to the other pixels. A value of 99 will not change anything and can be skipped. For more blur, you want to use a small arg1 (below 8).

0

I had similar issue coding with Laravel, I used following solution:

$image = \Image::make($path);
$width = $image->getWidth();
$height = $image->getHeight();
$image->resize(round($width / 5), round($height / 5));
$image->blur(10);
$image->resize($width, $height);
$image->blur(70);
$image->save($path, 60, 'jpg');

Laravel uses http://image.intervention.io/, it can be used standalone.

You can change how much times to resize the image, how much blur to apply on small and enlarged image afterwards.

Vedmant
  • 2,265
  • 1
  • 27
  • 36
-2

easy

header('Content-Type: image/jpeg');
$image = imagecreatefromjpeg('1.jpg');

$image = imagescale($image , $YOU_IMG_WIDTH/40, $YOU_IMG_WIDTH/40);
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR);
$image = imagescale($image , $YOU_IMG_WIDTH, $YOU_IMG_HEIGHT);

imagejpeg($image);
imagedestroy($image);