Fast and secure on-the-fly thumbnail generation middleware for Express, based on QuickThumb. Uses pre-defined sizes to prevent malicious exploitation. Built with Sharp for optimal performance and memory efficiency.
- On-demand thumbnail generation
- Pre-defined size configurations for security
- High-performance image processing with Sharp
- Support for modern image formats (including WebP)
- Compatible with Express and Connect
- Simple slug-based URL format
npm install quickthumb-slugimport express from 'express';
import quickthumb from 'quickthumb-slug';
const app = express();
app.use(quickthumb.static({
baseDir: new URL('.', import.meta.url).pathname + "/public",
sizes: {
square: { width: 400, height: 400, type: "crop" },
landscape: { width: 1200, height: 600, type: "crop" },
fitinportrait: { width: 400, height: 700, type: "resize" }
}
}));const express = require('express');
const qt = require('quickthumb-slug');
const app = express();
app.use(qt.static({
baseDir: __dirname + "/public",
sizes: {
square: { width: 400, height: 400, type: "crop" },
landscape: { width: 1200, height: 600, type: "crop" },
fitinportrait: { width: 400, height: 700, type: "resize" }
}
}));<!-- Original image -->
<img src="/images/photo.jpg" />
<!-- Thumbnailed version using 'square' configuration -->
<img src="/images/photo-square.jpg" />The static() middleware accepts an options object with the following properties:
| Option | Type | Description | Default |
|---|---|---|---|
baseDir |
string | Source images directory | Required |
cacheDir |
string | Generated thumbnails directory | [baseDir]/thumbs/ |
sizes |
object | Size configurations (see below) | Default sizes |
quality |
number | JPEG quality (0-100) | 80 |
Each size configuration in the sizes object can have:
width: Width in pixelsheight: Height in pixelstype: Processing type"crop"(default): Exact dimensions using center crop"resize": Maintain aspect ratio, fit within dimensions
{
small: { width: 320, height: 9999, type: "crop" },
medium: { width: 768, height: 9999, type: "crop" },
large: { width: 1200, height: 9999, type: "crop" }
}Thumbnails are automatically generated based on the filename format:
[original-name]-[size-slug].[extension]
Examples:
- Original:
photo.jpg - Square thumbnail:
photo-square.jpg - Landscape version:
photo-landscape.jpg
- JPEG/JPG
- PNG
- GIF
- WebP
Generated thumbnails are cached in [cacheDir]/[type]/[slug]/. The cache is automatically invalidated when the source image is modified.
This library uses Sharp for image processing, which provides:
- 4-5x faster processing than ImageMagick
- Lower memory usage
- Better image quality
- Native WebP support
If you're upgrading from version 1.x:
- No code changes required - API remains compatible
- ImageMagick is no longer required
- Better memory management and performance
- Added WebP support
MIT