imagemin-optipng vs imagemin-pngout vs imagemin-pngquant vs imagemin-webp
PNGおよびWebP画像の最適化におけるimageminプラグインの比較
imagemin-optipngimagemin-pngoutimagemin-pngquantimagemin-webp類似パッケージ:

PNGおよびWebP画像の最適化におけるimageminプラグインの比較

imagemin-optipngimagemin-pngoutimagemin-pngquant、およびimagemin-webpは、すべてimageminエコシステム向けの画像圧縮プラグインです。これらのパッケージは、PNGやWebP形式の画像をビルド時に最適化し、ファイルサイズを削減しながら視覚的品質を維持することを目的としています。PNG用の3つのプラグイン(optipng、pngout、pngquant)はそれぞれ異なるアルゴリズムと圧縮戦略を採用しており、WebP用のプラグインは現代的な次世代画像フォーマットへの変換を提供します。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
imagemin-optipng0169-106年前MIT
imagemin-pngout0143.47 kB0-MIT
imagemin-pngquant03267.28 kB162年前MIT
imagemin-webp05176.18 kB203年前MIT

PNGおよびWebP画像最適化:imageminプラグインの技術的比較

フロントエンド開発において、画像の最適化はパフォーマンス改善の鍵です。imagemin-optipngimagemin-pngoutimagemin-pngquantimagemin-webpは、それぞれ異なるアプローチでPNGやWebP画像を処理します。この記事では、各プラグインの内部動作、設定方法、実用上のトレードオフをコード付きで解説します。

🖼️ 圧縮方式:lossless vs lossy vs フォーマット変換

imagemin-optipngimagemin-pngoutlossless(可逆)圧縮 を行います。元の画像と完全に同じ画質を保ちつつ、メタデータの削除や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-pngquantlossy(非可逆)圧縮 を行います。色数を減らすことでファイルサイズを大幅に削減しますが、画質に若干の劣化が生じます。

// 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 の設定

losslesstrue にすると可逆圧縮になります。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 })
  ]
});

🌐 ブラウザサポートとフォールバック戦略

  • PNGプラグイン(optipng/pngout/pngquant): 全てのブラウザで問題なく表示可能。
  • WebPプラグイン: Safari 14未満、IEなど一部ブラウザで非対応。

WebPを使う場合は、<picture>要素でフォールバックを提供するのがベストプラクティスです。

<picture>
  <source srcset="image.webp" type="image/webp">
  <img src="image.png" alt="Description">
</picture>

📊 まとめ:どのプラグインを選ぶべきか?

プラグイン圧縮方式画質圧縮率処理速度ブラウザ互換性
imagemin-optipnglossless完全維持速い全て
imagemin-pngoutlossless完全維持非常に遅い全て
imagemin-pngquantlossy若干劣化非常に高速い全て
imagemin-webplossy/lossless可変高(lossy時)速い現代ブラウザ
  • 汎用的な最適化imagemin-optipng
  • 最大限のPNG圧縮imagemin-pngout(時間的余裕がある場合)
  • モバイルファーストでサイズ重視imagemin-pngquant
  • 最新ブラウザ向けの次世代画像imagemin-webp(フォールバック必須)

最終的には、プロジェクトの要件(画質優先かサイズ優先か)、ターゲットブラウザ、ビルド時間の制約を考慮して選択してください。

選び方: imagemin-optipng vs imagemin-pngout vs imagemin-pngquant vs imagemin-webp

  • imagemin-optipng:

    imagemin-optipngは、バランスの取れた圧縮性能と安定性を求めるプロジェクトに適しています。lossless圧縮のみをサポートし、実行速度と圧縮率のトレードオフが穏やかです。CI/CDパイプラインや大規模なアセット処理で信頼性が高く、依存関係も軽量です。ただし、最高レベルの圧縮率が必要な場合は他の選択肢を検討すべきです。

  • imagemin-pngout:

    imagemin-pngoutは、極限までファイルサイズを小さくしたい場合に有効ですが、実行時間が非常に長くなる可能性があります。lossless圧縮でありながら、他のツールよりも優れた圧縮率を達成することがありますが、その代償として処理時間がかかる点に注意が必要です。時間的制約が厳しくないビルド環境や、静的アセットの事前最適化用途に向いています。

  • imagemin-pngquant:

    imagemin-pngquantは、lossy圧縮により劇的なファイルサイズ削減を実現します。色数を256色以下に限定することで、特にグラデーションや写真のような複雑な画像でも高い圧縮効果を発揮します。視覚的品質をある程度犠牲にしてもサイズ削減を優先するモバイル向けアプリやウェブサイトに最適です。透明度を含むPNGにも対応しています。

  • imagemin-webp:

    imagemin-webpは、現代ブラウザ向けにPNGやJPEGをWebP形式に変換する必要がある場合に選択します。lossyおよびlosslessの両方のモードをサポートし、同等の画質で通常PNGよりも小さなファイルサイズを実現できます。ただし、Safari 14未満など古いブラウザでは非対応のため、フォールバック戦略(例:picture要素)を併用する必要があります。

imagemin-optipng のREADME

imagemin-optipng Build Status

Imagemin plugin for OptiPNG

Install

$ npm install imagemin-optipng

Usage

const imagemin = require('imagemin');
const imageminOptipng = require('imagemin-optipng');

(async () => {
	await imagemin(['images/*.png'], 'build/images', {
		use: [
			imageminOptipng()
		]
	});

	console.log('Images optimized!');
})();

API

imageminOptipng(options?)(buffer)

Returns a Promise<Buffer>.

options

Type: object

optimizationLevel

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:

  1. 1 trial
  2. 8 trials
  3. 16 trials
  4. 24 trials
  5. 48 trials
  6. 120 trials
  7. 240 trials
bitDepthReduction

Type: boolean
Default: true

Apply bit depth reduction.

colorTypeReduction

Type: boolean
Default: true

Apply color type reduction.

paletteReduction

Type: boolean
Default: true

Apply palette reduction.

interlaced

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.

errorRecovery

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.

buffer

Type: Buffer

Buffer to optimize.