In this chapter, we will consider a scenario where a photography competition is held and photographers need to upload photographs to the portal. The moment a photograph is uploaded, a thumbnail should be created immediately.
A function code is the main content that executes and performs some operations as shown as follows:
var Jimp = require("jimp"); // JavaScript function must export a single function via module.exports // To find the function and execute it module.exports = (context, myBlob) => { // context is a must have parameter and first parameter always // context is used to pass data to and from the function //context name is not fixed; it can be anything // Read Photograph with Jimp Jimp.read(myBlob).then((image) => { // Manipulate Photograph // resize the Photograph. Jimp.AUTO can be passed as one of the values. image .resize(200, 200) .quality(40) .getBuffer(Jimp.MIME_JPEG, (error, stream) => { // Check for errors while processing the Photograph. if (error) { // To print the message on log console context.log('There was an error processing the Photograph.'); // To communicate with the runtime that function is finished to avoid timeout context.done(error); } else { // To print the message on log console context.log('Successfully processed the Photograph'); // To communicate with the runtime that function is finished to avoid timeout // Bind the stream to the output binding to create a new blob context.done(null, stream); } }); }); };