GithubHelp home page GithubHelp logo

knowsbuy / image-lambda Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leonfancy/image-lambda

0.0 2.0 0.0 22.27 MB

An AWS Lambda function that AWS S3 can invoke to create thumbnails or reduce file size for png and jpg images.

Shell 2.27% JavaScript 97.73%

image-lambda's Introduction

image-lambda

Build Status

An AWS Lambda function that AWS S3 can invoke to create thumbnails or reduce file size for png and jpg images.

Features:

  • Reduce jpg/png file size. Image file size is reduced by more than 70%.
  • PNG files with 100% support for transparency.
  • Resize jpg/png based on configuration.

Dependencies

  • node, version: 4.x
  • Vagrant, Required to build the deployment zip package. If you are already working on Linux system, you don't have to install Vagrant.

How it works

Once you deployed image-lambda package to AWS Lambda and configured it. When an image is uploaded to AWS S3 bucket, S3 sends an notification to AWS Lambda and invokes the image-lambda function. image-lambda reduce/resize the image based on configuration and then put the processed images to target bucket/directory.

AWS Lambda

image-lambda use GraphicsMagick to resize image, and imagemin to reduce image file size.

Installation

git clone https://github.com/SlimFancy/image-lambda.git
cd image-lambda
npm install

Configuration

image-lambda supports configuration for reduce/resize image. There is config.json.sample in project root directory as example. You can copy to use it.

$ cp config.json.example config.json

Here is an example of configuration:

{
  "reduce": {
    "sourceDir": "images/uploads",
    "targetBucket": "example",
    "targetDir": "images/reduce",
    "ACL": "public-read"
  },
  "resizes": [
    {
      "width": 100,
      "sourceDir": "images/uploads",
      "targetBucket": "example",
      "targetDir": "images/100w",
      "ACL": "public-read"
    },
    {
      "height": 200,
      "sourceDir": "images/uploads",
      "targetBucket": "example",
      "targetDir": "images/200h",
      "ACL": "public-read"
    },
    {
      "height": 200,
      "width": 200,
      "resizeOption": "^",
      "sourceDir": "images/uploads",
      "targetBucket": "example",
      "targetDir": "images/200",
      "format": "jpg",
      "ACL": "public-read"
    }
  ]
}
  • reduce: Define params for reduce image.
  • resizes: Define different image sizes. This example creates 3 thumbnails with different sizes.
  • sourceDir/targetDir: For example, the uploaded image S3 key is "images/uploads/test-images/test.jpg", you want to generate a reduced image to directory images/reduce, so you can configure sourceDir=images/uploads and targetDir=images/reduce. Thus, you will get a reduced image with key "images/reduce/test-images/test.jpg". image-lambda replace the sourceDir in image s3 key with the targetDir.
  • ACL: private | public-read | public-read-write | authenticated-read | aws-exec-read | bucket-owner-read | bucket-owner-full-control. Controls the permission of generated images.
  • targetBucket: (Optionally) Specify the bucket where you want to put the generated images. If omitted will use the same bucket as the source image.
  • width/height: It's better to just specify one of them, the other side can be resized based on the ratio.
  • resizeOption: For resizing images, this optional parameter can be set to one of the ImageMagick geometry operators.
  • format: specify a different output format for the image (this parameter only valid for for resizes).

Create AWS Lambda deployment package

As AWS Lambda function actually running on Amazon Linux, if you compile node modules on you local machine which is not Linux system, some binaries (such as imagemin used in this project) might not work after you deploy function to AWS Lambda.

In order to solve this problem, Vagrant is used to provision a Linux machine locally, and the Lambda deployment package is built in this vagrant machine.

A npm script is created to provision the Linux machine and create the deployment zip package.

$ npm run vagrant:build

This command takes a few ten minutes the first time you run it, because it needs to download the Linux image and install node environment in the virtual machine. After this command finished, you can find the zip package in build/ folder with name image-lambda.zip.

If your local operating system is already Linux (such as Ubuntu, CentOS), you can just run below command to build the package:

$ npm run build

Contributing

If you'd like to contribute to the project, feel free to submit a PR.

Run tests:

$ npm test

test/fixture directory contains jpg and png images for testing. After you run tests, the reduced and resized images are stored in test/out.

License

MIT

image-lambda's People

Contributors

leonfancy avatar matthewberryman avatar slimfancy avatar cbdileo avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.