Laravel Image Optimizer
Laravel integration that shrinks PNG, JPG, SVG, and GIF images using system optimization tools.
Repository Health
Technical Analysis
Laravel Image Optimizer is the Laravel-specific integration of Spatie’s image-optimizer library. It reduces the file size of PNGs, JPGs, WebP, SVGs, and GIFs by running them through a chain of well-known command-line optimization tools such as jpegoptim, pngquant, optipng, svgo, and gifsicle, automatically detecting which binaries are installed on the host.
It ships a Laravel service provider, an ImageOptimizer facade, a publishable configuration file, and an optional HTTP middleware that transparently optimizes images as they are uploaded, so improving image performance often requires just a single method call.
What You Get
- An
ImageOptimizerfacade and container binding for the configured optimizer chain - Automatic detection and use of installed CLI tools (jpegoptim, pngquant, optipng, svgo, gifsicle, cwebp)
- A publishable config file to tune each tool’s options and the chain
- An
OptimizeImagesmiddleware that optimizes uploaded images automatically - Lossless-by-default optimization for PNG, JPG, WebP, SVG, and GIF
Common Use Cases
- Automatically optimizing user-uploaded images
- Batch-shrinking existing images to reduce storage and bandwidth
- Improving page performance by serving smaller images
- Optimizing images on the fly via request middleware
Under The Hood
Architecture - The package is a thin Laravel adapter over spatie/image-optimizer. src/ImageOptimizerServiceProvider.php reads config/image-optimizer.php, builds an OptimizerChain via src/OptimizerChainFactory.php, and binds it into the container; src/Facades/ImageOptimizer.php exposes it as a facade. src/Middlewares/OptimizeImages.php intercepts uploaded files on a request and optimizes them in place. The actual per-format optimization (jpegoptim, pngquant, optipng, svgo, gifsicle, cwebp) is delegated to the underlying Spatie library, which shells out to whichever binaries are present.
Tech Stack - PHP 8.0+, depending on spatie/image-optimizer ^1.2 and the Laravel framework (8 through 13). PSR-4 autoloaded under Spatie\LaravelImageOptimizer\. Requires the relevant CLI optimization tools installed on the host.
Code Quality - A small, mature Spatie package (248+ commits, 27 releases) with a PHPUnit/Testbench suite and PHP-CS-Fixer formatting. The code is minimal and idiomatic, delegating heavy lifting to the well-tested core library.
API Design - The surface is deliberately tiny: call ImageOptimizer::optimize($path) or register the middleware. Configuration is optional, tools are auto-detected, and there is essentially no boilerplate, making it one of the easiest ways to add image optimization to a Laravel app.