sergmoro1/yii2-byone-uploader

文件上传小部件。使用 Blueimp 和 JCrop jQuery 插件。易于设置,快速上手。

此包的官方仓库似乎已不存在,因此该包已被冻结。

安装: 182

依赖: 0

建议者: 0

安全: 0

星标: 3

关注者: 1

分支: 1

开放问题: 0

类型:yii2-extension

dev-master 2019-07-22 12:33 UTC

This package is auto-updated.

Last update: 2019-12-22 13:17:10 UTC


README

不再维护,请使用下一个版本 sergmoro1\yii2-uploader

演示

按类别查看照片。(当编辑时)
照片库 "之前" & "之后"(类似)。

优点

如果模型需要文件,您不需要

  • 在模型中定义 file 属性,
  • 处理表单填充的结果,
  • 想出保存文件的地方,

您可以在

  • 文件上添加描述,
  • 删除不需要的文件,
  • 定义压缩图像的大小,
  • 裁剪图像,
  • 通过鼠标交换文件行。

如何制作?

所有上传文件的有关信息都存储在一个表格中 - onefile。不需要在需要文件的模型中定义类型为 file 的字段。

文件被上传并存储在目录 frontend/web/files 中。对于每个模型,可以可能有子目录: frontend/web/files/userfrontend/web/files/post。在子目录中,文件按用户(或帖子)和大小排列

frontend/web/files/user/2
frontend/web/files/user/2/thumb
frontend/web/files/user/2/original

可以通过拖动鼠标行来排序上传文件行。

示例

用户必须可以上传照片。需要在模型 common/models/User 中定义
...
use sergmoro1\uploader\FilePath;
use sergmoro1\uploader\models\OneFile;
...

class User extends ActiveRecord implements IdentityInterface
{
   ...
  // Images sizes
  public $sizes = [
    // Catalog original should be define for cropping
    'original' => ['width' => 1600, 'height' => 900, 'catalog' => 'original'],
    'main' => ['width' => 400, 'height' => 400, 'catalog' => ''],
    'thumb' => ['width' => 90, 'height' => 90, 'catalog' => 'thumb'],
  ];
  // Get ref to the file, make dir and so
  public function behaviors()
  {
    return [
      'FilePath' => [
        'class' => FilePath::className(),
        'file_path' => '/files/user/',
      ]
    ];
  }
  // All files for User model
  public function getFiles()
  {
    return OneFile::find()
      ->where('parent_id=:parent_id AND model=:model', [
        ':parent_id' => $this->id,
        ':model' => 'common\models\User',
       ])
      // only if rows should be draggable & sortable
      ->orderBy('created_at')
      ->all();
  }

  ...

现在用户可以上传,文件将保存在

  frontend/web/files/user/user_id
  frontend/web/files/user/user_id/original
  frontend/web/files/user/user_id/thumb

因此,大小将按指定的方式修改。

要执行上传,需要将小部件放置在表单或任何其他视图中。例如,在 backend/views/user/_form.php

use sergmoro1\uploader\widgets\Byone;
...

  <?= Byone::widget([
    'model' => $model,
    'cropAllowed' => true,
  ]) ?>

如果需要裁剪图像(cropAllowed = true),则需要定义子目录 original。裁剪的纵横比在 main 子目录中设置。默认情况下,只有 main 图像将被裁剪,但您可以通过定义参数 crop 来指定任何其他目录(除了 original)。

  public $sizes = [
    'original' => ['width' => 3600, 'height' => 2400, 'catalog' => 'original'],
    'main' => ['width' => 400, 'height' => 400, 'catalog' => ''],
    'thumb' => ['width' => 120, 'height' => 80, 'catalog' => 'thumb', 'crop' => true],
  ];

可以上传任何数量的文件,但文件数量可以通过 maxFiles 限制。

安装

在应用程序目录中

$ composer require sergmoro1/yii2-byone-uploader "dev-master"

运行迁移

$ php yii migrate --migrationPath=@vendor/sergmoro1/yii2-byone-uploader/migrations

要在应用程序中注册模块 - common/config/main.php

  'modules' => [
    'uploader' => [
      'class' => 'sergmoro1\uploader\Module',
  ],

如果使用 advanced 模板,则应定义 before_web 参数。例如 backend\config\params.php

<?php
return [
    'before_web' => 'backend',
    ...
];

对于 frontendbasic 模板则不需要。

上传文件的描述

您可以为文件留下评论。为此,在表单 backend/views/user/_form.php 中,在已提到的小部件中,需要添加参数 appendeixView

...
  <?= Byone::widget([
    'model' => $model,
    'appendixView' => '/user/appendix',
    'cropAllowed' => true,
  ]) ?>

并添加视图,例如 backend/views/user/appendix.php 以下内容

<span id='description'>
    <?php echo isset($file->vars->description) ? $file->vars->description : ''; ?>
</span>

默认定义了 description 字段,但字段不限。

选项

cropAllowed (false) 如果需要裁剪图片,必须在模型的 $sizes 数组中定义 'original' 大小。

draggable (false) 如果需要交换上传的文件,则必须在获取器 getFiles() 中按 created_at 排序行。

  public function getFiles()
  {
    return OneFile::find()
      ->where('parent_id=:parent_id AND model=:model', [
        ':parent_id' => $this->id,
        ':model' => 'common\models\YourModel',
       ])
      ->orderBy('created_at')
      ->all();
  }

acceptFileTypes ('image\/[jpeg|jpg|png|gif]')

minFileSize (0.1Mb)

maxFileSize (2Mb)

maxFiles (0 - 任意数量)

secure (true) 普通扩展名需要用户授权,但验证可能被关闭。