shrinkray
A high-performance Rust library for image processing powered by libvips.
Features
- Fast image processing - Built on libvips for exceptional performance
- Fluent API - Intuitive builder pattern for chaining operations
- Format conversion - Support for JPEG, PNG, WebP, and AVIF
- Resizing & cropping - Multiple fit modes (crop, clip, max)
- Filters & effects - Vintage, sepia, monochrome, duotone, and more
- Image adjustments - Sharpen, blur, tint, rotation, and trimming
- Type-safe - Strong typing for colours, formats, and options
Installation
System Dependencies
- libvips 8.17.3 (for older 8.16.x libvips versions use shrinkray 1.0.1)
- libaom (for AVIF support via libheif)
- libheif
- libjpeg
- libpng
- libwebp
For more information on installing the required dependencies, see the Ubuntu and Alpine base images.
Usage
Basic Example
use ImageProcessor;
use ;
Create Thumbnails
let thumbnail = new
.thumbnail // 200x200 square thumbnail
.process?;
Apply Filters
let filtered = new
.vintage
.sharpen
.process?;
Duotone Effect
let duotone = new
.duotone
.duotone_alpha // 75% opacity
.process?;
Rotation and Trimming
let trimmed = new
.rotate
.trim // Auto-trim transparent/whitespace borders
.process?;
Custom Aspect Ratio
let cropped = new
.aspect_ratio
.width
.fit
.process?;
Device Pixel Ratio (Retina Support)
let retina = new
.width
.height
.device_pixel_ratio // Output will be 800x600
.process?;
Available Options
Resizing
width(i32)- Set output widthheight(i32)- Set output heightresize(i32, i32)- Set both width and heightthumbnail(i32)- Create square thumbnailfit(Fit)- Resize behavior (Crop, Clip, Max)device_pixel_ratio(i32)- DPR multiplieraspect_ratio(i32, i32)- Target aspect ratio
Format & Quality
format(Format)- Output format (Jpeg, Png, Webp, Avif)quality(i32)- Quality 1-100 (default: 75)lossless(bool)- Enable lossless compression
Adjustments
rotate(u16)- Rotate 90, 180, or 270 degreessharpen(u8)- Sharpening intensity 1-100blur(u8)- Blur amount 1-100trim()- Auto-trim borderstrim_colour(u8, u8, u8)- Trim specific colourbackground(u8, u8, u8)- Background colour for padding/flattening
Filters
grayscale()- Convert to grayscalemonochrome(u8)- Monochrome with intensitysepia(u8)- Sepia tone filtervintage(u8)- Vintage film effectpolaroid(u8)- Polaroid effectkodachrome(u8)- Kodachrome film simulationtechnicolor(u8)- Technicolor effecttint(u8, u8, u8)- Colour tint overlayduotone(u8, u8, u8, u8, u8, u8)- Duotone with shadow and highlight coloursduotone_alpha(u8)- Duotone opacity 1-100
Safety Limits
max_megapixels(f64)- Maximum input image sizemax_output_resolution(u32)- Maximum output dimension
ProcessedImage API
The process() method returns a ProcessedImage with these methods:
bytes(&self) -> &[u8]- Get image bytes as a sliceinto_bytes(self) -> Vec<u8>- Consume and get owned bytesformat(&self) -> Format- Get the output formatmime_type(&self) -> &'static str- Get MIME type string
Full Server Implementation
This library powers the shrinkray image server, which provides:
- HTTP/S3/file backend routing
- HMAC signature verification
- Prometheus metrics
- OpenTelemetry tracing
- Kubernetes deployment configs
See the main repository for the complete server implementation.
License
MIT License - see LICENSE for details.