node-image-processing-cloud-function v0.3.6
node-image-processing-cloud-function
Google Cloud Function library to resize images and store them in a bucket.
Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
Installing
npm install node-image-processing-cloud-function
Using
Here is an example of how a product feed can be generated.
const { ImageBucketEventProcessor } = require('node-image-processing-cloud-function');
const OUTPUT_BUCKET_NAME = process.env.OUTPUT_BUCKET_NAME;
const DEFAULT_CACHE_TIME = 60 * 60 * 24; // 1 day
const CACHE_CONTROL = `public, max-age=${DEFAULT_CACHE_TIME}`;
const processor = new ImageBucketEventProcessor({
outputBucketName: OUTPUT_BUCKET_NAME,
cacheControl: CACHE_CONTROL,
});
processor.addTransformationProfile({
name: 'Test',
match: { dir: { startsWith: 'test' },
allowDelete: true,
transformations: [
{
width: 300,
height: 300,
quality: 80,
fileSuffix: '-300w',
formats: ['jpeg', 'webp']
}, {
width: 600,
height: 600,
quality: 80,
fileSuffix: '-600w',
},
],
});
exports.handler = async (event) => {
try {
await processor.processEvent(event);
} catch (e) {
console.error('handler(): ', e);
}
};
The handler would resize images in the paths starting with test
.
Deployment
Single function can handle only one trigger, so to support both objects creation and deletion it needs to be deployed separately.
Deployment of the function with objects creation trigger.
gcloud functions deploy SourceToStaticResizer \
--entry-point handler\
--runtime nodejs10\
--region europe-west3\
--trigger-resource $TRIGGER_BUCKET_NAME\
--trigger-event google.storage.object.finalize\
--memory 512MB\
--retry\
--set-env-vars OUTPUT_BUCKET_NAME=$OUTPUT_BUCKET_NAME
512 MB RAM is recommended to resize files that are up-to 50 MB.
Deployment of the function with deletion trigger.
gcloud functions deploy SourceToStaticResizerDeleter \
--entry-point handler\
--runtime nodejs10\
--region europe-west3\
--trigger-resource $TRIGGER_BUCKET_NAME\
--trigger-event google.storage.object.delete\
--memory 128MB\
--retry\
--set-env-vars OUTPUT_BUCKET_NAME=static.silkyfoxy.com
Running the tests
npm test
npm run lint
License
This project is licensed under the MIT License - see the LICENSE file for details.