自动化图像压缩
Automating Image Compression

原始链接: https://www.ramijames.com/thoughts/on-automating-image-compression

## 使用 pngquant 和 jpegoptim 进行命令行图像压缩 为了高效、自动化的图像压缩,**pngquant** 和 **jpegoptim** 是优秀的免费、跨平台命令行工具。它们非常适合项目范围内的优化、CI/CD 管道,以及在没有手动 GUI 交互的情况下获得可重复的结果。 **安装** 使用 Homebrew (macOS) 或 apt/dnf/pacman (Linux) 等包管理器即可轻松完成。 **PNG 压缩:** 使用 `pngquant --quality=65-85 --force --ext .png image.png` 压缩单个文件,或使用 `find ... -exec pngquant ... {} \;` 递归压缩。`--skip-if-larger` 标志可以防止重新压缩。 **JPG 压缩:** `jpegoptim --strip-all --max=85 image.jpg` 压缩单个文件。使用 `find ... -exec jpegoptim ... {} \;` 递归压缩。`--strip-all` 移除元数据,`--max=85` 在压缩和质量之间取得平衡。 **速度与效率:** 使用 `xargs -P [核心数]` 进行并行处理,以获得更快的速度,尤其是在服务器上。可选地,可以按文件大小过滤文件(例如 `-size +50k`),以避免压缩已经很小的图像。这些命令在自动化环境中是安全的。

这个Hacker News讨论的核心是自动化网络部署的图像压缩。原始帖子(ramijames.com)可能详细描述了一个工作流程,但评论者指出其中存在显著的简化。 一个关键问题是,建议的GitHub工作流程并没有将优化后的图像提交到Git,而是需要预先压缩所有图像,而不仅仅是新图像。此外,压缩可能会引入视觉伪影,因此在部署前需要验证——建议使用无损编码以确保安全。 讨论强烈建议使用WebP(通用支持)和理想情况下JPEG XL等现代图像格式,而不是较旧的PNG和JPG格式。一位评论员还指出Figma提供了内置的PNG质量/文件大小调整功能。另一位分享了一个ImageOptim克隆的链接,该克隆支持WebP编码,作为提交前的解决方案。
相关文章

原文

I recently poked around some command-line tools for compression. There are a lot of options, but the best seem to be pngquant and jpegoptim. They are fairly cross-platform and free. Both big bonuses in my book.

I want to use CLI tools because they:

  • Work across entire projects, not one file at a time
  • Are easy to automate (CI, build scripts, pre-deploy steps)
  • Produce consistent, repeatable results
  • Are not GUI tools which means no manual exporting required
Exported from Figma using 'Detailed'

Compressed using pngquant - 59,449 bytes

Step 1: Install the Tools

macOS (Homebrew)

brew install pngquant jpegoptim

Linux

Ubuntu / Debian:

sudo apt update
sudo apt install pngquant jpegoptim

Fedora:

sudo dnf install pngquant jpegoptim

Arch:

sudo pacman -S pngquant jpegoptim

You can then verify installation with:

pngquant --version
jpegoptim --version

Step 2: Compress PNGs

Single PNG file compression

pngquant --quality=65-85 --force --ext .png image.png

Recursively compress all PNGs in a project

find ./website-folder -type f -name "*.png" -exec pngquant --skip-if-larger --ext .png --quality=65-85 {} \;

This command recursively finds all PNG files, compresses them in place, preserves filenames, and applies a quality range suitable for most web UI and illustration assets. It will skip the file if larger, meaning that it won't try to recompress files that it has already compressed.

Step 3: Compress JPGs

Single JPG file compression

jpegoptim --strip-all --max=85 image.jpg

Recursively compress all JPGs in a project

find ./website-folder \( -iname "*.jpg" -o -iname "*.jpeg" \) -type f -exec jpegoptim --strip-all --max=85 {} \;

--max=85 applies strong compression with minimal visual loss, while --strip-all removes EXIF and metadata. Files are overwritten in place.

Optional: Parallel Processing!

Parallel execution is especially useful on Linux servers and modern multi-core Macs.

# PNGs
find ./website-folder -type f -name "*.png" -print0 \
  | xargs -0 -P 4 pngquant --force --ext .png --quality=65-85

# JPGs
find ./website-folder \( -iname "*.jpg" -o -iname "*.jpeg" \) -type f -print0 \
  | xargs -0 -P 4 jpegoptim --strip-all --max=85

Increase the -P value based on available CPU cores. These commands are safe for CI runners and build servers.

Optional: Skip Small Images

You can avoid processing files that won’t meaningfully benefit from compression.

# PNGs larger than 50KB
find ./website-folder -type f -name "*.png" -size +50k \
  -exec pngquant --force --ext .png --quality=65-85 {} \;

# JPGs larger than 50KB
find ./website-folder \( -iname "*.jpg" -o -iname "*.jpeg" \) -type f -size +50k \
  -exec jpegoptim --strip-all --max=85 {} \;
联系我们 contact @ memedata.com