优化 Laravel 应用中的图像
This package is the Laravel 6.0 and up specific integration of spatie/image-optimizer. It can optimize PNGs, JPGs, SVGs and GIFs by running them through a chain of various image optimization tools. The package will automatically detect which optimization binaries are installed on your system and use them.
Here's how you can use it:
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);
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 an request.
Don't use Laravel you say? No problem! Just use the underlying spatie/image-optimizer directly.
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
You can install the package via composer:
composer require spatie/laravel-image-optimizer
The package will automatically register itself.
The package uses a bunch of binaries to optimize images. To learn which ones on 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.
The package comes with some sane defaults to optimize images. You can modify that configuration by publishing the config file.
php artisan vendor:publish --provider="Spatie\LaravelImageOptimizer\ImageOptimizerServiceProvider"
This is the contents of the config/image-optimizer file that will be published:
…
If you want to automatically optimize images that get uploaded to your application add the \Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class in the http kernel.
// app/Http/Kernel.php
protected $middlewareAliases = [
...
'optimizeImages' => \Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class,
];
You can resolve a configured instance of Spatie\ImageOptimizer\OptimizerChain out of the container:
// 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);
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);
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);
All images that in requests to routes that use the optimizeImages-middleware will be optimized automatically.
Route::middleware('optimizeImages')->group(function () {
// all images will be optimized automatically
Route::post('upload-images', 'UploadController@index');
});
To learn how to create your own optimizer read the "Writing custom optimizers" section in the readme of the underlying spatie/image-optimizer package.
You can add the fully qualified classname of your optimizer as a key in the optimizers array in the config file.
Here are some example conversions that were made by the optimizer.
Please see CHANGELOG for more information what has changed recently.
composer test
Please see CONTRIBUTING for details.
If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.
You're free to use this package (it's MIT-licensed), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards on our company website.
The idea of a middleware that optimizes all files in a request is taken from approached/laravel-image-optimizer.
The MIT License (MIT). Please see License File for more information.
暂无开放 Issues,或尚未同步最近议题。