veronica
v0.3.1
Published
Library for manipulating images
Readme
Veronica
Veronica is an image manipulation library built on top of sharp, providing easy resizing, cropping, and format conversion with built-in caching support.
Features
- Fast image resizing with aspect ratio preservation
- Smart cropping using smartcrop
- Native WebP, AVIF, PNG, JPEG, and GIF support
- Built-in image caching
- Placeholder image generation
- Auto-orientation based on EXIF data
- Cross-platform (macOS, Linux, Windows)
Requirements
No external binaries required! Sharp comes with pre-built binaries for all major platforms.
For placeholder generation, you'll need the canvas dependencies:
# Debian/Ubuntu
sudo apt-get install build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev
# macOS
brew install pkg-config cairo pango libpng jpeg giflib librsvg pixman
# Arch Linux
sudo pacman -S cairo pango giflib libjpeg-turbo librsvgInstallation
npm install veronicaBasic Usage
const Veronica = require('veronica');
const veronica = new Veronica({
cache: '/tmp/veronica-cache'
});
const resizeOptions = {
width: 400,
height: 400,
gravity: 'Center', // Or 'smartcrop' for automatic smart cropping
quality: 80,
type: 'webp' // Optional: output as WebP
};
// Resize the image and store it in the cache folder
veronica.getResizedImage('/path/to/original/image.jpg', resizeOptions, function(err, resizedPath) {
// resizedPath contains the path to the cached resized image
});API
new Veronica(options)
Create a new Veronica instance.
options.cache- Directory for cached files (default:/tmp/veronica-cache)
veronica.image(filePath)
Returns a VeronicaImage instance for the given file path.
const image = veronica.image('/path/to/image.jpg');
// Get image dimensions
image.size((err, size) => {
console.log(size.width, size.height);
});veronica.resize(filePath, options, callback)
Resize an image with the given options.
Options:
width- Target widthheight- Target heightmaxWidth- Maximum width (maintains aspect ratio)maxHeight- Maximum height (maintains aspect ratio)gravity- Crop gravity:'Center','North','South','East','West', or'smartcrop'quality- Output quality 1-100 (default: 80)type- Output format:'webp','jpeg','png', etc.target- Output file pathauto_orient- Auto-rotate based on EXIF (default: true)
veronica.getResizedImage(filePath, options, callback)
Same as resize(), but with automatic caching. Returns cached version if available.
veronica.convertToWebp(filePath, options, callback)
Convert an image to WebP format.
Options:
quality- Output quality 1-100 (default: 80)target- Output file path
veronica.placeholder(options, callback)
Generate a placeholder image.
Options:
width- Width in pixels (default: 100)height- Height in pixels (default: 100)text- Custom text (default: "WxH")backgroundStyle- Background color (default: '#CCC')textStyle- Text color (default: '#FFF')fontFamily- Font family (default: 'Impact')dpr- Device pixel ratio multiplier
Migrating from v0.2.x
Version 0.3.0 replaced GraphicsMagick (gm) with sharp. The API remains the same, but:
- No need to install GraphicsMagick or cwebp
- Significantly faster processing
- Native WebP support without external binaries
- Requires Node.js >= 18.17.0
Note: The callback-based API is maintained for backwards compatibility. While sharp uses Promises internally, veronica continues to use callbacks to ensure existing code works without modification for now.
License
MIT
