deanar / yii2-file-processor
为 Yii2 上传和处理文件和图片
Requires
- php: >=5.4.0
- bower-asset/jcrop: *
- rubaxa/fileapi: *
- yiisoft/yii2: *
- yiisoft/yii2-imagine: *
This package is not auto-updated.
Last update: 2024-09-28 14:58:10 UTC
README
上传和处理文件和图片。
基于 jquery.fileapi 链接到 github
安装
通过 composer 安装此扩展是首选方式。
运行
php composer.phar require --prefer-dist deanar/yii2-file-processor:"0.1.*"
或添加
"deanar/yii2-file-processor": "0.1.*"
到你的 composer.json
文件的 require 部分并更新 composer 依赖项;
如果安装失败,请尝试使用最小稳定性:dev。
然后运行迁移
./yii migrate/up --migrationPath=@deanar/fileProcessor/migrations
将模块包含到你的 web 配置中
'modules' => [ 'fp' => [ 'class' => 'deanar\fileProcessor\Module', //'image_driver' => \deanar\fileProcessor\models\Uploads::IMAGE_DRIVER_GD, 'variations_config' => require(__DIR__ . '/file_processor_variations.php'), //'root_path' => '@frontend/web', // default: @webroot //'root_url' => 'http://front.example.com', // default: current host (Yii::$app->request->getHostInfo()) 'upload_dir' => 'uploads', //'default_quality' => 95, //'default_resize_mod' => 'outbound', //'unlink_files' => true, //'debug' => true, // FileAPI debug. false by default ], ]
将行为附加到你的模型中
public function behaviors() { return [ 'fileSequence' => [ 'class' => \deanar\fileProcessor\behaviours\ConnectFileSequence::className(), 'defaultType' => 'projects', 'registeredTypes' => ['projects', 'files'], // or 'projects, files' as string ] ]; }
在配置目录中创建 file_processor_variations.php
文件并配置图片变体,例如
use deanar\fileProcessor\components\WatermarkFilter; return [ 'projects' => [ '_original' => false, 'thumb' => [200, 150, 'inset'], 'small' => [300, 200, 'outbound', 75], 'big' => [ 'width' => 600, 'height' => 350, 'mode' => 'outbound', 'quality' => 75, 'watermark' => [ 'path' => 'watermark.png', 'position' => WatermarkFilter::WM_POSITION_BOTTOM_RIGHT, 'margin' => 10, ] ], ], 'article_header' => [ '_original' => true, 'thumb' => [200, 150, 'inset'], ], 'avatar_picture' => [ '_original' => true, 'preview' => [200, 200, 'outbound'], // For single file uploads. Automatically will be updated 'avatar' attribute in 'Project' model // with <id> of currently uploaded file '_insert' => ['app\models\Project' => 'avatar'], // variants of access control definitions '_acl' => '*', // * - all users, like without _acl '_acl' => '@', // @ - authenticated users only '_acl' => ['users' => ['admin', 'user1']], // defined list of users '_acl' => ['app\models\Project' => 'user_id'], // if current user id equals to `user_id` attribute of model `app\models\Project` '_acl' => function ($type_id, $user_id) { // callable check return \app\models\Project::findOne($type_id)->user_id == $user_id; }, ], // Used if no variation with specified name found '_default' => [ ], // Mixin for all variations. Used by merging arrays. '_all' => [ ], ];
注意! 不要忘记在你的上传目录中禁用 php 执行。例如:如果你使用 Apache 服务器,你可以在上传目录的根目录中创建 .htaccess
文件,并在其中包含以下代码
RemoveHandler .php
AddType text/html .php
升级说明
运行迁移
./yii migrate/up --migrationPath=@deanar/fileProcessor/migrations
在 ConnectFileSequence 行为中,将 deleteTypes
属性更改为 registeredTypes
。
用法
一旦安装了此扩展,只需将其添加到你的表单中即可,只需在视图添加小部件代码
多文件上传小部件
<?= \deanar\fileProcessor\widgets\MultiUploadWidget::widget([ 'type' => 'projects', 'type_id' => $model->id, 'options' => [ 'autoUpload' => true, 'multiple' => true, 'accept' => 'image/*,application/zip', 'duplicate' => false, 'maxSize' => '2M', // you can use 'M', 'K', 'G' or simple size in bytes 'maxFiles' => 3, 'imageSize' => [ 'minWidth' => 150, 'maxWidth' => 2000, 'minHeight' => 150, 'maxHeight' => 2000, ], ], 'htmlOptions' => [ 'class' => 'additional-class', 'data-attribute' => 'value', ], ]) ?>
单文件上传小部件
<?= \deanar\fileProcessor\widgets\SingleUploadWidget::widget([ 'type' => 'projects', 'type_id' => $model->id, 'crop' => true, 'preview' => true, 'previewSize' => [200,200], 'options' => [ 'accept' => 'image/*', 'maxSize' => '2M', // you can use 'M', 'K', 'G' or simple size in bytes 'imageSize' => [ 'minWidth' => 150, 'maxWidth' => 2000, 'minHeight' => 150, 'maxHeight' => 2000, ], ], 'htmlOptions' => [ 'class' => 'additional-class', 'data-attribute' => 'value', ], ]) ?>
如果 preview
设置为 false
,则 crop
自动设置为 false
,将是一个非常简单的上传小部件。如果 crop
设置为 true
,则 accept
选项自动设置为 'image/*'
。对于不裁剪的单文件上传,autoUpload
自动设置为 true
。
要设置窗口大小和裁剪区域的最小大小,请使用 previewSize
属性。默认为 [200,200]
。
imageAutoOrientation
选项默认设置为 false
你可以通过以下方式访问你的图片/文件
$model = ExampleModel::findOne(1); $uploads = $model->getFiles(); foreach($uploads as $u){ echo $u->imgTag('thumb2', true,['style'=>'border:1px solid red;']); //or just url (for files/download links) echo \yii\helpers\Html::a($u->original, $u->getPublicFileUrl('original', true)); }
你可以这样过滤文件
$uploads = $model->imagesOnly()->getFiles(); // or $uploads = $model->filesOnly()->getFiles();
你可以获取行中的第一个文件
$uploads = $model->getFirstFile();
你可以在 GridView
中显示你的图片/文件。
在列列表中添加
[ 'class' => 'deanar\fileProcessor\components\ImageColumn', 'header' => 'Image', // optional 'empty' => 'No Image', // optional 'type' => 'projects', // optional, default value goes from behavior options 'variation' => '_thumb', 'htmlOptions' => [], // optional ],
你可以通过 DisplayWidget
在任何地方显示你的图片/文件列表,例如在 DetailView
小部件或只是视图。
与 DetailView
有关的案例
'attributes' => [ 'id', 'title', ... [ 'attribute'=>'Images', 'value'=>\deanar\fileProcessor\widgets\DisplayWidget::widget(['type'=>'projects','type_id'=>$model->id,'variation'=>'_thumb']), 'format'=>'raw', ], ... 'text', ],
DisplayWidget 的所有属性都是必需的。
待办事项和进度
- 用于单文件上传的特殊小部件 [*****]
- 访问控制系统 [*****]
- 国际化(EN + RU) [*****]
- 更多自定义 [**---]
- 裁剪和 jQuery.fileapi 的其他功能 [****-]
- 通过 URL 或路径上传文件的 API [-----]
- 生成新图像变体的控制台命令 [-----]
- 查看和编辑所有上传文件的管理员界面 [-----]
- 动态生成图像变体的模式(按请求) [-----]
- 后台生成图像变体的模式 [-----]
- 高级变体功能:水印、裁剪、旋转等 [***--]
- 漂亮的警报(例如 http://rubaxa.github.io/Ply/) [-----]
- 重构 [*----]
变更日志
=======
0.1.4 (2016-06-05)
- 土耳其语支持(感谢 https://github.com/fg)
0.1.3 (2016-05-19)
- 在控制台中保存模型的可能性
0.1.2 (2015-08-18)
- 错误:多个带有裁剪的单文件上传小部件
- 错误:删除单文件上传小部件中的图片预览
0.1.1 (2015-07-16)
imageAutoOrientation
选项默认设置为false
0.1.0 (2015-03-10)
- 第一个标记版本。