A simple express middleware for resizing images using sharp and pipes
MIT License
Express middleware for resizing images with sharp and short url query parameters.
Supports content negotiation, and http caches.
import express from 'express';
import { sharpie as createSharpie } from 'sharpie';
const app = express();
const sharpie = createSharpie({
param: 'url', // or an async function(req) that returns a file path
q: 90,
rs: "w:320,h:240,max",
format: false, // negotiate format
bg: 'white',
crop: 'center',
flatten: true,
hostnames: false,
ratio: 'minXMinY',
sizes: '16,32,48', // sizes for ico format
signs: {
assignment: ':', // use ~ for better uri-encoding
separator: ',' // use ! for better uri-encoding
},
https: {
rejectUnauthorized: false
}
});
// will get the url through req.params[opts.param] – e.g /param/foo.jpg
app.get('/param:url(*)', sharpie);
// will get the url through req.query[opts.param] – e.g. /query?url=/foo.jpg
app.get('/query', sharpie);
app.listen();
It supports a limited subset of sharp options that can be given as parameters or as defaults when initializing the middleware:
ex=x:50,y:50,w:100,h:100
extracts the full image.param
Key in req.params or req.query
Or async req => filepath
function.
formats list of allowed formats for negotiation. Default to ['svg', 'png', 'jpeg', 'webp'] To test avif encoding, pass formats: ['svg', 'png', 'jpeg', 'webp', 'avif'].
hostnames whitelist of other hostnames that can be proxied. True for all, false for none. Or a function, a map, an array - anything taking a hostname and returning a boolean.
sizes the sizes of the favicon in ico format, separated by a comma. defaults to 64,32,16.
Unrecognized types are proxied as is with a warning. (Future versions might change that behavior).
import { sharp } from 'sharpie';
All errors are HttpError instances, passed to next middleware.