Image -> Resize -> S3
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
THIS API DOCUMENTATION IS OUT OF DATE
This following API refers to the 0.1.x
branch which uses Buffer
s and Stream
s and never hits disk.
Unfortunately, this kills low-memory systems.
API
var resize =
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 imageskey
- can also be set using theS3_KEY
environmental variablesecret
- can also be set using theS3_SECRET
environmental variablebucket
- can also be set using theS3_BUCKET
environmental variable
imageMagick
- uses ImageMagick by default as it's what's supported by Herokuheaders{}
- default headers for variantsquality
- downgrades JPEG quality to this value if previously higheroriginal{}
- basically a specialvariant{}
variants[]
- an array ofvariant{}
, each with the following properties:slug
- images are saved in the[name].[slug].[format]
formatmaxSize
- image is resized to this maximum dimension. Variant is not created if its maximum dimension is already smaller.headers
- set headers per variantquality
- 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 aBuffer
or a readableStream
name''
is the name of the image as stored on S3options{}
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])
.
identify
s the image and stores all the metadata in this instance as resize().identity
. The following attributes are added:
resize().supportedFormat
- aBoolean
that tells you whether the input format is supported. Iffalse
, this lib will convert the image toJPEG
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 S3headers
- as sent to S3length
- in bytesidentity
- itsidentify
metadata
Events
identify
error (err)
resize (variant)
- When a variant is starts resizingresized (variant)
- When a variant is resized and identifiedresized stream (variant, stream)
- Stream for the resized variantresized buffer (variant, buffer)
- Buffer for the resized variantupload (variant)
- When a variant begins uploadinguploaded (variant)
- When a variant has been uploadedfinished (variant)
- When a variant isresized
anduploaded
as they are executed in parallelend
- 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.