@empellio/image-optimizer
v0.1.0
Published
Server-side image optimization middleware for Node.js (inspired by the Next.js Image API). Sharp-powered resizing, format conversion (AVIF, WebP, JPEG, PNG), HTTP cache headers, and pluggable caching (memory, disk, Redis).
Downloads
5
Maintainers
Readme
@empellio/image-optimizer
Server-side image optimization library (inspired by the Next.js Image API).
Installation
npm i @empellio/image-optimizer sharp gotQuick start
import express from "express";
import { createImageOptimizer } from "@empellio/image-optimizer";
const app = express();
const optimizer = createImageOptimizer({
cache: { type: "disk", path: "./.cache/images", ttl: 60 * 60 * 24 },
formats: ["webp", "avif", "jpeg", "png"],
maxWidth: 4000,
maxHeight: 4000,
defaultQuality: 80,
});
app.use("/image", optimizer.express());
app.listen(3000);Fastify
import Fastify from "fastify";
import { createImageOptimizer } from "@empellio/image-optimizer";
const fastify = Fastify();
const optimizer = createImageOptimizer();
fastify.register(optimizer.fastify(), { prefix: "/image" });
fastify.listen({ port: 3000 });Request
GET /image?url=https://example.com/photo.jpg&w=800&h=600&fit=crop&format=webp&quality=80Parameters
url: source image (URL, local path, or uploaded buffer)w: target width (optional)h: target height (optional)fit:cover|contain|fill|inside|outside|crop(default:cover)format:webp|avif|jpeg|png(default: keep original)quality: 1–100 (default: 80)crop:smart(attention-based) orcenter(default)bg: background color (used when flattening transparent images)blur: blur radius (e.g., for placeholders)grayscale: convert to grayscalerotate: rotation in degrees (e.g., 90, 180)placeholder:blurto generate a 10px blurred placeholder
Programmatic usage
import { createImageOptimizer } from "@empellio/image-optimizer";
const optimizer = createImageOptimizer();
const result = await optimizer.process({ buffer, format: "webp", w: 300 });Tests & build
npm run test
npm run buildDemo servers
- Express demo:
npm run demo:express→ http://localhost:3001 - Fastify demo:
npm run demo:fastify→ http://localhost:3002
# run demo servers
npm run demo:express
npm run demo:fastify