Laravel Image Optimizer

Laravel integration that shrinks PNG, JPG, SVG, and GIF images using system optimization tools.

Library
Composer
v1.8.3
1,309stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
64/100Good
Development Activity56
Maintenance32
Community68
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
79/100Good
Architecture80
Code Quality85
Innovation62
Learning Curve90

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 ImageOptimizer facade 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 OptimizeImages middleware 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search