long / aws-lambda-image

Automatic image resize/reduce on AWS Lambda

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

aws-lambda-image

Build Status Code Climate Coverage Status npm version Join the chat at https://gitter.im/aws-lambda-image

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.

Requirements

  • node.js ( AWS Lambda working version is 4.3.2 )
  • make

Installation

Clone this repository and install dependencies:

$ git clone git@github.com:ysugimoto/aws-lambda-image.git
$ cd aws-lambda-image
$ NODE_ENV=production npm install .

When upload to Lambda, the project will bundle all files. So we should ignore development packages (e.g. test tools)

If you are developper, please install all packages :-)

Packaging

AWS Lambda accepts zip archived package. To create it, run make lambda task simply.

$ make lambda

It will create aws-lambda-image.zip at project root. You can upload it.

Configuration

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.json

Configuration is simple, see below:

{
  "bucket": "your-destination-bucket",
  "backup": {
      "directory": "./original",
  },
  "reduce": {
      "directory": "./reduced",
      "prefix": "reduced-",
      "quality": 90,
      "acl": "public-read"
  },
  "resizes": [
    {
      "size": 300,
      "directory": "./resized/small",
      "prefix": "resized-",
    },
    {
      "size": 450,
      "directory": "./resized/medium",
      "suffix": "_medium"
    },
    {
      "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
    }
  ]
}

Configuration Parameters

name field type description
bucket - String Destination bucket name at S3 to put processed image. If not supplied, it will use same bucket of event source.
backup - Object Backup original file setting.
directory String Image directory path. When starts with ./ relative to the source, otherwise creates a new tree.
bucket String Destination bucket to override. If not supplied, it will use bucket setting.
reduce - Object Reduce setting following fields.
directory String Image directory path. When starts with ./ relative to the source, otherwise creates a new tree.
prefix String Append filename prefix if supplied.
suffix String Append filename suffix if supplied.
quality Number Determine reduced image quality ( enables only JPG ).
bucket String Destination bucket to override. If not supplied, it will use bucket setting.
acl String Permission of S3 object. See acl values.
resize - Array Resize setting list of following fields.
background String Background color to use for transparent pixels when destination image doesn't support transparency.
bucket String Destination bucket to override. If not supplied, it will use bucket setting.
crop String Dimensions to crop the image. See ImageMagick crop documentation.
directory String Image directory path. When starts with ./ relative to the source, otherwise creates a new tree.
prefix String Append filename prefix if supplied.
suffix String Append filename suffix if supplied.
gravity String Changes how size and crop. See ImageMagick gravity documentation.
quality Number Determine reduced image quality ( forces format JPG ).
format String Image format override. If not supplied, it will leave the image in original format.
size String Image dimensions. See ImageMagick geometry documentation.
orientation Boolean Auto orientation if value is true.
acl String Permission of S3 object. See acl values.

If you want to check how this works with your configuration, you can use configtest:

$ make configtest

Complete / Failed hooks

You can handle resize/reduce/backup 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);
});

Image resize

  • ImageMagick (installed on AWS Lambda)

Image reduce

  • cjpeg
  • pngquant
  • pngout

License

MIT License.

Author

Yoshiaki Sugimoto

Image credits

Thanks for testing fixture images:

About

Automatic image resize/reduce on AWS Lambda

License:MIT License


Languages

Language:JavaScript 98.2%Language:Makefile 1.8%