Parse Cloud module for image manipulation using sharp
Install the module by npm
$ npm i -S parse-cloud-image
or using yarn
$ yarn add parse-cloud-image
Processing images can be done using the new file triggers on Parse Server introduced in version 4.2.0 All triggers can be found here
Define beforeSaveFile
trigger:
import { ParseImage } from 'parse-cloud-image'
Parse.Cloud.beforeSaveFile(async (request: Parse.Cloud.FileTriggerRequest) => {
const image = ParseImage.from(request.file)
return image.resize(256)
})
Create ParseImage
to make image manipulations. If the file is not an image file error will be thrown.
ParseImage
wraps a ParseFile
and provides several basic methods for manipulations:
When both a width and height are provided, aspect ratio will be preserve, ensure the image covers both provided dimensions by cropping/clipping to fit. For more options check sharp's documentation and use process
method below
const image = ParseImage.from(parseFile)
image.resize(256, 128)
Rotate the image by specified angle. For more options check sharp's documentation and use process
method below
const image = ParseImage.from(parseFile)
image.rotate() // auto-rotated using EXIF Orientation tag
Scaling the image by specified factor. For more options check sharp's documentation and use process
method below
const image = ParseImage.from(parseFile)
image.scale(1.0, 0.5)
These methods are the heart of the ParseImage
giving elegant access to all capabilities of sharp
over given ParseFile
. Let's say you need to flip and rotate the image. This can be done like this:
const image = ParseImage.from(parseFile)
const processedImage = await image.edit(proc => proc.flip().rotate().toBuffer())
}
At the end the closure must return Buffer
from the chain of actions which will be serialized as new ParseFile
.
In some cases you would need option to manipulate in parallel image or create multiple different changes on different clones
of the image. This can be done with sharp
and the process
method of ParseImae
providing a Sharp
instances which can be piped.
const file = bucket.file("image.jpg").createWriteStream()
const image = ParseImage.from(parseFile)
...
image.process(sharp => sharp.resize(width, height)).pipe(file)