Sharp is available in Routzie machines for advanced image processing. Unlike standard Node.js usage, all operations are performed via the Routzie media server and work with BlobReference
objects. This allows you to manipulate images efficiently and securely in distributed environments.
Use the sharp
function to create a SharpReference
from a blob:
const sharpReference = await sharp(context, myBlobReference)
resize(width, height, options?)
– Resize imagegrayscale()
/ greyscale()
– Convert to grayscalerotate(angle, options?)
– Rotate imageextract({ left, top, width, height })
– Crop regioncomposite(images, options?)
– Composite multiple imagestoBlob()
– Export result as a new BlobReference
metadata()
– Get image metadataSee full API in TypeScript typings for all available methods.
route('GET', 'download-image', async function downloadImage(req: ProcessedRequest) { const result = await fetch(req.context, 'https://picsum.photos/200/300') if (result.body instanceof BlobReference) { const sharpReference = await sharp(req.context, result.body) const grayscaleBlob = await sharpReference.grayscale().toBlob() return new RouteResult(grayscaleBlob, 200) } return new RouteResult('Unable to resolve image', 500) })
route(['POST'], 'process-image', async function(req: ProcessedRequest) { if (req.body instanceof BlobReference) { const sharpRef = await sharp(req.context, req.body) const resized = sharpRef.resize(100, 100) const cropped = resized.extract({ left: 10, top: 10, width: 80, height: 80 }) const outBlob = await cropped.toBlob() return new RouteResult(outBlob, 200) } return new RouteResult('Bad request', 400) })
route(['POST'], 'composite-images', async function(req: ProcessedRequest) { const { baseImage, overlayImage } = req.body // both BlobReference const baseSharp = await sharp(req.context, baseImage) const overlaySharp = await sharp(req.context, overlayImage) const composited = baseSharp.composite([ { input: overlaySharp, top: 20, left: 20, opacity: 0.5 } ]) const outBlob = await composited.toBlob() return new RouteResult(outBlob, 200) })
route(['GET'], 'image-metadata', async function(req: ProcessedRequest) { const imageBlob = await storage.getValue('image') if (imageBlob instanceof BlobReference) { const sharpRef = await sharp(req.context, imageBlob) const meta = await sharpRef.metadata() return new RouteResult(meta, 200, { 'Content-Type': 'application/json' }) } return new RouteResult('Image not found', 404) })
BlobReference
objects for input and output.toBlob()
to optimize performance.metadata()
to inspect image properties before processing.© 2025 Routzie Routzie.com