What is pngquant-bin?
The pngquant-bin npm package provides a Node.js wrapper for the pngquant command-line tool, which is used to compress PNG images. It allows developers to reduce the file size of PNG images while maintaining a balance between quality and compression.
What are pngquant-bin's main functionalities?
Compress PNG images
This feature allows you to compress PNG images by specifying the quality range. The code sample demonstrates how to use the pngquant-bin package to compress an image named 'input.png' and save the compressed version as 'output.png'.
const pngquant = require('pngquant-bin');
const { execFile } = require('child_process');
execFile(pngquant, ['--quality=65-80', 'input.png', '-o', 'output.png'], err => {
if (err) {
throw err;
}
console.log('Image compressed successfully');
});
Batch processing of PNG images
This feature allows you to compress multiple PNG images in a directory. The code sample demonstrates how to read all files in the 'images' directory, compress each one, and save the compressed versions in the 'compressed_images' directory.
const pngquant = require('pngquant-bin');
const { execFile } = require('child_process');
const fs = require('fs');
const path = require('path');
const inputDir = 'images';
const outputDir = 'compressed_images';
fs.readdir(inputDir, (err, files) => {
if (err) {
throw err;
}
files.forEach(file => {
const inputFile = path.join(inputDir, file);
const outputFile = path.join(outputDir, file);
execFile(pngquant, ['--quality=65-80', inputFile, '-o', outputFile], err => {
if (err) {
throw err;
}
console.log(`${file} compressed successfully`);
});
});
});
Other packages similar to pngquant-bin
imagemin-pngquant
The imagemin-pngquant package is a plugin for Imagemin that uses pngquant to compress PNG images. It offers similar functionality to pngquant-bin but is designed to be used within the Imagemin ecosystem, which provides a more extensive set of image optimization tools.
pngcrush-bin
The pngcrush-bin package provides a Node.js wrapper for the pngcrush command-line tool, which is another utility for optimizing PNG images. While pngcrush focuses on reducing file size by eliminating unnecessary data, it may not achieve the same level of compression as pngquant.
optipng-bin
The optipng-bin package offers a Node.js wrapper for the OptiPNG command-line tool, which is used to optimize PNG images. OptiPNG performs lossless compression, meaning it reduces file size without affecting image quality, but it may not achieve the same compression ratios as pngquant.
pngquant-bin
pngquant
is a PNG compressor that significantly reduces file sizes by converting images to a more efficient 8-bit PNG format
You probably want imagemin-pngquant
instead.
Install
$ npm install pngquant-bin
Make sure you have the correct version of libimagequant.
# via Homebrew for macOS
$ brew install libimagequant
# via apt-get for Debian distributions
$ sudo apt-get install libimagequant-dev
Usage
import {execFile} from 'node:child_process';
import pngquant from 'pngquant-bin';
execFile(pngquant, ['-o', 'output.png', 'input.png'], error => {
console.log('Image minified!');
});
CLI
$ npm install --global pngquant-bin
$ pngquant --help
Updating pre-compiled binaries
The Linux binaries are statically linked so they should work on all Linux distributions. To recompile them:
sudo apt-get install libpng-dev
./configure CFLAGS=-static && make && cp pngquant pngquant-64
- Repeat the above commands, but in a 32-bin docker container started with: docker run -ti -v
pwd
:/source i386/debian:9.3 bash