Skip to content

Instantly share code, notes, and snippets.

@fsmunoz
Last active April 20, 2017 19:11
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 fsmunoz/1cd2b6c9f29b0eb6dfac8827b991d72d to your computer and use it in GitHub Desktop.
Save fsmunoz/1cd2b6c9f29b0eb6dfac8827b991d72d to your computer and use it in GitHub Desktop.
// The JPEGINATOR
// OpenWhisk action to convert images to JPEG
//
// Author: Frederico Munoz <frederico.munoz@pt.ibm.com>
// Date: APR 2017
// License: Eclipse Public License 1.0
var fs = require('fs');
var gm = require('gm').subClass({imageMagick: true});
exports.main = function (params) {
// We must receive the image as base64 and identified by the image
// keyword
var b64string = params.image;
var buf = Buffer.from(b64string, 'base64')
// Using promises since gm is async
return new Promise(function(resolve, reject) {
gm(buf).toBuffer('JPEG', function (err, buffer) {
if (err)
{
console.log("Error");
return {result: "error"};
} else {
console.log("Success");
resolve({image: buffer.toString('base64')});
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment