Package Exports
- cwebp
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (cwebp) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
node-webp
Node.js wrapper for cwebp and dwebp binaries from WebP image processing utility.
Installation
npm install cwebp
Getting latest version of WebP
You can get latest WebP source, pre-compiled binaries and installation instructions from its official website, or from its downloads repository.
Linux users may use this installation script to automatically download and install latest WebP binaries:
curl -s https://raw.githubusercontent.com/Intervox/node-webp/latest/bin/install_webp | sudo bash
MacOS users may install WebP using MacPorts:
sudo port selfupdate
sudo port install webp
If none of it suit your needs, you may build the WebP utilities yourself.
Alternative ways to install WebP
MacOS users may install webp 0.4.0
using homebrew:
brew install webp
You may also install webp 0.3.x
as npm module:
npm install webp
Important: Using old WebP versions
Old versions of WebP (prior to 0.4.1
)
are not compatible with the latest node-webp
version.
If you're using old version of WebP, please,
use node-webp 0.1.x
.
Check this section for more info about new streaming features of the latest WebP version.
Usage
var CWebp = require('cwebp').CWebp;
var DWebp = require('dwebp').DWebp;
var encoder = new CWebp(source_image);
var decoder = new DWebp(source_webp);
or
// new is optional
var encoder = CWebp(source_image);
var decoder = DWebp(source_webp);
or
// Backward-compatibility with cwebp@0.1.x
var CWebp = require('cwebp');
Specifying path to cwebp binary
By default node-webp
looks for cwebp
and dwebp
binary in your $PATH
.
Specifying path as a constructor option
var Webp = require('cwebp');
var binPath = require('webp').cwebp;
var webp = new Webp(source, binPath);
Changing default behaviour
var CWebp = require('cwebp').CWebp;
CWebp.bin = require('webp').cwebp;
var encoder = new CWebp(source);
var DWebp = require('cwebp').DWebp;
DWebp.bin = require('webp').dwebp;
var decoder = new DWebp(source);
N.B.: webp
npm module provide old webp 0.3.x
binaries.
Available source types
When source is a string node-webp
treats it as a file path.
var CWebp = require('cwebp').CWebp;
var DWebp = require('dwebp').DWebp;
var encoder = new CWebp('original.jpeg');
var decoder = new DWebp('converted.webp');
It also accepts Buffers and Streams.
var encoder = new CWebp(buffer);
var decoder = new DWebp(stream);
Note that node-webp
will start listening to the data in a source stream
only when .write()
, .stream()
or .toBuffer()
is called.
Encoding and decodind WebP images
encoder.write('image.webp', function(err) {
console.log(err || 'encoded successfully');
});
decoder.write('image.png', function(err) {
console.log(err || 'decoded successfully');
});
Getting output image as a Buffer
decoder.toBuffer(function(err, buffer) {
// ...
});
Getting output image as a readable Stream
var stream = encoder.stream();
stream.pipe(destination);
stream.on('error', function(err) {
// something bad happened
});
Working with Streams
Different versions of WebP have different level of streaming support:
Feature | Older WebP versions | WebP 0.4.1 |
node-webp 1.x |
node-webp 0.1.x |
---|---|---|---|---|
cwebp stdin streaming | no | no | mock | mock |
cwebp stdout streaming | no | native | mock | native |
dwebp stdin streaming | no | native | mock | native |
dwebp stdout streaming | no | native | mock | native |
mock means that node-webp
acts as if the feature is supported,
mocking it using temporary files.
So, converting Stream into a Buffer with node-webp 0.x
will cause
two temporary files to be created and then removed
(one to store input stream, and another to read output buffer from).
Note that node-webp
native streaming will work only
if your WebP version have native support for the corresponding stream,
while mock streaming will work with any version of WebP.
IMPORTANT: If you're using old version of WebP, please,
use node-webp 0.1.x
.
Using promises
node-webp
supports A+ promises.
encoder.write('image.webp').then(function() {
// ...
});
encoder.toBuffer().then(function(buffer) {
// ...
});
decoder.stream().then(function(stream) {
// ...
});
node-webp
uses when.js library.
Specifying conversion options
node-webp
provides helper functions for most of cwebp
and dwebp
conversion options.
For the full list of available helpers see methods.json file.
encoder.quality(60);
decoder.tiff();
Sending raw command
encoder.command('-d', 'dump.pgm');
Verbose errors reporting
node-webp
returns any error reported by cwebp
or dwebp
.
By default it uses standard error reporting mode,
but it's possible to enable cwebp
verbose error reporting.
var CWebp = require('cwebp').CWebp;
new CWebp(source).verbose().toBuffer(function (err, res) {
// err.message contains verbose error
});
dwebp
don't support verbose error reporting.
Changing default behaviour
var CWebp = require('cwebp').CWebp;
CWebp.verbose = true;
new CWebp(source).toBuffer(function (err, res) {
// err.message contains verbose error
});