Skip to content

Instantly share code, notes, and snippets.

@rluisr
Last active December 19, 2017 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rluisr/44fe64e67b7959a07313b3393eca0258 to your computer and use it in GitHub Desktop.
Save rluisr/44fe64e67b7959a07313b3393eca0258 to your computer and use it in GitHub Desktop.
jpg to webp for node.js
const fs = require('fs');
const path = require('path');
const CWebp = require('cwebp').CWebp;
module.exports.encode = (imgBuf) => new Promise((resolve, reject) => {
if (!fs.existsSync('/usr/local/bin/cwebp')) {
const RESOURCES_DIR = path.join(__dirname, "../bin");
process.env.PATH += `:${RESOURCES_DIR}`;
process.env.LD_LIBRARY_PATH += `:${RESOURCES_DIR}`;
}
const encoder = new CWebp(imgBuf);
encoder.quality(80);
encoder.toBuffer((err, buf) => {
if (err) reject(err);
resolve(buf);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment