Lambda Image is a simple image class to allow to easily manipulate images on AWS Lambda
If you wish to download an image from a url, you must install got as a package in your project.
If you wish to be able to load and save to s3, (and you aren't on aws lambda) you must install aws-sdk
as a package in your project.
If you are testing locally, ensure you have imagemagick installed
const lambdaImage = require('lambda-image');
const image = await lambdaImage('/path/to/image.png');
const { height, width } = await image.getDimensions();
const resizedImage = await lambdaImage(await image.resize(512, 512));
await resizedImage.save({ bucket: 's3bucket', key: await resizeImage.getHashKey() });
Returns a image object. Must be called with await
loadInfo
can be either a
- buffer
- object with
{ bucket, key }
for an s3 file - base64 image with
data:{contentType};base64
prefix - url
- file path
If a url is provided, you must provide the got library in your dependecies.
loadOptions
is an object that can contain a profile of your aws credentials
const options = {
profile: 'default',
};
image.crop(width, height, gravity)
Returns the buffer for the image when crop is appliedimage.getContentType()
Get the mime typeimage.getDimensions()
Get the dimensions in an object{ height, width }
image.getExt()
Get the image extensionimage.getHash()
Get the md5 hash of the imageimage.getHashKey()
Get the save key based on the md5 hash2C2A8686BFA31A2AE5F55A7F60009E14 => 2/C/2C2A8686BFA31A2AE5F55A7F60009E14.png
image.getHeight()
Get the height of the imageimage.getSize()
Get the image file size in bytesimage.getType()
Get the image extensionimage.getWidth()
Get the width of the imageimage.identify()
Get the data fromimagemagick identify
image.quality(percentage)
Returns the buffer for the image when quality of percentage is appliedimage.resize(width, height)
Returns the buffer for the image when resized to width / height - Does not cropimage.resizeAndCropCenter(width, height)
Returns the buffer for the image when resized to width / height after croppingimage.rotate(degrees, color)
Returns the buffer for the image when rotated. Color is used to fill in background color (defualts to transparent)image.save(saveInfo, saveOptions = loadOptions)
Saves the image based on the saveInfo and saveOptionsimage.toBase64()
Converts the image to base64 with thedata:{contentType};base64
prefiximage.toBase64Binary()
Converts the image to base64 without thedata:{contentType};base64
prefiximage.toBuf()
Converts the image to a buffer
saveInfo
can be either a
- object with
{ bucket, key }
to save to an s3 bucket - file path
loadOptions
is an object that can contain a profile of your aws credentials. This will default to saveOptions
const options = {
profile: 'default',
};