Automatically optimize uploaded images with middleware
mainYou can automatically optimize all images included in a request by applying the optimizeImages middleware to your routes.
First, register the middleware alias in your HTTP Kernel:
// app/Http/Kernel.php
protected $middlewareAliases = [
...
'optimizeImages' => \Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class,
];Then, apply it to your routes:
Route::middleware('optimizeImages')->group(function () {
// All images in requests to these routes will be optimized automatically
Route::post('upload-images', 'UploadController@index');
});Route::middleware('optimizeImages')->group(function () {
// all images will be optimized automatically
Route::post('upload-images', 'UploadController@index');
});