Created
January 17, 2018 22:34
-
-
Save SaulBurgos/7ab6341988255c9d35b44ccf9cf1b21c to your computer and use it in GitHub Desktop.
AWS code example to slice image
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
'use strict'; | |
console.log('Loading function'); | |
const aws = require('aws-sdk'); | |
const s3 = new aws.S3({ apiVersion: '2006-03-01' }); | |
var async = require('async'); | |
//http://aheckmann.github.io/gm/docs.html | |
var gm = require('gm').subClass({ imageMagick: true }); // Enable ImageMagick integration. | |
var util = require('util'); | |
exports.handler = (event, context, callback) => { | |
// Get the object from the event and show its content type | |
const bucketName = event.Records[0].s3.bucket.name; | |
const keyName = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')); | |
const paramsOrigin = { | |
Bucket: bucketName, | |
Key: keyName, | |
}; | |
var typeMatch = keyName.match(/\.([^.]*)$/); | |
var imageType = typeMatch[1]; //"jpg" | |
if (!typeMatch) { | |
callback("Could not determine the image type."); | |
return; | |
} | |
async.waterfall([ | |
function download(next) { | |
// Download the image from S3 into a buffer. | |
s3.getObject(paramsOrigin, next); | |
}, | |
function resize(response, next) { //if you need resize your image | |
gm(response.Body).size(function(err,size) { | |
if(err) { | |
next('Error getting size of image'); | |
} | |
if(size.width != 4096 ) { | |
this.resize(4096, 2048, "!").toBuffer(imageType, function(err, buffer) { | |
if (err) { | |
next('Error resizing image'); | |
} else { | |
next(null, response.ContentType, buffer,{ | |
width: 4096, | |
height: 2048 | |
}); | |
} | |
}); | |
} else { | |
next( null, response.ContentType,response.Body,{ | |
width: 4096, | |
height: 2048 | |
}); | |
} | |
}); | |
}, | |
function slice (contentType, data,imgDimension, next) { | |
var allPromises = []; | |
//example tiles to slice | |
var positions = [ | |
[0,0], | |
[256,0], | |
[512,0], | |
]; | |
function getPromiseCrop(id,positionX,positionY,imageData) { | |
return new Promise(function (resolve, reject){ | |
gm(imageData).crop(256, 256,positionX,positionY) | |
.toBuffer(imageType, function(err, buffer) { | |
if (err) { | |
reject('error promise'); | |
next(err); | |
} else { | |
console.log('promise done' + id); | |
s3.putObject({ | |
Bucket: bucketName, | |
Key: 'lambda_output/' + id + '.jpg', | |
Body: buffer, | |
ContentType: contentType | |
},function(){ | |
resolve(); | |
}); | |
} | |
}); | |
}); | |
} | |
for (var y = 0; y < positions.length; y++) { | |
var currentPromise = getPromiseCrop(y,positions[y][0],positions[y][1],data); | |
allPromises.push(currentPromise); | |
} | |
Promise.all(allPromises).then(function(values) { | |
next( null,'done all promises'); | |
},function(error){ | |
console.log(error); | |
}); | |
}, | |
function upload(msg,next) { | |
next( null,msg); | |
} | |
], function (err) { | |
if (err) { | |
callback(null, err); | |
} else { | |
callback(null, 'Successfully sliced'); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code example was triggered by the put on a bucket