Last active
December 19, 2017 13:35
-
-
Save rluisr/44fe64e67b7959a07313b3393eca0258 to your computer and use it in GitHub Desktop.
jpg to webp for node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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