【问题标题】:Optimize uploaded images with php (jpeg)使用 php (jpeg) 优化上传的图片
【发布时间】:2012-12-13 21:25:33
【问题描述】:

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

getimagesize()
imagecreatefromjpeg()
imagejpeg()

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

但是,Page Speed 仍然告诉我这些图像可以优化。如何在 php 脚本中完成此优化?在 imagejpeg() 中设置较低的质量也没有什么区别。

【问题讨论】:

    标签: php gd jpeg image-compression image-optimization


    【解决方案1】:

    imagejpeg 函数是您指定质量的地方。如果您已经将其设置为适当的值,那么您几乎无能为力。

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

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

    我也刚刚阅读了以下内容:

    有多种工具可以对 JPEG 和 PNG 文件进行进一步的无损压缩,而不会影响图像质量。对于 JPEG,我们建议使用 jpegtranjpegoptim(仅在 Linux 上可用;使用 --strip-all 选项运行)。对于 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 等进行进一步压缩
    【解决方案2】:

    修复功能:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-24
          • 2011-08-17
          • 2010-09-26
          • 2011-03-17
          • 2014-07-11
          • 2013-01-29
          相关资源
          最近更新 更多