czim/file-handling
Composer 安装命令:
composer require czim/file-handling
包简介
File Handling Helper
README 文档
README
File Handling and Storage Helper
Handles uploads, manipulations and (external) storage
Changelog
Usage
This package is framework-independent.
Here's an example of how to set up variant processing in general:
<?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); $validator = new \Czim\FileHandling\Support\Download\UriValidator(); $factory = new \Czim\FileHandling\Storage\File\StorableFileFactory( $helper, $interpreter, $downloader, $validator ); $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, ], ], ], ]);
For Laravel, you could use the following framework specific storage implementation:
<?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);
It is recommended of course to use the dependency container / IoC solution of your framework to simplify the above approach.
Custom Container
If you don't have a feasible PSR-11 container available, you can use a very simple implementation provided with this package.
<?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) ) );
Storage
Files can be stored using customizable storage implementations.
A very simple adapter/decorator for the Laravel storage is provided.
For any other framework/setup you will (for now) have to write your own implementation of the \Czim\FileHandling\Contracts\Storage\StorageInterface.
Variants
When a file is processed, variants can be created automatically and stored along with the original.
These can be resizes, crops or recolors of the original image. This package is set up to allow you to easily create your own strategies for making variants.
A single variant is defined by one or more strategy steps, making it possible to combine effects and re-use strategies.
Variants can be re-created from the original.
Note that it this package is designed to easily create and add in custom variant strategies. Consider the source code for the strategies listed below examples to get you started.
Image Strategies
Included strategies for image manipulation:
-
ImageAutoOrientStrategy: Re-orients rotated or flipped images. -
ImageResizeStrategy: Resizes (and crops) images.
(Uses Stapler's options & approach to resizes.) -
ImageWatermarkStrategy: Pastes a watermark onto an image.
In any corner or the center.
Options:
position(string):top-left,top-right,center,bottom-left,bottom-right.
watermark(string): full path to the watermark image.
The watermark should be a PNG (transparent) image for best results. -
ImageOptimizationStrategyOptimizes images to decreate their file size.This strategy requires installation of spatie/image-optimizer.
In order for it to work, you'll need to install a few image optimizers as well:
jpegoptimoptipngpngquantsvgogifsicle
Installation example for Ubuntu:
sudo apt-get install jpegoptim optipng pngquant gifsicle sudo npm install -g svgo
Installation example for MacOS, using Homebrew:
brew install jpegoptim optipng pngquant svgo gifsicle
Video Strategies
Included strategies for video manipulation:
VideoScreenshotStrategy: Extracts a video frame for a preview.
(Requiresffmpeg/ffprobe).
Options:
seconds(int): the second of video runtime to take the shot at.
percentage(int): the percentage of video runtime to take the shot at (overruled byseconds).ffmpeg(string): path to ffmpeg binary (if not/usr/bin/ffmpeg).ffprobe(string): path to ffprobe binary (if not/usr/bin/ffprobe).
Variant Gotcha
When using the resize strategy while working with potentially EXIF-rotated images, keep in mind that portrait/landscape width/height only resizes may run into trouble unless they are auto-oriented first.
For this reason, it is recommended to precede the ImageResizeStrategy by the ImageAutoOrientStrategy.
In general, it is always a good idea to consider the order in which strategies are applied.
You may also opt to combine multiple strategies into one strategy class, if efficiency is important. You may use this approach to prevent opening/saving the same file more than once.
Handling URI content
In previous versions, this package automatically retrieved content from raw string content with URIs to local or remote files. This has now been disabled by default to minimize security risks.
For more information on this change, see SECURITY for more information.
Configuration
Configuration of file handling is set by injecting an associative array with a tree structure into the FileHandler.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
czim/file-handling 适用场景与选型建议
czim/file-handling 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 840.86k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2017 年 08 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「file」 「storage」 「s3」 「uploads」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 czim/file-handling 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 czim/file-handling 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 czim/file-handling 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A SDK for working with B2 cloud storage.
Small library to access Microsoft Windows Azure Blob Storage with a Service or a StreamWrapper.
Pop Http Component for Pop PHP Framework
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
A very simple yet useful helper class to handle PHP file uploads.
Wordpress S3 Uploads Drop In
统计信息
- 总下载量: 840.86k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 14
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2017-08-05