coderius/yii2-upload-file-behavior

yii2 点击计数器

v1.0 2020-08-26 20:59 UTC

This package is auto-updated.

Last update: 2024-09-29 05:55:50 UTC


README

Software License Latest Version Code Coverage Code Intelligence Status Code Quality Build Status Total Downloads

关于

Yii2 上传文件行为 - 简单地将图片和文件上传到服务器的简单方法。不再需要在控制器中编写大量代码,并通过数小时进行测试。结果 - 节省上传文件到网站的时间和劳动力成本。只需从 GitHub 上上传扩展包,并将一些代码粘贴到需要的模型类(\yii\db\ActiveRecord)中,即可处理文件上传。更多内容请见下文。

安装

安装此扩展的首选方式是通过 composer

首先下载扩展。在终端中运行以下命令

composer require "coderius/yii2-upload-file-behavior"

或在 composer.json 中添加

"coderius/yii2-upload-file-behavior": "^1.0"

然后运行 composer update

用法

此扩展是为在 \yii\db\ActiveRecord 模型类中使用而创建的。

配置行为。

  • $nameOfAttributeFile = (string) 默认名称 'file'。从文件系统中上传文件实例的虚拟属性。
  • $nameOfAttributeStorage = (string) 默认名称 'face_img'。在数据库中保存上传文件路径的属性。
  • $newFileName = (string) 分配给上传文件的名称
  • $directories = (array) 上传文件夹和上传处理器的配置。由单独的数组组成。每个数组包含目标文件夹路径和上传文件到此文件夹的处理器的设置,如 'path' 和 'handler' -'path' - 包含目标文件夹的路径 -'handler' - 处理下载的文件并将其保存到在参数 'path' 中指定的位置。

此扩展是为在 \yii\db\ActiveRecord 模型类中使用而创建的。

  • 因此,首先在模型类中放置到 yii2-upload-file-behavior 的命名空间。
  • 创建公共变量 $file 以从文件系统中加载文件。
  • 数据库必须有一个用于存储文件路径的属性。以下示例中的是 'img_src' 属性(在公共函数 rules() 中标记为保存)
  • 然后像示例一样粘贴需要的配置 behaviors() 方法。

注意。 不要忘记包含依赖项的命名空间。

示例

    namespase your/models;

    use coderius\yii2UploadFileBehavior\UploadFileBehavior;
    use yii\imagine\Image;
    use Imagine\Image\Point;
    use Imagine\Image\Box;

    class YourModel extends \yii\db\ActiveRecord
    {
        public $file;

        //'img_src' - attribute to save path to file in db
        public function rules()
        {
            return [
                [['img_src'], 'safe'],
        }

        ...

            public function behaviors()
            {
                return [
                    //Another behaviors
                    //...

                    'uploadFileBehavior' => [
                        'class' => UploadFileBehavior::className(),
                        'nameOfAttributeStorage' => 'img_src',
                        'directories' => [
                            
                            [
                                'path' => function($attributes){
                                    return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/big/');
                                },
                                'hendler' => function($fileTempName, $newFilePath){
                                    Image::thumbnail($fileTempName, 900, 900*2/3)
                                    ->copy()
                                    ->crop(new Point(0, 0), new Box(900, 900*2/3))
                                    ->save($newFilePath, ['quality' => 80]);
                                    sleep(1);
                                }
                            ],
                            [
                                'path' => function($attributes){
                                    return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/middle/');
                                },
                                'hendler' => function($fileTempName, $newFilePath){
                                    Image::thumbnail($fileTempName, 400, 400*2/3)
                                    ->save($newFilePath, ['quality' => 80]);
                                    sleep(1);
                                }
                            ],
                            [
                                'path' => function($attributes){
                                    return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/small/');
                                },
                                'hendler' => function($fileTempName, $newFilePath){
                                    Image::thumbnail($fileTempName, 150, 150*2/3)
                                    ->save($newFilePath, ['quality' => 80]);
                                    sleep(1);
                                }
                            ],
                        ]
                    ],

                ];
            }

        ...
    }    

简短的简单配置

'uploadFileBehavior' => [
    'class' => UploadFileBehavior::className(),
    'nameOfAttributeStorage' => 'img_src',
    'newFileName' => 'image-123',
    'targets' => [
        [
            'path' => '@uploadsPath',
            'hendler' => [
                'type' => UploadFileBehavior::TYPE_IMAGE,
                'config' => [
                    'size' => [
                        'width' => 400,
                        'height'=> 400*2/3
                    ],
                    'quality' => 80
                ]
            ]
        ]
    ]
]        

在这种情况下,允许的参数是:'size' & 'quality'。

附加操作

  1. 为保存上传文件的目标文件夹创建别名。
  2. 在 'frontend/web' 目录中创建目标文件夹,如下例所示。
  3. 不要忘记创建虚拟属性。如果其名称为 '$file',则不需要设置 'nameOfAttributeFile'(默认为 (string)'file')的配置。

测试

在扩展文件夹中运行测试。

$ ./vendor/bin/phpunit

注意!要运行所有测试,需要通过 composer 上传所有依赖项。如果测试单个扩展,则从扩展所在的根目录运行命令

composer update

当所有依赖项下载完毕后,在终端中从根文件夹运行所有测试

./vendor/bin/phpunit tests

或仅单元测试

./vendor/bin/phpunit --testsuite Unit

如果扩展在应用中进行测试,则设置正确的 phpunit 路径并运行一些命令。

致谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可文件