xbing2002 / laravel-image-optimizer
优化您的Laravel应用中的图片
Requires
- php: ^7.2
- laravel/framework: ~5.7.0|~5.8.0
- spatie/image-optimizer: ^1.1.0
Requires (Dev)
- orchestra/testbench: ~3.7.0|~3.8.0
- phpunit/phpunit: ^8.0
README
本包是Laravel 5.4及以上版本的spatie/image-optimizer的特定集成。它可以通过一系列各种图片优化工具对PNG、JPG、SVG和GIF进行优化。该包会自动检测系统上安装了哪些优化二进制文件并使用它们。
以下是使用方法
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);
你不喜欢门面吗?没问题!只需从容器中解析出配置的
app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);
该包还包含一个中间件,可以自动优化所有请求中的图片。
你说不使用Laravel?没问题!只需直接使用底层的spatie/image-optimizer。
安装
您可以通过composer安装此包
composer require xbing2002/laravel-image-optimizer dev-master
该包将自动注册自己。
该包使用多个二进制文件来优化图片。有关如何安装这些二进制文件的信息,请参阅底层image-optimizer包的优化工具部分的readme。该readme还包含有关这些工具将对您的图片执行的操作的信息。
该包附带一些合理的默认值以优化图片。您可以通过发布配置文件来修改该配置。
php artisan vendor:publish --provider="Spatie\LaravelImageOptimizer\ImageOptimizerServiceProvider"
这是将要发布的config/image-optimizer
文件的内容
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 [ /** * 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 know to cause troubles ], Gifsicle::class => [ '-b', // required parameter for this package '-O3' // this produces the slowest but best results ], ], /** * 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, ];
如果您想自动优化上传到应用中的图片,请在http内核中将\Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class
添加到中间件。
// app/Http/Kernel.php protected $routeMiddleware = [ ... 'optimizeImages' => \Spatie\LaravelImageOptimizer\Middlewares\OptimizeImages::class, ];
用法
您可以从容器中解析出配置的Spatie\ImageOptimizer\OptimizerChain
实例
// 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);
你不喜欢门面吗?没问题!只需从容器中解析出配置的
app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);
使用中间件
请求使用optimizeImages
中间件的路线的所有图片将被自动优化。
Route::middleware('optimizeImages')->group(function () { // all images will be optimized automatically Route::post('upload-images', 'UploadController@index'); });
添加自己的优化器
有关如何创建自己的优化器的信息,请参阅底层spatie/image-optimizer包的readme中的“编写自定义优化器”部分。
您可以在配置文件的optimizers
数组中将优化器的完全限定类名添加为键。
示例转换
以下是优化器所执行的一些示例转换。
变更日志
请参阅CHANGELOG了解最近更改的信息。
测试
composer test
贡献
请参阅CONTRIBUTING以获取详细信息。
安全性
如果您发现任何安全相关的问题,请通过电子邮件freek@spatie.be联系,而不是使用问题跟踪器。
明信片软件
您可以使用这个软件包(它是MIT授权的),但如果它进入了您的生产环境,我们非常感谢您从家乡给我们寄一张明信片,并说明您使用了我们哪个软件包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
我们将所有收到的明信片发布在我们的公司网站上。
致谢
关于中间件的想法,用于优化请求中的所有文件,来自approached/laravel-image-optimizer。
支持我们
Spatie是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述这里。
您的业务是否依赖于我们的贡献?请联系我们,在Patreon上支持我们。所有承诺都将用于分配人力进行维护和新奇事物。
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。