php - 模糊图像的最佳方式

时间:2017-03-13 08:23:06

标签: 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);

The Images

4 个答案:

答案 0 :(得分:8)

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

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);

我借用了缩小尺寸的想法和some of the code form this answer

我已经使用您的图片测试了解决方案,结果是:

enter image description here

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

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

答案 1 :(得分:2)

那时我在项目中也遇到了这个问题,我使用了一些长代码,但是我认为该代码不合适并创建了自己的小代码,这就是该代码

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);

答案 2 :(得分:0)

简单

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);

答案 3 :(得分:0)

关于 Michael K 回答中 IMG_FILTER_SMOOTH 步骤的说明:

imagefilter($sm, IMG_FILTER_SMOOTH,99);

根据PHP:imagefilter

<块引用>

IMG_FILTER_SMOOTH 应用 9 单元卷积矩阵,其中中心像素的权重为 arg1,其他权重为 1.0。通过将总和除以 arg1 + 8.0(矩阵的总和)对结果进行归一化。 接受任何浮点数,大值(实际上:2048 或更多)= 没有变化

因此,imagefilter() 中的 arg1 对 3x3 内核的中心像素相对于其他像素进行加权。值 99 不会改变任何内容,可以跳过。对于更多的模糊,你想使用一个小的 arg1(低于 8)。