Google 的 Page Speed 无损图像压缩如何工作?

发布于 2024-10-27 00:11:58 字数 1778 浏览 0 评论 0 原文

当您在网站上运行适用于 Firebug/Firefox 的 Google PageSpeed 插件时,它会建议可以无损压缩图像的情况,并提供下载此较小图像的链接。

例如:

这适用于 JPG 和 PNG 文件类型(我还没有测试过 GIF 或其他文件类型。)

还要注意 Flickr 缩略图(所有这些图像都是 75x75 像素。)它们是一些相当大的节省。如果这真的那么好,雅虎为什么不将此服务器端应用到他们的整个图书馆并减少他们的存储和带宽负载?

即使 Stackoverflow.com 也能带来一些非常小的节省:

我发现 PageSpeed 对我使用 Photoshop 的“另存为 Web”功能创建的 PNG 文件提出了相当可观的节省。

所以我的问题是,他们对图像做了什么改变才能将图像减少这么多?我猜不同的文件类型有不同的答案。这对于 JPG 来说真的是无损的吗?他们如何击败 Photoshop?我应该对此有点怀疑吗?

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 this smaller image.

For example:

This applies across both JPG and PNG filetypes (I haven't tested GIF or others.)

Note too the Flickr thumbnails (all those images are 75x75 pixels.) They're some pretty big savings. If this is really so great, why aren't Yahoo applying this server-side to their entire library and reducing their storage and bandwidth loads?

Even Stackoverflow.com stands for some very minor savings:

I've seen PageSpeed suggest pretty decent savings on PNG files that I created using Photoshop's 'Save for Web' feature.

So my question is, what changes are they making to the images to reduce them by so much? I'm guessing there are different answers for different filetypes. Is this really lossless for JPGs? And how can they beat Photoshop? Should I be a little suspicious of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

清眉祭 2024-11-03 00:11:59

如果您确实对技术细节感兴趣,请查看源代码:


对于 PNG 文件,他们使用 OptiPNG 并进行一些试错方法

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

当应用所有四种组合时,保留最小的结果。就这么简单。

(注意:如果您提供 -o 2-o 7optipng 命令行工具也会执行此操作)


对于 JPEG 文件,它们使用 < a href="http://code.google.com/p/jpeglib/source/browse/" rel="noreferrer">jpeglib 具有以下选项:

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

同样,WEBP 使用 libwebp 具有这些选项:

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

还有 image_converter.cc 用于无损转换为最小尺寸格式。

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


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.

软糯酥胸 2024-11-03 00:11:59

我使用 jpegoptim 来优化 JPG 文件和 optipng 优化 PNG 文件。

如果您使用的是 bash,则无损优化目录中所有 JPG(递归)的命令是:

find /path/to/jpgs/ -type f -name "*.jpg" -exec jpegoptim --strip-all {} \;

您可以将 -m[%] 添加到 jpegoptim< /code> 有损压缩 JPG 图片,例如:

 find /path/to/jpgs/ -type f -name "*.jpg" -exec jpegoptim -m70 --strip-all {} \;

要优化目录中的所有 PNG:

find /path/to/pngs/ -type f -name "*.png" -exec optipng -o2 {} \;

-o2 是默认优化级别,您可以将其从 o2 更改为 <代码>o7。请注意,更高的优化级别意味着更长的处理时间。

I use jpegoptim to optimize JPG files and optipng to optimize PNG files.

If you're on bash, the command to losslessly optimize all JPGs in a directory (recursively) is:

find /path/to/jpgs/ -type f -name "*.jpg" -exec jpegoptim --strip-all {} \;

You can add -m[%] to jpegoptim to lossy compress JPG images, for example:

 find /path/to/jpgs/ -type f -name "*.jpg" -exec jpegoptim -m70 --strip-all {} \;

To optimize all PNGs in a directory:

find /path/to/pngs/ -type f -name "*.png" -exec optipng -o2 {} \;

-o2 is the default optimization level, you can change this from o2 to o7. Notice that higher optimization level means longer processing time.

回忆追雨的时光 2024-11-03 00:11:59

这是用编码器的 CPU 时间换取压缩效率的问题。压缩是对较短表示形式的搜索,如果您更加努力地搜索,您会发现较短的表示形式。

还有一个充分利用图像格式功能的问题,例如 PNG8+a 而不是 PNG24+a、JPEG 中优化的霍夫曼表等。Photoshop

在为 Web 保存图像时并没有真正努力做到这一点,因此任何工具都可以击败它,这并不奇怪。

请参阅

It's a matter of trading encoder's CPU time for compression efficiency. Compression is a search for shorter representations, and if you search harder, you'll find shorter ones.

There is also a matter of using image format capabilities to the fullest, e.g. PNG8+a instead of PNG24+a, optimized Huffman tables in JPEG, etc.

Photoshop doesn't really try hard to do that when saving images for the web, so it's not surprising that any tool beats it.

See

メ斷腸人バ 2024-11-03 00:11:59

在 Windows 中复制 PageSpeed 的 JPG 压缩结果

我能够使用 Windows 版本的 jpegtran 获得与 PageSpeed 完全相同的压缩结果,您可以在 www.jpegclub.org/jpegtran。我使用 DOS 提示符运行可执行文件(使用“开始”>“CMD”)。为了获得与 PageSpeed 压缩完全相同的文件大小(精确到字节),我指定了 Huffman 优化,如下所示:

jpegtran -optimize source_filename.jpg output_filename.jpg

有关压缩选项的更多帮助,只需在命令提示符下键入: jpegtran

或者使用自动生成的Firebug 中 PageSpeed 选项卡中的图像:

我能够按照 Pumbaa80 的建议来访问 PageSpeed 的优化文件。希望此处的屏幕截图能够进一步阐明 FireFox 环境。 (但我无法在 Chrome 中访问这些优化文件的本地版本。)

并使用 Adob​​e Bridge 和 Adob​​e Bridge 清理凌乱的 PageSpeed 文件名。正则表达式:

虽然FireFox中的PageSpeed能够为我生成优化的图像文件,但它也更改了它们的名称,将简单的名称变成:

nice_picture.jpg

我发现

nice_picture_fff5e6456e6338ee09457ead96ccb696.jpg

这似乎是一个常见的抱怨。由于我不想手动重命名所有图片,因此我使用了 Adob​​e Bridge 的重命名工具以及正则表达式。您可以使用其他接受正则表达式的重命名命令/工具,但我怀疑 Adob​​e Bridge 对于我们大多数处理 PageSpeed 问题的人来说很容易使用!

  1. 启动 Adob​​e Bridge
  2. 选择所有文件(使用 Control A)
  3. 选择“工具”>“批量重命名(或 Control Shift R)
  4. 在预设字段中选择“字符串替换”。新文件名字段现在应显示“字符串替换”,后跟“原始文件名”
  5. 启用名为“使用正则表达式”的复选框
  6. 在“查找”字段中,输入正则表达式(这将选择从最右边的下划线分隔符):

    _(?!.*_)(.*)\.jpg$

  7. 在“替换为”字段中,输入:

    .jpg

  8. (可选)单击“预览”按钮查看建议的批量重命名结果,然后关闭

  9. 单击“重命名”按钮

请注意,处理后,Bridge 会取消选择不受影响的文件。如果您想清理所有 .png 文件,您需要重新选择所有图像并修改上面的配置(将“png”替换为“jpg”)。您还可以将上述配置保存为预设,例如“Clean PageSpeed jpg Images”,以便您将来可以快速清理文件名。

配置屏幕截图/故障排除

如果您遇到问题,某些浏览器可能无法正确显示上面的正则表达式(归咎于我的转义字符),因此有关配置屏幕截图(以及这些说明),请参阅:

如何使用 Adob​​e Bridge 的批量重命名工具清理有杂乱的优化 PageSpeed 图像文件名

To Replicate PageSpeed's JPG Compression Results in Windows:

I was able to get exactly the same compression results as PageSpeed using the Windows version of jpegtran which you can get at www.jpegclub.org/jpegtran. I ran the executable using the DOS prompt (use Start > CMD). To get exactly the same file size (down to the byte) as PageSpeed compression, I specified Huffman optimization as follows:

jpegtran -optimize source_filename.jpg output_filename.jpg

For more help on compression options, at the command prompt, just type: jpegtran

Or to Use the Auto-generated Images from the PageSpeed tab in Firebug:

I was able to follow Pumbaa80's advice to get access to PageSpeed's optimized files. Hopefully the screenshot here provides further clarity for the FireFox environment. (But I was not able to get access to a local version of these optimized files in Chrome.)

And to Clean up the Messy PageSpeed Filenames using Adobe Bridge & Regular Expressions:

Although PageSpeed in FireFox was able to generate optimized image files for me, it also changed their names turning simple names like:

nice_picture.jpg

into

nice_picture_fff5e6456e6338ee09457ead96ccb696.jpg

I discovered that this seems to be a common complaint. Since I didn't want to rename all my pictures by hand, I used Adobe Bridge's Rename tool along with a Regular Expression. You could use other rename commands/tools that accept Regular Expressions, but I suspect that Adobe Bridge is readily available for most of us working with PageSpeed issues!

  1. Start Adobe Bridge
  2. Select all files (using Control A)
  3. Select Tools > Batch Rename (or Control Shift R)
  4. In the Preset field select "String Substitution". The New Filenames fields should now display “String Substitution”, followed by "Original Filename"
  5. Enable the checkbox called “Use Regular Expression”
  6. In the “Find” field, enter the Regular Expression (which will select all characters starting at the rightmost underscore separator):

    _(?!.*_)(.*)\.jpg$

  7. In the “Replace with” field, enter:

    .jpg

  8. Optionally, click the Preview button to see the proposed batch renaming results, then close

  9. Click the Rename button

Note that after processing, Bridge deselects files that were not affected. If you want to clean all your .png files, you need reselect all the images and modify the configuration above (for "png" instead of "jpg"). You can also save the configuration above as a preset such as "Clean PageSpeed jpg Images" so that you can clean filenames quickly in future.

Configuration Screenshot / Troubleshooting

If you have troubles, it's possible that some browsers might not show the RegEx expression above properly (blame my escape characters) so for a screenshot of the configuration (along with these instructions), see:

How to Use Adobe Bridge's Batch Rename tool to Clean up Optimized PageSpeed Images that have Messy Filenames

离笑几人歌 2024-11-03 00:11:59

在我看来,能够一次性有效处理大多数图像格式的最佳选择是 trimage
它根据图像格式有效地利用 optipng、pngcrush、advpng 和 jpegoptim,并提供近乎完美的无损压缩。

如果使用命令行,实现非常简单。

sudo apt-get install trimage    
trimage -d images/*

瞧! :-)
此外,您还会发现一个非常简单的界面来手动执行此操作。

In my opinion the best option out there that effectively handles most image formats in a go is trimage.
It effectively utilizes optipng, pngcrush, advpng and jpegoptim based on the image format and delivers near perfect lossless compression.

The implementation is pretty easy if using a command line.

sudo apt-get install trimage    
trimage -d images/*

and voila! :-)
Additionally you will find a pretty simple interface to do it manually as well.

绅士风度i 2024-11-03 00:11:59

有一个非常方便的批处理脚本,可以使用 OptiPNG 递归优化文件夹下的图像(来自

FOR /F "tokens=*" %G IN ('dir /s /b *.png') DO optipng -nc -nb -o7 -full %G

一行!

There's a very handy batch script that recursively optimizes images beneath a folder using OptiPNG (from this blog):

FOR /F "tokens=*" %G IN ('dir /s /b *.png') DO optipng -nc -nb -o7 -full %G

ONE LINE!

晨曦慕雪 2024-11-03 00:11:59

如果您正在寻找批处理,请记住如果您没有 Xserver 可用,则会出现修剪抱怨。在这种情况下,只需编写 bash 或 php 脚本来执行诸如

<?php
    echo "Processing jpegs<br />";
    exec("find /home/example/public_html/images/ -type f -name '*.jpg' -exec jpegoptim --strip-all {} \;");
    echo "Processing pngs<br />";
    exec("find /home/example/public_html/images/ -type f -name '*.png' -exec optipng -o7 {} \;");
?>

“更改选项”之类的操作来满足您的需求。

If you are looking for batch processing, keep in mind trimage complains if you don't have Xserver avail. In that case just write a bash or php script to do something like

<?php
    echo "Processing jpegs<br />";
    exec("find /home/example/public_html/images/ -type f -name '*.jpg' -exec jpegoptim --strip-all {} \;");
    echo "Processing pngs<br />";
    exec("find /home/example/public_html/images/ -type f -name '*.png' -exec optipng -o7 {} \;");
?>

Change options to suite your needs.

就像说晚安 2024-11-03 00:11:59

对于 Windows,有几个拖放界面可以轻松访问。

https://sourceforge.net/projects/nikkhokkho/files/FileOptimizer/

对于 PNG我发现这个是为了我的乐趣,显然这个 GIU 中包含了 3 个不同的工具。只需拖放即可为您完成。

https://pnggauntlet.com/

不过这需要时间,尝试压缩一个 1MB png 文件 - 我很惊讶有多少 CPU进入这个压缩比较,这一定是这里发生的事情。似乎图像被压缩了 100 种方式,最好的获胜:D

关于 JPG 压缩,我觉得剥离颜色配置文件和所有额外信息是有风险的 - 但是 - 如果每个人都这样做 - 这是商业标准,所以我就这么做了我自己:D

我今天在 WP 安装中节省了 5500 个文件 113MB,所以它绝对值得!

For windows there are several drag'n'drop interfaces for easy access.

https://sourceforge.net/projects/nikkhokkho/files/FileOptimizer/

For PNG files I found this one for my enjoyment, apparently 3 different tools wrapped in this GIU. Just drag and drop and it does it for you.

https://pnggauntlet.com/

It takes time though, try compressing a 1MB png file - I was amazed how much CPU went into this compression comparison which has to be what is going on here. Seems the image is compressed a 100 ways and the best one wins :D

Regarding the JPG compression I to feel its risky to strip of color profiles and all extra info - however - if everyone is doing it - its the business standard so I just did it myself :D

I saved 113MB on 5500 files on a WP install today, so its definately worth it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文