An AWS Lambda Function to resize/reduce images automatically. When an image is put on AWS S3 bucket, this package will resize/reduce it and put to S3.
node.js( AWS Lambda working version is 4.3.2 )make
Clone this repository and install dependencies:
$ git clone git@github.com:ysugimoto/aws-lambda-image.git
$ cd aws-lambda-image
$ npm install .AWS Lambda accepts zip archived package. To create it, run make lambda task simply.
$ make lambdaIt will create aws-lambda-image.zip at project root. You can upload it.
This works with config.json put on project root. There is config.json.sample as example. You can copy to use it.
$ cp config.json.sample config.jsonConfiguration is simple, see below:
{
"bucket": "your-destination-bucket",
"reduce": {
"directory": "reduced",
"quality": 90
},
"resizes": [
{
"size": 300,
"directory": "resized/small"
},
{
"size": "600x600^",
"gravity": "Center",
"crop": "600x600",
"directory": "resized/cropped-to-square"
},
{
"size": 600,
"directory": "resized/600-jpeg",
"format": "jpg",
"background": "white"
},
{
"size": 900,
"directory": "resized/large",
"quality": 90
}
]
}bucket: [String] Destination bucket name at S3 to put processed image. If not supplied, it will use same bucket of event source.reduce: [Object] Reduce setting.directory: [String] Image directory path.bucket: [Object] Destination bucket to override. If not supplied, it will usebucketsetting.quality: [Number] Determine reduced image quality ( enables onlyJPG).
resizes: [Array] Resize setting.background: [String] Background color to use for transparent pixels when destination image doesn't support transparency.bucket: [Object] Destination bucket to override. If not supplied, it will usebucketsetting.crop: [String] Dimensions to crop the image. See ImageMagick crop documentation.directory: [String] Image directory path.format: [String] Image format override. If not supplied, it will leave the image in original format.gravity: [String] Changes howsizeandcrop. See ImageMagick gravity documentation.quality: [Number] Determine reduced image quality ( enables onlyJPG).size: [String] Image dimensions. See ImageMagick geometry documentation.
If you want to check how this works with your configuration, you can use configtest:
$ make configtestYou can handle resize/reduce process on success/error result on index.js. ImageProcessor::run will return Promise object, run your original code:
processor.run(config)
.then(function(proceedImages)) {
// Success case:
// proceedImages is list of ImageData instance on you configuration
/* your code here */
// notify lambda
context.succeed("OK, numbers of " + proceedImages.length + " images has proceeded.");
})
.catch(function(messages) {
// Failed case:
// messages is list of string on error messages
/* your code here */
// notify lambda
context.fail("Woops, image process failed: " + messages);
});ImageMagick(installed on AWS Lambda)
cjpegpngquantpngout
MIT License.
Yoshiaki Sugimoto
Thanks for testing fixture images: