How does Google's Page Speed lossless image compression work?

后端 未结 9 639
Happy的楠姐
Happy的楠姐 2020-11-30 16:54

When you run Google\'s PageSpeed plugin for Firebug/Firefox on a website it will suggest cases where an image can be losslessly compressed, and provide a link to download th

9条回答
  •  旧时难觅i
    2020-11-30 16:58

    If you're really interested in the technical details, check out the source code:

    • png_optimizer.cc
    • jpeg_optimizer.cc
    • webp_optimizer.cc

    For PNG files, they use OptiPNG with some trial-and-error approach

    // we use these four combinations because different images seem to benefit from
    // different parameters and this combination of 4 seems to work best for a large
    // set of PNGs from the web.
    const PngCompressParams kPngCompressionParams[] = {
      PngCompressParams(PNG_ALL_FILTERS, Z_DEFAULT_STRATEGY),
      PngCompressParams(PNG_ALL_FILTERS, Z_FILTERED),
      PngCompressParams(PNG_FILTER_NONE, Z_DEFAULT_STRATEGY),
      PngCompressParams(PNG_FILTER_NONE, Z_FILTERED)
    };
    

    When all four combinations are applied, the smallest result is kept. Simple as that.

    (N.B.: The optipng command line tool does that too if you provide -o 2 through -o 7)


    For JPEG files, they use jpeglib with the following options:

     JpegCompressionOptions()
         : progressive(false), retain_color_profile(false),
           retain_exif_data(false), lossy(false) {}
    

    Similarly, WEBP is compressed using libwebp with these options:

      WebpConfiguration()
          : lossless(true), quality(100), method(3), target_size(0),
            alpha_compression(0), alpha_filtering(1), alpha_quality(100) {}
    

    There is also image_converter.cc which is used to losslessly convert to the smallest format.

提交回复
热议问题