Why Image Uploading Not Save in Database in Yii2

Yii2 upload file beliefs

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

Nearly

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

Installation

The preferred way to install this extension is through composer.

Commencement download extention. Run the control in the terminal:

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

or add together in composer.json

              "coderius/yii2-upload-file-behavior": "^i.0"                          

and run composer update

Usage

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

Configyration behavior.

  • $nameOfAttributeFile = (string) default proper name 'file'. Virtual attribute for uploading file instance from file systrem.
  • $nameOfAttributeStorage = (string) default proper name 'face_img'. Attribute for saving path to uploaded file in db.
  • $newFileName = (cord) 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 binder 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, showtime in model class put namespace to yii2-upload-file-behavior.
  • Create public variable $file for loading file from filesystem.
  • The database must accept an aspect to store the file path. In example below information technology is 'img_src' attribute (marked like save in public function rules())
  • And then past needed configs behaviors() method like in case.

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

Example

                              namespase your/models;      apply coderius\yii2UploadFileBehavior\UploadFileBehavior;     apply yii\imagine\Image;     employ Imagine\Image\Indicate;     use Imagine\Epitome\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 role behaviors()             {                 render [                     //Another behaviors                     //...                      'uploadFileBehavior' => [                         'form' => UploadFileBehavior::className(),                         'nameOfAttributeStorage' => 'img_src',                         'directories' => [                                                          [                                 'path' => part($attributes){                                     return \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/big/');                                 },                                 'hendler' => office($fileTempName, $newFilePath){                                     Prototype::thumbnail($fileTempName, 900, 900*2/3)                                     ->copy()                                     ->crop(new Point(0, 0), new Box(900, 900*two/3))                                     ->salvage($newFilePath, ['quality' => 80]);                                     sleep(i);                                 }                             ],                             [                                 'path' => function($attributes){                                     render \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/middle/');                                 },                                 'hendler' => office($fileTempName, $newFilePath){                                     Prototype::thumbnail($fileTempName, 400, 400*2/3)                                     ->save($newFilePath, ['quality' => eighty]);                                     sleep(i);                                 }                             ],                             [                                 'path' => function($attributes){                                     render \Yii::getAlias('@portfoleoPhotosPath/' . $attributes['id'] . '/small/');                                 },                                 'hendler' => function($fileTempName, $newFilePath){                                     Paradigm::thumbnail($fileTempName, 150, 150*ii/3)                                     ->salve($newFilePath, ['quality' => 80]);                                     sleep(1);                                 }                             ],                         ]                     ],                  ];             }          ...     }                          

Short unproblematic config:

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

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

Boosted actions:

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

Testing

Run tests in extention folder.

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

When all dependencies downloaded run all tests in concluding from root binder:

              ./vendor/bin/phpunit tests                          

Or for merely unit of measurement:

              ./vendor/bin/phpunit --testsuite Unit of measurement                          

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

Credits

  • Sergio Coderius

License

The MIT License (MIT). Please run across License File for more than information.

williamsdemuchys.blogspot.com

Source: https://github.com/coderius/yii2-upload-file-behavior

0 Response to "Why Image Uploading Not Save in Database in Yii2"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel