Skip to content

Instantly share code, notes, and snippets.

@nokenwa
Last active March 24, 2020 11:26
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 nokenwa/5b3e11879f3a799575ac7e204920723d to your computer and use it in GitHub Desktop.
Save nokenwa/5b3e11879f3a799575ac7e204920723d to your computer and use it in GitHub Desktop.
const cloudinary = require('cloudinary').v2;
const { promisify } = require('util');
const uploadImage = promisify(cloudinary.uploader.upload); //YOU NEED TO SETUP AN ENVIRONMENT VARIABLE WITH 'CLOUDINARY_URL'
exports.handler = function(context, event, callback){
const pictureWidth = parseFloat(1200);
const pictureHeight = parseFloat(1600);
const ratio = pictureWidth / pictureHeight;
const cloudinaryTransforms = {
transformation: [
{ aspect_ratio: ratio, crop: 'crop' },
{ height: pictureHeight, crop: 'scale' },
{
overlay: context.PICTURE_OVERLAY_ID, //REPLACE WITH OVERLAY NAME IN CLOUDINARY
flags: 'relative',
height: '1.0',
width: '1.0',
},
{flags: "layer_apply", x: 0, y: 0},
{ overlay: {
font_family: "Cookie",
font_size: 150,
width: (pictureWidth-100),
text: `from ${event.Name}`,
},
},
{flags: "layer_apply", x: 0, y: ((pictureHeight-150)/2)},
],
};
const imageBaseUrl = event.MediaUrl0;
const res = uploadImage(imageBaseUrl, {
public_id:
imageBaseUrl.substr(imageBaseUrl.lastIndexOf('/') + 1),
eager: [cloudinaryTransforms],
}).then(res =>{
const url = res.eager[0].secure_url;
callback(null, { picture: url });
}).catch(err =>{
callback(err.message);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment