php - 模糊图像的最佳方式

dan*_*oli 4 php imagefilter

我试图模糊第一个像第三个图像,但我不能这样做!:(

 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);
Run Code Online (Sandbox Code Playgroud)

图像

Mic*_*l K 11

尝试缩小图像并在调整大小的图像上应用高斯模糊的循环.这样你就可以在大图像上保存一些性能:

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);
Run Code Online (Sandbox Code Playgroud)

我借用了缩小尺寸的想法,一些代码构成了这个答案

我用您的图像测试了解决方案,结果如下:

在此输入图像描述

您可以通过更改小图像的循环次数来详细说明并增加/减少模糊.如果你改变for ($x=1; $x <=40; $x++){for ($x=1; $x <=75; $x++){你会得到更多的模糊.

此解决方案的缺点是,由于调整大小,您将获得可见的像素化.与在原始大小的图像上应用模糊循环相比,优势在于更好的性能,更少的服务器负载和执行时间.