Skip to content

Instantly share code, notes, and snippets.

@dzeitman
Last active April 22, 2018 03:27
Show Gist options
  • Save dzeitman/9bf2ac4ddc40bb98d8bf954884270856 to your computer and use it in GitHub Desktop.
Save dzeitman/9bf2ac4ddc40bb98d8bf954884270856 to your computer and use it in GitHub Desktop.
fetch image with multiple faces and return cropped faces array;
/**
1> Create a webtask.
2> Add NPM Module for Cloudinary
3> Add secrets for your cloud, api_key and api_secret
*/
var cloudinary = require("cloudinary");
var namor = require('namor');
function uploadImage(context,cb){
var url = context.query.url || "http://res.cloudinary.com/de-demo/image/upload/v1524355278/emerge-hackathon-faces.jpg";
var public_id = namor.generate({ words: 2, numbers: 0 });
var options = {faces:true, public_id: public_id};
cloudinary.v2.uploader.upload(url, options, function(error,result){
if(error){
console.log(error);
cb(error);
}
if(result){
var faces = result.faces;
var urls = [];
var cnt = 0;
faces.forEach(function(item){
var name = result.public_id;
var x = item[0];
var y = item[1];
var width = item[2];
var height = item[3];
var template = `https://res.cloudinary.com/de-demo/image/upload/x_${x},y_${y},w_${width},h_${height},c_crop/w_200/${name}`;
urls.push(template);
cnt++
if(cnt === faces.length){
cb(null,urls);
}
});
}
})
}
module.exports = function(context, cb) {
cloudinary.config({
"cloud_name": context.secrets.cloud_name,
"api_key": context.secrets.api_key,
"api_secret": context.secrets.api_secret
});
uploadImage(context,cb)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment