imagemin-optipng、imagemin-pngout、imagemin-pngquant、およびimagemin-webpは、すべてimageminエコシステム向けの画像圧縮プラグインです。これらのパッケージは、PNGやWebP形式の画像をビルド時に最適化し、ファイルサイズを削減しながら視覚的品質を維持することを目的としています。PNG用の3つのプラグイン(optipng、pngout、pngquant)はそれぞれ異なるアルゴリズムと圧縮戦略を採用しており、WebP用のプラグインは現代的な次世代画像フォーマットへの変換を提供します。
フロントエンド開発において、画像の最適化はパフォーマンス改善の鍵です。imagemin-optipng、imagemin-pngout、imagemin-pngquant、imagemin-webpは、それぞれ異なるアプローチでPNGやWebP画像を処理します。この記事では、各プラグインの内部動作、設定方法、実用上のトレードオフをコード付きで解説します。
imagemin-optipng と imagemin-pngout は lossless(可逆)圧縮 を行います。元の画像と完全に同じ画質を保ちつつ、メタデータの削除やIDATチャンクの再エンコードによってファイルサイズを削減します。
// imagemin-optipng: lossless圧縮
import imagemin from 'imagemin';
import optipng from 'imagemin-optipng';
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [optipng({ optimizationLevel: 3 })]
});
// imagemin-pngout: lossless圧縮(より高圧縮)
import imagemin from 'imagemin';
import pngout from 'imagemin-pngout';
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [pngout()]
});
imagemin-pngquant は lossy(非可逆)圧縮 を行います。色数を減らすことでファイルサイズを大幅に削減しますが、画質に若干の劣化が生じます。
// imagemin-pngquant: lossy圧縮
import imagemin from 'imagemin';
import pngquant from 'imagemin-pngquant';
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [pngquant({ quality: [0.6, 0.8] })]
});
imagemin-webp は フォーマット変換 を行います。PNGやJPEGをWebP形式に変換し、lossyまたはlosslessのいずれかで圧縮します。
// imagemin-webp: PNG → WebP変換
import imagemin from 'imagemin';
import webp from 'imagemin-webp';
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [webp({ quality: 80 })]
});
各プラグインは、ユースケースに応じて細かく調整可能です。
imagemin-optipng の設定optimizationLevel(0〜7)で圧縮レベルを指定。数値が高いほど圧縮率は向上しますが、処理時間も増加します。
optipng({
optimizationLevel: 5 // デフォルトは3
})
imagemin-pngout の設定copy オプションでメタデータの保持を制御できます。
pngout({
copy: 'all' // デフォルトは 'none'
})
imagemin-pngquant の設定quality は [min, max] の配列で指定。圧縮後の品質を動的に調整します。
pngquant({
quality: [0.65, 0.8], // 65%〜80%の品質範囲
speed: 1 // 1(高圧縮)〜11(高速)
})
imagemin-webp の設定lossless を true にすると可逆圧縮になります。quality はlossy時の画質を制御します。
webp({
lossless: false,
quality: 85,
alphaQuality: 90
})
imagemin-optipng: 処理速度が速く、中程度の圧縮率。日常的なビルドに最適。imagemin-pngout: 圧縮率は最高クラスだが、処理に数秒〜数十秒かかる場合も。リリースビルドでの最終最適化向き。imagemin-pngquant: 数百ミリ秒で劇的なサイズ削減。画質劣化を許容できるなら最強の選択肢。imagemin-webp: WebPエンコーダーの速度に依存。通常は高速で、PNGより30%以上小さいファイルが得られる。💡 実際のプロジェクトでは、
pngquant+optipngの組み合わせも有効です。まずpngquantで色数を減らし、その後optipngでlossless最適化を行うことで、さらなるサイズ削減が可能です。
// pngquant → optipngのチェーン
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [
pngquant({ quality: [0.6, 0.8] }),
optipng({ optimizationLevel: 3 })
]
});
WebPを使う場合は、<picture>要素でフォールバックを提供するのがベストプラクティスです。
<picture>
<source srcset="image.webp" type="image/webp">
<img src="image.png" alt="Description">
</picture>
| プラグイン | 圧縮方式 | 画質 | 圧縮率 | 処理速度 | ブラウザ互換性 |
|---|---|---|---|---|---|
imagemin-optipng | lossless | 完全維持 | 中 | 速い | 全て |
imagemin-pngout | lossless | 完全維持 | 高 | 非常に遅い | 全て |
imagemin-pngquant | lossy | 若干劣化 | 非常に高 | 速い | 全て |
imagemin-webp | lossy/lossless | 可変 | 高(lossy時) | 速い | 現代ブラウザ |
imagemin-optipngimagemin-pngout(時間的余裕がある場合)imagemin-pngquantimagemin-webp(フォールバック必須)最終的には、プロジェクトの要件(画質優先かサイズ優先か)、ターゲットブラウザ、ビルド時間の制約を考慮して選択してください。
imagemin-optipngは、バランスの取れた圧縮性能と安定性を求めるプロジェクトに適しています。lossless圧縮のみをサポートし、実行速度と圧縮率のトレードオフが穏やかです。CI/CDパイプラインや大規模なアセット処理で信頼性が高く、依存関係も軽量です。ただし、最高レベルの圧縮率が必要な場合は他の選択肢を検討すべきです。
imagemin-pngoutは、極限までファイルサイズを小さくしたい場合に有効ですが、実行時間が非常に長くなる可能性があります。lossless圧縮でありながら、他のツールよりも優れた圧縮率を達成することがありますが、その代償として処理時間がかかる点に注意が必要です。時間的制約が厳しくないビルド環境や、静的アセットの事前最適化用途に向いています。
imagemin-pngquantは、lossy圧縮により劇的なファイルサイズ削減を実現します。色数を256色以下に限定することで、特にグラデーションや写真のような複雑な画像でも高い圧縮効果を発揮します。視覚的品質をある程度犠牲にしてもサイズ削減を優先するモバイル向けアプリやウェブサイトに最適です。透明度を含むPNGにも対応しています。
imagemin-webpは、現代ブラウザ向けにPNGやJPEGをWebP形式に変換する必要がある場合に選択します。lossyおよびlosslessの両方のモードをサポートし、同等の画質で通常PNGよりも小さなファイルサイズを実現できます。ただし、Safari 14未満など古いブラウザでは非対応のため、フォールバック戦略(例:picture要素)を併用する必要があります。
Imagemin plugin for OptiPNG
$ npm install imagemin-optipng
const imagemin = require('imagemin');
const imageminOptipng = require('imagemin-optipng');
(async () => {
await imagemin(['images/*.png'], 'build/images', {
use: [
imageminOptipng()
]
});
console.log('Images optimized!');
})();
Returns a Promise<Buffer>.
Type: object
Type: number
Default: 3
Select an optimization level between 0 and 7.
The optimization level 0 enables a set of optimization operations that require minimal effort. There will be no changes to image attributes like bit depth or color type, and no recompression of existing IDAT datastreams. The optimization level 1 enables a single IDAT compression trial. The trial chosen is what. OptiPNG thinks it’s probably the most effective. The optimization levels 2 and higher enable multiple IDAT compression trials; the higher the level, the more trials.
Level and trials:
Type: boolean
Default: true
Apply bit depth reduction.
Type: boolean
Default: true
Apply color type reduction.
Type: boolean
Default: true
Apply palette reduction.
Type: boolean | undefined | null
Default: false
Enable Adam7 PNG interlacing on any images that are processed. Interlaced images look better when they're loaded partially, but usually interlace makes compression less efficient. Set to undefined or null to keep the same interlacing as the input image.
Type: boolean
Default: true
A reasonable amount of effort will be spent to try to recover as much data as possible of a broken image, but the success cannot generally be guaranteed.
Type: Buffer
Buffer to optimize.