coderius/yii2-upload-file-behavior

yii2 hit counter

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

This package is auto-updated.

Last update: 2024-03-29 04:32:43 UTC


README

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

About

Yii2 upload file behavior - simple wey to upload images and files to server. No need anymore wrote tonn of code in controller and else testing it by houers. As a result - saving time and labor costs for uploading files to the site. Only needed upload extention from github and past some less code to model class (\yii\db\ActiveRecord) where needed hendler uploading files. More on this below.

Installation

The preferred way to install this extension is through composer.

First download extention. Run the command in the terminal:

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

or add in composer.json

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

and run composer update

Usage

This extention created for usage in \yii\db\ActiveRecord model classes.

Configyration behavior.

  • $nameOfAttributeFile = (string) default name 'file'. Virtual attribute for uploading file instance from file systrem.
  • $nameOfAttributeStorage = (string) default name 'face_img'. Attribute for saving path to uploaded file in db.
  • $newFileName = (string) name which is assigned to uploaded file
  • $directories = (array) configs to upload folder and upload hendlers.Сonsists of separate arrays. Each array contains settings for the path to the target folder and a handler for uploading files to this folder like 'path' and 'handler' -'path' - contains path to target folder -'hendler' - Processes the downloaded file and saves to the specified in param 'path' location.

This extention created for usage in \yii\db\ActiveRecord model classes.

  • So, first in model class put namespace to yii2-upload-file-behavior.
  • Create public variable $file for loading file from filesystem.
  • The database must have an attribute to store the file path. In example below it is 'img_src' attribute (marked like save in public function rules())
  • Then past needed configs behaviors() method like in example.

!Note. Don't forget to include the dependency namespaces.

Example

    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);
                                }
                            ],
                        ]
                    ],

                ];
            }

        ...
    }    

Short simple config:

'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
                ]
            ]
        ]
    ]
]        

In this case allowed params is: 'size' & 'quality'.

Additional actions:

  1. Create aliases to target folders for saving uploaded files.
  2. Create target folders in 'frontend/web' dirrectory like example.
  3. Don't forget create vertual attribute. If it named like '$file', then no need set config to 'nameOfAttributeFile'(default = (string)'file').

Testing

Run tests in extention folder.

$ ./vendor/bin/phpunit

Note! For running all tests needed upload all dependencies by composer. If tested single extention, then run command from root directory where located extention:

composer update

When all dependencies downloaded run all tests in terminal from root folder:

./vendor/bin/phpunit tests

Or for only unit:

./vendor/bin/phpunit --testsuite Unit

If extention tested in app, then set correct path to phpunit and run some commands.

Credits

License

The MIT License (MIT). Please see License File for more information.