What is optipng-bin?
The optipng-bin package provides a Node.js wrapper for the OptiPNG image optimization tool. It allows you to optimize PNG images by reducing their file size without losing quality. This can be particularly useful for web developers looking to improve page load times by minimizing image sizes.
What are optipng-bin's main functionalities?
Optimize a single PNG image
This feature allows you to optimize a single PNG image using the highest optimization level (-o7). The code sample demonstrates how to use the optipng-bin package to optimize 'input.png' and log a message upon completion.
const execFile = require('child_process').execFile;
const optipng = require('optipng-bin');
execFile(optipng, ['-o7', 'input.png'], (err) => {
if (err) {
throw err;
}
console.log('Image optimized');
});
Optimize multiple PNG images
This feature allows you to optimize multiple PNG images in a batch. The code sample demonstrates how to loop through an array of image filenames and optimize each one using the optipng-bin package.
const execFile = require('child_process').execFile;
const optipng = require('optipng-bin');
const images = ['image1.png', 'image2.png', 'image3.png'];
images.forEach((image) => {
execFile(optipng, ['-o7', image], (err) => {
if (err) {
throw err;
}
console.log(`${image} optimized`);
});
});
Custom optimization level
This feature allows you to specify a custom optimization level for the PNG image. The code sample demonstrates how to use the optipng-bin package to optimize 'input.png' with an optimization level of 2.
const execFile = require('child_process').execFile;
const optipng = require('optipng-bin');
execFile(optipng, ['-o2', 'input.png'], (err) => {
if (err) {
throw err;
}
console.log('Image optimized with custom level');
});
Other packages similar to optipng-bin
imagemin-optipng
imagemin-optipng is a plugin for the imagemin image optimization tool that uses OptiPNG to compress PNG images. It offers similar functionality to optipng-bin but is designed to be used within the imagemin ecosystem, which supports multiple image formats and optimization plugins.
pngquant-bin
pngquant-bin is a Node.js wrapper for the pngquant command-line tool, which compresses PNG images using lossy compression. Unlike optipng-bin, which focuses on lossless compression, pngquant-bin can achieve smaller file sizes by allowing some quality loss.
pngcrush-bin
pngcrush-bin is a Node.js wrapper for the pngcrush command-line tool, which optimizes PNG images by trying various compression methods and selecting the best one. It offers similar functionality to optipng-bin but uses a different optimization algorithm.
optipng-bin
OptiPNG is a PNG optimizer that recompresses image files to a smaller size, without losing any information
You probably want imagemin-optipng
instead.
Install
$ npm install --save optipng-bin
Usage
import {execFile} from 'node:child_process';
import optipng from 'optipng-bin';
execFile(optipng, ['-out', 'output.png', 'input.png'], error => {
console.log('Image minified!');
});
CLI
$ npm install --global optipng-bin
$ optipng --help