fatonsopa/ laravel-image-optimizer
优化您的Laravel应用程序中的图像
Requires
- php: ^7.0
- fatonsopa/image-optimizer: ^1.0.4
- illuminate/support: ~5.5.0|~5.6.0
Requires (Dev)
- orchestra/testbench: ~3.5.0|~3.6.0
- phpunit/phpunit: ^6.3|^7.0
This package is not auto-updated.
Last update: 2024-09-29 05:55:15 UTC
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);
你说不喜欢外观?没问题!只需从容器中解析配置的 Spatie\ImageOptimizer\OptimizerChain
实例。
app(Spatie\ImageOptimizer\OptimizerChain::class)->optimize($pathToImage);
包还包含一个 中间件,可自动优化所有请求中的图像。
你说不使用Laravel?没问题!只需直接使用底层的 spatie/image-optimizer。
安装
您可以通过 composer 安装此包
composer require spatie/laravel-image-optimizer
包将自动注册自己。
包使用多个二进制文件来优化图像。有关这些二进制文件的安装方法和详细信息,请参阅底层 image-optimizer 包的 优化工具部分。该说明还包含有关 这些工具对图像将做什么 的信息。
包自带了一些合理的默认配置来优化图像。您可以通过发布配置文件来修改此配置。
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 kernel 中添加 \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);
你说不喜欢外观?没问题!只需从容器中解析配置的 Spatie\ImageOptimizer\OptimizerChain
实例。
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 包中的 "Writing custom optimizers" 部分。
您可以将优化器的完全限定类名作为键添加到配置文件中的 optimizers
数组中。
示例转换
以下是优化器执行的一些 示例转换。
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
测试
composer test
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全
如果您发现任何安全相关的问题,请发送电子邮件至 freek@spatie.be 而不是使用问题跟踪器。
Postcardware
您可以使用这个包(它遵循 MIT 许可),但如果它进入了您的生产环境,我们非常感谢您从您的家乡给我们寄一张明信片,说明您正在使用我们哪些包。
我们的地址是:Spatie,Samberstraat 69D,2060 安特卫普,比利时。
我们将所有收到的明信片 发布在我们的公司网站上。
致谢
中间件优化请求中所有文件的想法来自 approached/laravel-image-optimizer。
支持我们
Spatie 是一家位于比利时安特卫普的网页设计公司。您可以在我们的网站上找到我们所有开源项目的概述 这里。
您的业务依赖于我们的贡献吗?在 Patreon 上联系我们并支持我们。所有承诺都将用于分配人力以维护和开发新酷炫的东西。
许可证
MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。