A while ago we released image-optimizer. In short this package can make all kinds of images smaller by stripping out metadata and applying a little bit of compression. Read this blogpost to learn more about it. Although it's pretty easy to work with the package, we felt that we could deliver a more seamless experience in Laravel apps. That's why we created our newly released laravel-image-optimizer package.

The package uses a bunch of binaries to optimize images. To learn which ones and how to install them, head over to the optimization tools section in the readme of the underlying image-optimizer package. That readme also contains info on what these tools will do to your images.

Once the laravel-image-optimizer package is installed it's laughably easy to optimize images.

If you like facades this is how you'd go about it:

use ImageOptimizer ; ImageOptimizer::optimize($pathToImage); ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage);

You don't like facades you say? No problem! Just resolve a configured instance of Spatie\ImageOptimizer\OptimizerChain out of the container:

app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);

The package also contains a middleware to automatically optimize all images in a request.

Route::middleware( 'optimizeImages' )->group( function () { Route::post( 'upload-images' , 'UploadController@index); });

If you want to customize the chain of tools and the options being passed to them, you can do so by publishing and modifying the config file. This is the default contents:

use Spatie \ ImageOptimizer \ Optimizers \ Svgo ; use Spatie \ ImageOptimizer \ Optimizers \ Optipng ; use Spatie \ ImageOptimizer \ Optimizers \ Gifsicle ; use Spatie \ ImageOptimizer \ Optimizers \ Pngquant ; use Spatie \ ImageOptimizer \ Optimizers \ Jpegoptim ; return [ 'optimizers' => [ Jpegoptim::class => [ '--strip-all' , '--all-progressive' , ], Pngquant::class => [ '--force' , ], Optipng::class => [ '-i0' , '-o2' , '-quiet' , ], Svgo::class => [ '--disable=cleanupIDs' , ], Gifsicle::class => [ '-b' , '-O3' , ], ], 'timeout' => 60 , 'log_optimizer_activity' => false , ];

To learn more about laravel-image-optimzer head over to the readme on GitHub. And be sure to check out the other Laravel package we've previously made.