Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - Best way to blur images

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

like image 559
danial dezfooli Avatar asked Mar 13 '17 08:03

danial dezfooli


People also ask

How to make image blur in PHP?

PHP | Imagick blurImage() function The Imagick::blurImage() function is an inbuilt function in PHP which is used to add blur filter to the image. This function returns True on success.

How do you blur an image?

Quick tip: You can adjust the blur after the photo is taken as well. If you take a picture in Portrait mode, open it in the Photos app, tap Edit, and then tap the f button at the top left. Use the slider to change the blur effect.

How to increase the amount of Blur in an image?

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.

What are the advantages of Blur loop over original image?

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. Show activity on this post. 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

Is there a way to colorize images with PHP?

It turns out PHP's Colorize is the equivalent of Photoshop's "Linear Dodge" layer filter. Hue adjustments have always worked well for me, so I figured I could try with PHP. This function is kind of slow on larger images, but on small images like what I'm using it for, the difference is trivial.

How to get Gaussian blur on a resized 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: 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.


2 Answers

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.

like image 171
Michael Krikorev Avatar answered Oct 19 '22 20:10

Michael Krikorev


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);
like image 3
Mehul Velani Avatar answered Oct 19 '22 19:10

Mehul Velani