marten-cz / upload-manager
Nette 框架的上传管理器
Requires
- php: >=5.6.0
- ext-gd: *
- aws/aws-sdk-php: ~3.19
- guzzlehttp/guzzle: ~6.3.0
- nette/bootstrap: ~2.3.0|~2.4.0|^3.0.0
- nette/di: ~2.3.0|~2.4.0|^3.0.0
- nette/finder: ~2.3.0|~2.4.0|~2.5.0
- nette/http: ~2.3.0|~2.4.0|^3.0.0
- nette/utils: ~2.4.0|~2.5.0|^3.0.0
Requires (Dev)
- mockery/mockery: ^0.9.4
- nette/tester: ~1.7.0
Suggests
- ext-exif: to read and keep images EXIF for fixing image orientation
README
Nette 框架的上传管理器
安装
composer.json
"ondrs/upload-manager": "v7.0.0"
配置
注册扩展
extensions: uploadManager: ondrs\UploadManager\DI\Extension
最小配置
uploadManager: relativePath: '/uploads'
完整配置
uploadManager: basePath: %wwwDir% relativePath: '/uploads' fileManager: blacklist: {php} imageManager: maxSize: 1280 type: jpg quality: 80 saveOriginal: FALSE dimensions: 800: - {800, NULL} - shrink_only 500: - {500, NULL} - shrink_only 250: - {250, NULL} - shrink_only thumb: - {100, NULL} - shrink_only
在大多数情况下,您希望选择 wwwDir
作为您的 basePath
(默认选择),以便您的文件公开可用。relativePath
相对于 basePath
,因此文件上传的完整路径如下所示
{basePath}/{relativePath}[/{dir}]
dir
是一个可选参数,您可以在脚本运行时在 listen()
或 upload()
方法中设置它。
文件管理器
- 黑名单
- 黑名单中的文件扩展名数组,默认为 php
图像管理器
-
最大尺寸
- 图像的最大尺寸,如果其更大,则将自动调整大小到该尺寸
- 可以是图像的 X 坐标数字
- 或数组 [X, Y]
-
尺寸
-
要调整大小的图像尺寸数组
-
格式为
PREFIX: - {X_SIZE, Y_SIZE} - RESIZE_OPTION
-
PREFIX
可以是您想要的任何内容,它将被添加到调整大小的文件中:PREFIX_file.jpg
-
Y_SIZE
以及RESIZE_OPTION
都是可选的 -
RESIZE_OPTION
默认设置为Image::SHRINK_ONLY
-
例如,我们将根据上述完整配置设置 UploadManager。
$this->upload->filesToDir('dir/path')
上传一个大小为 (1680 x 1050) 的图像文件 foo.jpg
将创建 5 个文件:foo.jpg, 800_foo.jpg, 500_.jpg, 250_foo.jpg, thumb_foo.jpg
,这些文件将保存在 %wwwDir%/uploads/super/dir
中。所有文件都按比例调整大小,并带有相应的前缀。foo.jpg 被视为原始文件,但调整为 1280px。
AWS S3 支持
设置您的凭证并将您的存储桶名称作为 basePath。就这么简单
uploadManager: basePath: 'your-bucket-name' relativePath: 'some/path/to/dir' s3: region: 'eu-central-1' version: '2006-03-01' credentials: key: 'xxxxxxx' secret: 'xxxxxxx'
就是这么简单!
用法
将 ondrs\UploadManager\Upload
注入到您的 presenter 或您想要的任何地方
/** @var \ondrs\UploadManager\Upload @inject public $upload;
然后进行上传。
public function renderUpload() { $this->upload->filesToDir('path/to/dir'); }
如果您只想上传单个文件(例如使用表单),请调用 singleFileToDir()
方法
public function processForm($form) { /** @var Nette\Http\FileUpload */ $fileUpload = $form->values->file; $this->upload->singleFileToDir('path/to/dir', $fileUpload); }
事件
真正的乐趣在于事件。它们旨在帮助您轻松控制和管理上传过程。
-
onQueueBegin
- 在上传开始前调用
- 接受一个参数
- 一个 Nette\Http\FileUpload 对象数组,这些对象将被上传
-
onQueueComplete
- 上传完成后调用
- 接受两个参数
- Nette\Http\FileUpload 数组
- \SplFileInfo 对象数组,这些对象已被上传
-
onFileBegin
- 在上传每个文件前调用
- 接受两个参数
- Nette\Http\FileUpload
- dir 是按
{relativePath}[/{dir}]
构建的
-
onFileComplete
- 在上传每个文件的完成后调用
- 接受三个参数
- 原始文件的 Nette\Http\FileUpload 对象
- 上传文件的 \SplFileInfo 对象
- dir 是按
{relativePath}[/{dir}]
构建的
现实世界示例
/** * @param int $eventId */ public function uploadAttachment($eventId) { /** * @param FileUpload $fileUpload * @param \SplFileInfo $uploadedFile * @param $path */ $this->upload->onFileComplete[] = function (\SplFileInfo $uploadedFile, FileUpload $fileUpload, $path) use ($eventId) { $filename = $uploadedFile->getFilename(); $this->db->table('crm_attachments') ->insert([ 'filename' => $filename, 'path' => $path, 'crm_events_id' => $eventId, ]); }; /** * @param array $files * @param array $uploadedFiles */ $this->upload->onQueueComplete[] = function(array $files, array $uploadedFiles) use($eventId) { $uploadedFiles = array_map(function($i) { return $i->getFilename(); }, $uploadedFiles); $this->db->table('crm_events') ->wherePrimary($eventId) ->update([ 'text' => implode(';', $uploadedFiles), ]); }; $this->upload->filesToDir('attachments/' . $eventId); }