sibche / 文件处理
文件处理助手
Requires
- php: >=5.5.9
- ext-curl: *
- imagine/imagine: ^0.7
- myclabs/php-enum: ^1.4
- psr/container: ^1.0
- symfony/http-foundation: ^3.0|^4.0|^5.0
Requires (Dev)
- illuminate/contracts: ^5.3|^6.0
- mikey179/vfsstream: ^1.6
- mockery/mockery: 0.9.*
- php-coveralls/php-coveralls: ^2.1
- php-ffmpeg/php-ffmpeg: ^0.9.5
- phpunit/phpunit: ^7.0|^8.0
Suggests
- league/flysystem-aws-s3-v3: Required for AWS S3 file storage
- php-ffmpeg/php-ffmpeg: Required for video conversion and thumbnail generation
- spatie/image-optimizer: Required to use the image optimizer variant strategy
README
文件处理和存储助手
处理上传、操作和(外部)存储
变更日志
使用方法
该软件包与框架无关。
以下是一个设置通用变种处理的示例
<?php // Set up a storage implementation (for your framework of choice), and a PSR-11 container implementation. /** @var \Czim\FileHandling\Contracts\Storage\StorageInterface $storage */ /** @var \Psr\Container\ContainerInterface $container */ $sourcePath = 'storage/input-file-name.jpg'; // Source File $helper = new \Czim\FileHandling\Support\Content\MimeTypeHelper; $interpreter = new \Czim\FileHandling\Support\Content\UploadedContentInterpreter; $downloader = new \Czim\FileHandling\Support\Download\UrlDownloader($helper); $factory = new \Czim\FileHandling\Storage\File\StorableFileFactory($helper, $interpreter, $downloader); $file = $factory->makeFromLocalPath($sourcePath); // Handler $strategyFactory = new \Czim\FileHandling\Variant\VariantStrategyFactory($container); $strategyFactory->setConfig([ 'aliases' => [ 'resize' => \Czim\FileHandling\Variant\Strategies\ImageResizeStrategy::class, 'autoOrient' => \Czim\FileHandling\Variant\Strategies\ImageAutoOrientStrategy::class, ], ]); $processor = new \Czim\FileHandling\Variant\VariantProcessor($factory, $strategyFactory); $handler = new \Czim\FileHandling\Handler\FileHandler($storage, $processor); $handler->process($file, new Target($file->path()), [ 'variants' => [ 'tiny' => [ 'autoOrient' => [], 'resize' => [ 'dimensions' => '30x30', ], ], 'orient' => [ 'autoOrient' => [ 'quiet' => false, ], ], ], ]);
对于Laravel,你可以使用以下特定框架的存储实现
<?php // Storage $storage = new \Czim\FileHandling\Storage\Laravel\LaravelStorage( \Storage::disk('testing'), true, url('testing') ); // If you're using a Laravel version that does not have a PSR-11 compliant container yet: $container = new \Czim\FileHandling\Support\Container\LaravelContainerDecorator(app()); app()->bind(\Imagine\Image\ImagineInterface::class, \Imagine\Gd\Imagine::class);
当然,建议使用你框架的依赖注入/ IoC 解决方案来简化上述方法。
自定义容器
如果你没有可用的PSR-11容器,可以使用该软件包提供的非常简单的实现。
<?php $container = new \Czim\FileHandling\Support\Container\SimpleContainer; $container->registerInstance( \Czim\FileHandling\Variant\Strategies\ImageResizeStrategy::class, new \Czim\FileHandling\Variant\Strategies\ImageResizeStrategy( new \Czim\FileHandling\Support\Image\Resizer( new \Imagine\Gd\Imagine ) ) ); $container->registerInstance( \Czim\FileHandling\Variant\Strategies\ImageAutoOrientStrategy::class, new \Czim\FileHandling\Variant\Strategies\ImageAutoOrientStrategy( new \Czim\FileHandling\Support\Image\OrientationFixer(new \Imagine\Gd\Imagine) ) );
存储
可以使用可定制的存储实现来存储文件。
提供了一个非常简单的Laravel存储适配器/装饰器。对于任何其他框架/设置,你将(目前)需要编写自己的实现,以实现 \Czim\FileHandling\Contracts\Storage\StorageInterface。
变种
处理文件时,可以自动创建并存储变种,与原始文件一起。
这些可以是原始图像的缩放、裁剪或重新着色。该软件包已设置,允许你轻松创建自己的变种策略。
单个变种由一个或多个策略步骤定义,这使得组合效果和重用策略成为可能。
可以从原始文件重新创建变种。
请注意,该软件包旨在轻松创建和添加自定义变种策略。以下策略的源代码可作为示例入门。
图像策略
包含的图像处理策略
-
ImageAutoOrientStrategy
:重新定位旋转或翻转的图像。 -
ImageResizeStrategy
:调整(和裁剪)图像大小。
(使用 Stapler 的选项和调整方法。) -
ImageWatermarkStrategy
:将水印粘贴到图像上。
任何角落或中心。
选项
position
(字符串):top-left
、top-right
、center
、bottom-left
、bottom-right
。
watermark
(字符串):水印图像的完整路径。
水印应使用 PNG(透明)图像,以获得最佳效果。 -
ImageOptimizationStrategy
:优化图像以减小文件大小。此策略需要安装 spatie/image-optimizer。
要使其工作,你需要安装一些图像优化器
jpegoptim
optipng
pngquant
svgo
gifsicle
Ubuntu的安装示例
sudo apt-get install jpegoptim optipng pngquant gifsicle sudo npm install -g svgo
MacOS的安装示例,使用 Homebrew
brew install jpegoptim optipng pngquant svgo gifsicle
视频策略
包含的视频处理策略
VideoScreenshotStrategy
:提取视频帧以进行预览。
(需要ffmpeg
/ffprobe
。)
选项
seconds
(整数):在视频运行时间的第几秒提取画面。
percentage
(整数):在视频运行时间的第几百分比提取画面(由seconds
覆盖)。ffmpeg
(字符串):ffmpeg 二进制文件的路径(如果不是/usr/bin/ffmpeg
)。ffprobe
(字符串):ffprobe 二进制文件的路径(如果不是/usr/bin/ffprobe
)。
变种注意事项
当处理可能包含EXIF旋转的图像时,使用调整大小的策略,请注意,仅调整宽高比可能遇到麻烦,除非它们先自动定位。
因此,建议在ImageResizeStrategy
之前使用ImageAutoOrientStrategy
。
通常来说,考虑策略应用的顺序总是一个好主意。
如果效率很重要,您还可以选择将多个策略组合成一个策略类。您可以使用这种方法来避免打开/保存同一文件多次。
配置
文件处理配置是通过向FileHandler注入具有树状结构的关联数组来设置的。
贡献
有关详细信息,请参阅CONTRIBUTING。
致谢
许可协议
MIT许可协议(MIT)。有关更多信息,请参阅许可文件。