image-resize-s3

Upload multiple sizes of an image to AWS S3

npm install image-resize-s3
32 downloads in the last week
65 downloads in the last month

Image -> Resize -> S3 Build Status

Use this library to resize images and upload them to S3. It is designed for low-memory systems such as Heroku or modulus.io as well as handling large images (tested with a 19MB image).

As this is used for another project, discore, I've purposely limited the options. Let me know if there are specific things you'd like to add. Some forced options:

  • Images are converted to progressive
  • PNGs are stored as [name].png
  • JPEGs are stored as [name].jpg

Examples

  • the tests have some examples, though they are general
  • discore has a real-world usage example

THIS API DOCUMENTATION IS OUT OF DATE

This following API refers to the 0.1.x branch which uses Buffers and Streams and never hits disk. Unfortunately, this kills low-memory systems.

API

var resize = require('image-resize-s3')

resize.isSupportedFormat(format)

Checks whether a format is supported by this library. Currently, the supported output formats are:

  • PNG
  • JPEG

resize.options(options{})

View the default options.

  • s3{} - S3 options for uploading images
    • key - can also be set using the S3_KEY environmental variable
    • secret - can also be set using the S3_SECRET environmental variable
    • bucket - can also be set using the S3_BUCKET environmental variable
  • imageMagick - uses ImageMagick by default as it's what's supported by Heroku
  • headers{} - default headers for variants
  • quality - downgrades JPEG quality to this value if previously higher
  • original{} - basically a special variant{}
  • variants[] - an array of variant{}, each with the following properties:
    • slug - images are saved in the [name].[slug].[format] format
    • maxSize - image is resized to this maximum dimension. Variant is not created if its maximum dimension is already smaller.
    • headers - set headers per variant
    • quality - set quality per variant

By default, variants have a HTTP cache header of 1 year and are stored in reduced redundancy storage in S3 for cost savings (as you can just create them again).

resize(buffer || stream, name [, options [, callback]])

  • buffer || stream - the image must either be a Buffer or a readable Stream
  • name'' is the name of the image as stored on S3
  • options{}
    • format - output format. By default, it's the input format.
  • callback() is an optional callback, executing .end() immediately.

resize().end([callback])

Executes .identify() then .upload(). However, it also returns an error if the image type is not supported (GIFs).

resize().identify([callback])

Also aliased as .import([callback]).

identifys the image and stores all the metadata in this instance as resize().identity. The following attributes are added:

  • resize().supportedFormat - a Boolean that tells you whether the input format is supported. If false, this lib will convert the image to JPEG by default.

If callback is set, callback(null, identity) where identity is the identify metadata.

View the source code to view additional information that is automatically processed.

resize().upload([callback])

Also aliased as .export([callback]).

Resizes all the images and uploads them to S3. Requires identify() to be executed first.

Variants

Each variant emitted will have the following metadata:

  • slug
  • maxSize
  • format
  • name - as saved on S3
  • headers - as sent to S3
  • length - in bytes
  • identity - its identify metadata

Events

  • identify
  • error (err)
  • resize (variant) - When a variant is starts resizing
  • resized (variant) - When a variant is resized and identified
  • resized stream (variant, stream) - Stream for the resized variant
  • resized buffer (variant, buffer) - Buffer for the resized variant
  • upload (variant) - When a variant begins uploading
  • uploaded (variant) - When a variant has been uploaded
  • finished (variant) - When a variant is resized and uploaded as they are executed in parallel
  • end - When all the variants are finished

License

The MIT License (MIT)

Copyright (c) 2013 Jonathan Ong me@jongleberry.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you