PHP code example of coderius / yii2-upload-file-behavior

1. Go to this page and download the library: Download coderius/yii2-upload-file-behavior library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

coderius / yii2-upload-file-behavior example snippets


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

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

                ];
            }

        ...
    }