### Install Package via Composer Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Installs the spatie/laravel-image-optimizer package using Composer. ```bash composer require spatie/laravel-image-optimizer ``` -------------------------------- ### Run Tests Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Execute the test suite for the package using Composer. ```bash composer test ``` -------------------------------- ### Publish Configuration File Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Publishes the configuration file for the image optimizer package to your Laravel project. ```bash php artisan vendor:publish --provider="Spatie\LaravelImageOptimizer\ImageOptimizerServiceProvider" ``` -------------------------------- ### Image Optimizer Configuration Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Configuration options for the image optimizer package, including default optimizers and their parameters. ```php 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; use Spatie\ImageOptimizer\Optimizers\Cwebp; return [ /** * When calling `optimize` the package will automatically determine which optimizers * should run for the given image. */ 'optimizers' => [ Jpegoptim::class => [ '-m85', // set maximum quality to 85% '--strip-all', // this strips out all text information such as comments and EXIF data '--all-progressive' // this will make sure the resulting image is a progressive one ], Pngquant::class => [ '--force' // required parameter for this package ], Optipng::class => [ '-i0', // this will result in a non-interlaced, progressive scanned image '-o2', // this set the optimization level to two (multiple IDAT compression trials) '-quiet' // required parameter for this package ], Svgo::class => [ '--disable=cleanupIDs' // disabling because it is known to cause trouble ], Gifsicle::class => [ '-b', // required parameter for this package '-O3' // this produces the slowest but best results ], Cwebp::class => [ '-m 6', // for the slowest compression method in order to get the best compression. '-pass 10', // for maximizing the amount of analysis pass. '-mt', // multithreading for some speed improvements. '-q 90', //quality factor that brings the least noticeable changes. ], ], /** * The maximum time in seconds each optimizer is allowed to run separately. */ 'timeout' => 60, /** * If set to `true` all output of the optimizer binaries will be appended to the default log. * You can also set this to a class that implements `Psr\Log\LoggerInterface`. */ 'log_optimizer_activity' => false, ]; ``` -------------------------------- ### Optimize Image using Facade Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Optimizes an image by replacing it with an optimized version. An optional second parameter can specify a different output path. ```php use ImageOptimizer; // Optimize and replace original image ImageOptimizer::optimize($pathToImage); // Optimize and save to a different path ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage); ``` -------------------------------- ### Optimize Image using Facade Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Optimizes an image using the ImageOptimizer facade. The original image can be replaced or saved to a different path. ```php use ImageOptimizer; // the image will be replaced with an optimized version which should be smaller ImageOptimizer::optimize($pathToImage); // if you use a second parameter the package will not modify the original ImageOptimizer::optimize($pathToImage, $pathToOptimizedImage); ``` -------------------------------- ### Optimize Image using Container Instance Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Optimizes an image by resolving the OptimizerChain from the Laravel container and calling its optimize method. ```php app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage); ``` -------------------------------- ### Apply OptimizeImages Middleware to Routes Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Applies the 'optimizeImages' middleware to a group of routes, automatically optimizing all images uploaded within those routes. ```php Route::middleware('optimizeImages')->group(function () { // all images will be optimized automatically Route::post('upload-images', 'UploadController@index'); }); ``` -------------------------------- ### Register OptimizeImages Middleware Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Adds the OptimizeImages middleware to the http kernel for automatic image optimization on uploads. ```php // app/Http/Kernel.php protected $middlewareAliases = [ ... 'optimizeImages' => \Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class, ]; ``` -------------------------------- ### Optimize Image using OptimizerChain Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Optimizes an image using the OptimizerChain instance resolved from the container. The original image can be replaced or saved to a different path. ```php // the image will be replaced with an optimized version which should be smaller app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage); // if you use a second parameter the package will not modify the original app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage, $pathToOptimizedImage); ``` -------------------------------- ### Add Custom Optimizer Source: https://github.com/spatie/laravel-image-optimizer/blob/main/README.md Demonstrates how to add a custom optimizer to the configuration. This involves specifying the fully qualified class name of your optimizer in the `optimizers` array within the configuration file. ```php $config['optimizers'] = [ 'Your\Custom\Optimizer\Classname' => [ // Optimizer specific configuration ], // Other optimizers... ]; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.