Skip to content

Instantly share code, notes, and snippets.

@petarjs
Created January 6, 2017 14:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save petarjs/83c7c599c810e46af8bbf9275b6cc3de to your computer and use it in GitHub Desktop.
Save petarjs/83c7c599c810e46af8bbf9275b6cc3de to your computer and use it in GitHub Desktop.
exports.handler = function(event, context) {
// We'd get this from the Lambda event in reality
var sampleInput = {
from: 1,
to: 39,
bucketName: 'test-bucket',
imageSizes: [{
width: 600,
height: 400,
description: 'A medium thumbnail',
prefix: 'thumb-',
suffix: '-600x400'
}, {
width: 200,
height: 100,
description: 'A mini thumbnail',
prefix: 'thumb-',
suffix: '-200x100'
}]
};
sampleInput.contentType = sampleInput.contentType || '.jpg';
// Init the S3 service wrapper
S3Wrapper.init({
bucketName: input.bucketName
});
// toUrl is now a function that will return a full S3 url for an image index
var toUrl = UrlTransformer.toImageUrl(sampleInput.contentType);
// Image.transformImage is curried, so transformImageToSizes is now a function
// that will transform an image to each thumb size from sampleInput.imageSizes
var transformImageToSizes = Image.transformImage(sampleInput.imageSizes);
var imageIndexes = _.range(input.from, input.to + 1);
// Paths where the images should be stored
var urls = _.map(imageIndexes, toUrl);
when
.all(_.map(urls, transformImageToSizes))
.then(function() {
console.log('complete!');
context.done(null, true);
}, function(reason) {
console.log('failed!', reason);
context.fail(reason, null);
});
};
/**** UrlTransformer ****/
var UrlTransformer = {
toImageUrl: function(contentType, imageIndex) { return imageIndex + contentType; },
getResizedImageKey: function(imageSize, key) {
var keyParts = key.split('.');
var suffix = imageSize.suffix || imageSize.height + 'x' + imageSize.width;
var prefix = imageSize.prefix || '';
return prefix + keyParts[0] + suffix + '.' + keyParts[1];
}
}
/**** Image ****/
var Image = {
transformImage: transformImage,
resizeImage: resizeImage,
getImageThumbs: getImageThumbs
}
// Gets an image from S3, resizes it, and saves the thumbs back to S3
var transformImage = _.curry(function(imageSizes, path) {
return S3.getImage(path)
.then(getImageThumbs(imageSizes, null))
.catch(function(err) {
console.log('error in transformImage', err);
});
});
// Recursively resize the image to each image size.
// We assume that imageSizes is a sorted array so that it goes from bigger to smaller
var getImageThumbs = _.curry(function(imageSizes, originalImageKey, image) {
var currentSize = _.head(imageSizes);
if(!currentSize || !image) {
return when(true);
}
originalImageKey = originalImageKey || image.Key;
return resizeImage(currentSize, image)
.then(S3.saveImage(UrlTransformer.getResizedImageKey(currentSize, originalImageKey)))
.then(getImageThumbs(_.tail(imageSizes), originalImageKey))
.catch(function(err) {
console.log('error in getImageThumbs', err);
});
});
// Does the actual resizing, using sharp
// https://www.npmjs.com/package/sharp
function resizeImage(currentSize, image) {
return when.promise(function(resolve, reject) {
var resizedImageString = currentSize.height + 'x' + currentSize.width;
sharp(image.Body)
.resize(currentSize.width, currentSize.height)
.toBuffer(function(err,resizedImage, info) {
image.Body = resizedImage;
resolve(image);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment