Routzie - documentation

Using sharp in Routzie

Introduction

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.

API Overview

Creating a SharpReference

Use the sharp function to create a SharpReference from a blob:

const sharpReference = await sharp(context, myBlobReference)

Common Methods

  • resize(width, height, options?) – Resize image
  • grayscale() / greyscale() – Convert to grayscale
  • rotate(angle, options?) – Rotate image
  • extract({ left, top, width, height }) – Crop region
  • composite(images, options?) – Composite multiple images
  • toBlob() – Export result as a new BlobReference
  • metadata() – Get image metadata

See full API in TypeScript typings for all available methods.

Usage Examples

1. Download and Grayscale an Image

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) })

2. Resize and Crop an Uploaded Image

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) })

3. Composite Two Images

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) })

4. Get Image Metadata

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) })

Notes

  • Always work with BlobReference objects for input and output.
  • All processing is done remotely via the media server.
  • Chain multiple operations before calling toBlob() to optimize performance.
  • Use metadata() to inspect image properties before processing.

© 2025 Routzie Routzie.com