Photonify
Photonify is a utility to manage image uploads and automatically process them into fingerprinted files. The plugin also supports S3 uploads.
Installation
NPM
npm install photonify
Yarn
yarn add photonify
Usage
- Photonify has a method called
processFiles
that will create four resized photos for you by default. The arguments passed to this method will differ slightly depending on filesystem vs. S3 storage. - Both examples are below:
Filesystem Storage:
Parameters:
- File Array: Buffer[] - Required
- outputDest: String - Required
- outputFormat: String
- sizes: String
Example with Custom Sizes:
import { processFiles } from "photonify";
const imageBuffer = req.file.buffer;
const result = await processFiles([imageBuffer], {
outputDest: path.join(__dirname, "resized_images"),
sizes: {
lg: {
width: 500,
height: 250,
},
md: {
width: 250,
height: 125,
},
},
});
S3 Storage:
Parameters:
- File Array: Buffer[] - Required
- storage: String - Required
- s3Config: any - Required details here
- s3Bucket: String - Required
- outputFormat: String
Example:
import { processFiles } from "photonify";
const imageBuffer = req.file.buffer;
const result = await processFiles([imageBuffer], {
storage: "s3",
s3Config: {
region: "us-west-1",
},
s3Bucket: "photonify",
});
Photonify Uses Sharp
- Under the hood, Photonify uses Sharp to process images: https://github.com/lovell/sharp
- Sharp may require additional setup steps for your specific environment. See this documentation for more details.
Removing Files
- Photonify has support for removing files from S3
- Note: No support for local filesystem removal is added to Photonify. You are encouraged to use the built-in fs.unlink method instead.
Removing S3 Files:
Parameters:
- File Names: String[] - Required
- storage: String - Required
- s3Config: any - Required details here
- s3Bucket: String - Required
Example:
import { removeFiles } from "photonify";
await removeFiles(["file1.jpg", "file2.jpg"], {
storage: "s3",
s3Config: {
region: "us-west-1",
},
s3Bucket: "photonify",
});