Detect, fetch, and optimize images with sharp, with optional queue-backed processing via @platformatic/job-queue.
- Detects image type from file signatures (magic bytes)
- Optimizes raster images (
jpeg,png,webp,avif) - Prevents animated image optimization
- Supports optional SVG passthrough
- Provides
fetchAndOptimize()for URL-based workflows (viaundici.request()) - Provides queue APIs (
Queue,createQueue) powered by@platformatic/job-queuefor distributed work - Throws structured
ImageErrorobjects
npm i @platformatic/image-optimizerOptimizes an input image buffer.
width: target max width (withoutEnlargementis enabled)quality: output quality used by format-specific encodersallowSVG: whenfalse(default), SVG images are not optimized and it throws an error
Fetches an image and then runs optimize().
Returns:
buffer: optimized image buffercontentType: upstreamcontent-typeresponse header (ornull)cacheControl: upstreamcache-controlresponse header (ornull)
Returns the detected image type (for example jpeg, png, webp) or null.
Queue-backed optimizer powered by @platformatic/job-queue.
Methods:
start()stop()fetchAndOptimize(url, width, quality, allowSVG?, enqueueOptions?)(auto-starts on first use)
Notes:
- Queue processing is URL-based (
fetchAndOptimize) and returns{ buffer, contentType, cacheControl }. enqueueOptionsis forwarded to@platformatic/job-queueenqueueAndWait()(for example:timeout,maxAttempts,resultTTL).
Creates and starts a Queue instance.
import { createQueue } from '@platformatic/image-optimizer'
const queue = await createQueue({ concurrency: 2 })
const { buffer, contentType, cacheControl } = await queue.fetchAndOptimize('https://example.com/image.jpg', 800, 75)
await queue.stop()Apache-2.0 - See LICENSE for more information.