What is cwebp-bin?
The cwebp-bin npm package provides a Node.js wrapper for the cwebp command-line tool, which is used to convert images to the WebP format. This package allows developers to easily integrate WebP conversion into their Node.js applications.
What are cwebp-bin's main functionalities?
Convert JPEG to WebP
This feature allows you to convert a JPEG image to WebP format using the cwebp command-line tool. The code sample demonstrates how to use the execFile function from the child_process module to execute the cwebp command with the input and output file paths.
const cwebp = require('cwebp-bin');
const { execFile } = require('child_process');
execFile(cwebp, ['input.jpg', '-o', 'output.webp'], err => {
if (err) {
throw err;
}
console.log('Image converted to WebP format');
});
Convert PNG to WebP
This feature allows you to convert a PNG image to WebP format using the cwebp command-line tool. The code sample demonstrates how to use the execFile function from the child_process module to execute the cwebp command with the input and output file paths.
const cwebp = require('cwebp-bin');
const { execFile } = require('child_process');
execFile(cwebp, ['input.png', '-o', 'output.webp'], err => {
if (err) {
throw err;
}
console.log('Image converted to WebP format');
});
Set WebP Quality
This feature allows you to set the quality of the output WebP image. The code sample demonstrates how to use the '-q' option with the cwebp command to specify the quality level (in this case, 80) for the output WebP image.
const cwebp = require('cwebp-bin');
const { execFile } = require('child_process');
execFile(cwebp, ['input.jpg', '-q', '80', '-o', 'output.webp'], err => {
if (err) {
throw err;
}
console.log('Image converted to WebP format with quality 80');
});
Other packages similar to cwebp-bin
imagemin-webp
The imagemin-webp package is a plugin for the imagemin image optimization tool that allows you to convert images to the WebP format. It provides a higher-level API compared to cwebp-bin and integrates seamlessly with the imagemin ecosystem, making it easier to use in build processes and workflows.
sharp
The sharp package is a high-performance image processing library for Node.js that supports a wide range of image formats, including WebP. It provides a rich set of features for image manipulation, such as resizing, cropping, and format conversion. Compared to cwebp-bin, sharp offers more comprehensive image processing capabilities and a more user-friendly API.
webp-converter
The webp-converter package is a Node.js wrapper for the WebP conversion tools, similar to cwebp-bin. It provides functions to convert images to and from the WebP format. While it offers similar functionality to cwebp-bin, it also includes additional features such as batch conversion and support for animated WebP images.
cwebp-bin
WebP is a new image format that provides lossless and lossy compression for images on the web. WebP lossless images are 26% smaller in size compared to PNGs. WebP lossy images are 25-34% smaller in size compared to JPEG images at equivalent SSIM index.
You probably want imagemin-webp
instead.
Install
$ npm install cwebp-bin
Usage
const {execFile} = require('child_process');
const cwebp = require('cwebp-bin');
execFile(cwebp, ['input.png', '-o', 'output.webp'], err => {
if (err) {
throw err;
}
console.log('Image is converted!');
});
CLI
$ npm install --global cwebp-bin
$ cwebp --help
License
MIT © Imagemin