Apache module for on-the-fly image resizing and compression using libvips for high-performance image processing.
- On-the-fly image resizing via URL parameters
- High-performance processing using libvips
- Thread-safe operation ideal for mpm_worker and mpm_event
- Multi-format support for JPEG, PNG, GIF, and WebP
- Optimized compression using a unified quality factor for all image formats
- Smart caching with optional source modification time checking
- Flexible cache system that preserves directory structure
- Aspect ratio preservation for resized images
This module uses libvips for several key advantages:
- Exceptional performance: Process images rapidly with minimal resource usage
- Memory efficiency: Optimized streaming architecture that minimizes memory footprint
- Thread-safety: Designed from the ground up for concurrent operations
- Stability: Robust behavior in multi-threaded environments
- Modern design: Clean, well-documented API for easy integration
- Apache 2.4+ with development headers
- libvips 8.9+ (with development headers)
- pngquant (for PNG optimization)
- libimagequant-dev
- mozjpeg (for JPG optimization)
- cgif (for GIF support)
See the Dockerfile for a complete list of dependencies and build instructions.
make
sudo make install
sudo cp mod_image_resize.conf /etc/apache2/conf-available/
sudo a2enconf mod_image_resize
sudo systemctl restart apache2
sudo apt-get update
sudo apt-get install debhelper devscripts build-essential apache2-dev \
cmake git nasm pkg-config libpng-dev libimagequant-dev pngquant \
libarchive-dev libglib2.0-dev libexpat-dev libfftw3-dev libexif-dev \
libtiff-dev libwebp-dev libgsf-1-dev liblcms2-dev libpango1.0-dev \
librsvg2-dev meson ninja-build patchelf
dpkg-buildpackage -us -uc -b
This will create a .deb
package in the parent directory.
sudo dpkg -i ../mod-image-resize_*.deb
sudo apt-get install -f # To resolve any missing dependencies
The package includes:
- The Apache module installed to
/usr/lib/apache2/modules/
- Custom compiled libvips with MozJPEG support
- MozJPEG optimized JPEG library in
/opt/mozjpeg/lib/
- Integrated cgif library for efficient GIF support
- Apache configuration in
/etc/apache2/conf-available/
- Cache directory in
/var/cache/apache2/image_resize/
Images can be resized on-the-fly using URLs with the following format:
http://your-server.com/resized/640x480/path/to/image.jpg
This will resize the image located at /var/www/images/path/to/image.jpg
to dimensions 640x480, optimizing and caching the result.
<Location /resized>
SetHandler image-resize
# Source images directory
ImageResizeSourceDir /var/www/images
# Cache directory for resized images
ImageResizeCacheDir /var/cache/apache2/image_resize
# Universal image compression quality (0-100)
ImageResizeQuality 75
# Cache-Control max-age in seconds (1 day = 86400)
ImageResizeCacheMaxAge 86400
# Enable mutex for cache operations (On/Off)
ImageResizeMutex On
# Check if source image is newer than cached image (On/Off)
ImageResizeCheckMTime Off
</Location>
Option | Description | Default |
---|---|---|
ImageResizeSourceDir |
Directory containing source images | /var/www/images |
ImageResizeCacheDir |
Directory for storing resized images | /var/cache/apache2/image_resize |
ImageResizeQuality |
Universal image compression quality (0-100) | 75 |
ImageResizeCacheMaxAge |
Cache-Control max-age value in seconds | 86400 (1 day) |
ImageResizeMutex |
Enable mutex for cache operations | On |
ImageResizeCheckMTime |
Check if source image is newer than cached | Off |
A Dockerfile is included for easy testing in an isolated environment:
# Build the Docker image
docker build -t mod_image_resize .
# Run the container
docker run -d -p 8080:80 \
-v $(PWD)/images:/var/www/images \
-v image_resize_cache:/var/cache/apache2/image_resize \
mod_image_resize
- The module is thread-safe and works well with mpm_worker and mpm_event
- Cache operations can be protected by a mutex to prevent race conditions (configurable)
- The mutex is only used when writing to the cache, not when reading
- HTTP caching is controlled via Cache-Control and Expires headers (configurable with ImageResizeCacheMaxAge)
- libvips is optimized for high performance and minimal memory usage
- You can disable the mutex in environments where file locking is not necessary
- Setting
ImageResizeCheckMTime
toOn
may have a performance impact but ensures cache freshness
404 Not Found
: Returned when the source image doesn't exist400 Bad Request
: Returned for invalid URLs500 Internal Server Error
: Returned for processing errors
This module is released under the Apache License 2.0.