使用PHP(JPEG)优化上传的图像

18
当在Google Chrome中运行Page Speed时,建议优化/压缩图像。这些图像大多由用户上传,因此我需要在上传过程中对它们进行优化。关于使用PHP优化JPEG图像的内容,我所了解到的是使用以下GD函数:
getimagesize()
imagecreatefromjpeg()
imagejpeg()

我在上传后调整图像大小,所以已经通过这些函数拉取了图像,并且在使用imagecreatefromjpeg()调整大小后,还会使用imagecopyresampled()

但是,Page Speed仍然告诉我这些图像可以优化。在php脚本中如何实现优化?在imagejpeg()中降低质量也没有效果。

4个回答

22

在imagejpeg函数中,您可以指定图像的质量。如果您已经将其设置为适当的值,则几乎无法做其他事情。

页面速度可能会认为所有大小超过某个特定值的图像都需要“压缩”,因此请确保它们在高度/宽度方面尽可能小且压缩过。

您可以在pagespeed文档http://code.google.com/speed/page-speed/docs/payload.html#CompressImages中了解有关页面速度及其压缩建议的更多信息,其中描述了一些压缩适当的技术/工具。

我还读到了以下内容:

有几个可用的工具可以对JPEG和PNG文件进行进一步的无损压缩,而不影响图像质量。对于JPEG,我们推荐在Linux上运行带有--strip-all选项的jpegtranjpegoptim。对于PNG,我们推荐OptiPNGPNGOUT

因此,如果您确实想坚持使用Google的建议,也许可以使用PHP的exec来在上传文件时对文件运行这些工具之一。


要使用PHP进行压缩,您可以执行以下操作(听起来您已经在执行此操作):

其中$source_url是图像地址,$destination_url是保存位置,$quality是一个介于1到100之间的数字,选择使用多少JPEG压缩。

function compressImage($source_url, $destination_url, $quality) {
    $info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg') $image = imagecreatefromjpeg($source_url);
    elseif ($info['mime'] == 'image/gif') $image = imagecreatefromgif($source_url);
    elseif ($info['mime'] == 'image/png') $image = imagecreatefrompng($source_url);

    //save file
    imagejpeg($image, $destination_url, $quality);

    //return destination file
    return $destination_url;
}

好的,谢谢。我想现在这样就可以了。我已经按照你所描述的做了。在我的情况下,使用jpegtran等进一步压缩是不必要的。 - Juvlius

8

修复的功能:

function compressImage($source_url, $destination_url, $quality) {

    //$quality :: 0 - 100

    if( $destination_url == NULL || $destination_url == "" ) $destination_url = $source_url;

    $info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg' || $info['mime'] == 'image/jpg')
    {
        $image = imagecreatefromjpeg($source_url);
        //save file
        //ranges from 0 (worst quality, smaller file) to 100 (best quality, biggest file). The default is the default IJG quality value (about 75).
        imagejpeg($image, $destination_url, $quality);

        //Free up memory
        imagedestroy($image);
    }
    elseif ($info['mime'] == 'image/png')
    {
        $image = imagecreatefrompng($source_url);

        imageAlphaBlending($image, true);
        imageSaveAlpha($image, true);

        /* chang to png quality */
        $png_quality = 9 - round(($quality / 100 ) * 9 );
        imagePng($image, $destination_url, $png_quality);//Compression level: from 0 (no compression) to 9(full compression).
        //Free up memory
        imagedestroy($image);
    }else
        return FALSE;

    return $destination_url;

}

2
您可以使用Imagick类来实现此功能。考虑以下封装函数:
<?php
    function resizeImage($imagePath, $width, $height, $blur, $filterType = Imagick::FILTER_LANCZOS, $bestFit = false)
    {
        //The blur factor where &gt; 1 is blurry, &lt; 1 is sharp.
        $img= new \Imagick(realpath($imagePath));
        $img->setCompression(Imagick::COMPRESSION_JPEG); 
        $img->setCompressionQuality(40);
        $img->stripImage();
        $img->resizeImage($width, $height, $filterType, $blur, $bestFit);
        $img->writeImage();
    }

?>

想了解如何使用Imagick调整图像大小,可以参考以下链接:
http://php.net/manual/zh/class.imagick.php
http://php.net/manual/zh/imagick.resizeimage.php http://php.net/manual/zh/imagick.constants.php#imagick.constants.filters


-2

优化图像非常重要。许多CMS平台都有模块或插件来执行此过程。但是,如果您自己编程,则可以在此页面找到完整的php教程https://a1websitepro.com/optimize-images-with-php-in-a-directory-on-your-server/,您将学习如何实现imagecreatefromjpeg($SrcImage);imagecreatefrompng($SrcImage);以及imagecreatefromgif($SrcImage);。该页面上有书面和视频说明。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接