使用php (jpeg)优化上传的图像


Optimize uploaded images with php (jpeg)

在Google Chrome中运行Page Speed时,它建议优化/压缩图像。这些图片大多是用户上传的,所以我需要在上传的时候进行优化。我发现使用php优化jpeg图像类似于使用以下GD函数:

getimagesize()
imagecreatefromjpeg()
imagejpeg()

因为我在上传后调整图像的大小,我已经通过这些函数拉图像,此外,我在imagecreatefromjpeg()之后使用imagecopyresampled()来调整它的大小。

但是,页面速度仍然告诉我这些图像可以优化。如何在php脚本中完成此优化?在imagejpeg()中设置较低的质量也没有影响。

imagejpeg函数是您分配质量的地方。如果你已经把它设置为一个合适的值,那么你就没有什么别的办法了。

页面速度可能认为所有超过一定大小的图像都"需要压缩",也许只是确保它们都尽可能小(在高度/宽度方面)并压缩。

你可以在pagspeed docs http://code.google.com/speed/page-speed/docs/payload.html#CompressImages找到更多关于页面速度和压缩建议,其中描述了一些适当压缩的技术/工具。

我也刚刚读了以下内容:

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

所以也许(如果你真的想坚持谷歌的建议),你可以使用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;
}

修复功能:

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

您可以使用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/en/class.imagick.php
http://php.net/manual/en/imagick.resizeimage.phphttp://php.net/manual/en/imagick.constants.php imagick.constants.filters

优化你的图片是非常重要的。有几个CMS平台有模块或插件来完成这个过程。然而,如果你是自己编程,有一个完整的php教程位于这个页面https://a1websitepro.com/optimize-images-with-php-in-a-directory-on-your-server/你将展示如何实现imagecreatefromjpeg($SrcImage);imagecreatefrompng($SrcImage);imagecreatefromgif($SrcImage);有书面和视频指导在页面上。