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.
node-cwebp-bin
Dependency
WebP requires following libraries. See detail.
Linux
$ sudo apt-get install libjpeg-dev libpng-dev libtiff-dev libgif-dev
Mac OS X
$ sudo port install jpeg libpng tiff giflib
Install
$ npm install --save cwebp-bin
Usage
var execFile = require('child_process').execFile;
var cwebp = require('cwebp-bin').path;
execFile(cwebp, ['input.png', '-o', 'output.webp'], function (error) {
if (error) {
throw error;
}
console.log('Image was converted');
});
License
This is licensed under BSD.
WebP is licensed under Creative Commons Attribution 3.0 License.