What is imagemin-pngquant?
The imagemin-pngquant package is a plugin for Imagemin that uses pngquant to compress PNG images. It is designed to reduce the file size of PNG images while maintaining quality, making it useful for web developers and designers who need to optimize images for faster loading times.
What are imagemin-pngquant's main functionalities?
Basic PNG Compression
This feature allows you to compress PNG images using the pngquant algorithm. The quality option specifies the range of quality to maintain during compression.
const imagemin = require('imagemin');
const imageminPngquant = require('imagemin-pngquant');
(async () => {
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [
imageminPngquant({
quality: [0.6, 0.8]
})
]
});
console.log('Images optimized');
})();
Custom Quality Settings
This feature allows you to set custom quality settings for the PNG compression. The quality range can be adjusted to balance between file size and image quality.
const imagemin = require('imagemin');
const imageminPngquant = require('imagemin-pngquant');
(async () => {
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [
imageminPngquant({
quality: [0.3, 0.5]
})
]
});
console.log('Images optimized with custom quality settings');
})();
Speed Optimization
This feature allows you to optimize the speed of the compression process. The speed option can be set from 1 (slowest, best quality) to 11 (fastest, worst quality).
const imagemin = require('imagemin');
const imageminPngquant = require('imagemin-pngquant');
(async () => {
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [
imageminPngquant({
speed: 1
})
]
});
console.log('Images optimized with speed setting');
})();
Other packages similar to imagemin-pngquant
imagemin-optipng
imagemin-optipng is another Imagemin plugin that uses OptiPNG to compress PNG images. It is similar to imagemin-pngquant but uses a different algorithm that can sometimes result in better compression ratios at the cost of longer processing times.
imagemin-zopfli
imagemin-zopfli is an Imagemin plugin that uses Google's Zopfli compression algorithm to compress PNG images. It is known for achieving higher compression ratios than pngquant, but it is significantly slower.
imagemin-pngcrush
imagemin-pngcrush is an Imagemin plugin that uses PNGCrush to optimize PNG images. It offers a good balance between compression ratio and speed, making it a versatile alternative to imagemin-pngquant.
imagemin-pngquant
pngquant imagemin plugin
Install
$ npm install imagemin-pngquant
Usage
const imagemin = require('imagemin');
const imageminPngquant = require('imagemin-pngquant');
imagemin(['images/*.png'], 'build/images', {use: [imageminPngquant()]}).then(() => {
console.log('Images optimized');
});
API
imageminPngquant([options])(input)
Returns a Promise
for a Buffer
.
options
floyd
Type: number
boolean
Default: 0.5
Controls level of dithering (0 = none, 1 = full).
nofs
Type: boolean
Default: false
Disable Floyd-Steinberg dithering.
posterize
Type: number
Reduce precision of the palette by number of bits. Use when the image will be
displayed on low-depth screens (e.g. 16-bit displays or compressed textures).
quality
Type: string
Instructs pngquant to use the least amount of colors required to meet or exceed
the max quality. If conversion results in quality below the min quality the
image won't be saved.
Min and max are numbers in range 0 (worst) to 100 (perfect), similar to JPEG.
speed
Type: number
Default: 3
Speed/quality trade-off from 1
(brute-force) to 10
(fastest). Speed 10
has
5% lower quality, but is 8 times faster than the default.
verbose
Type: boolean
Default: false
Print verbose status messages.
input
Type: Buffer
Stream
Buffer or stream to optimize.
License
MIT © Imagemin