2017 © Pedro Peláez
 

yii2-extension yii2-upload-behavior

Yii2 file/image upload behavior for ActiveRecord

image

bajadev/yii2-upload-behavior

Yii2 file/image upload behavior for ActiveRecord

  • Monday, January 30, 2017
  • by boliver20
  • Repository
  • 2 Watchers
  • 0 Stars
  • 673 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 32 Forks
  • 0 Open issues
  • 8 Versions
  • 4 % Grown

The README.md

Yii2 file/image upload behavior for ActiveRecord

This package is the set of two similar behaviors. The first one allows you to keep the uploaded file as-is. And the second one allows you to generate set of thumbnails for the uploaded image. Behaviors could be attached multiple times for different attributes., (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

composer bajadev/yii2-upload-behavior "*"

or add, (*4)

"bajadev/yii2-upload-behavior": "*"

to the require section of your composer.json., (*5)

FileUploadBehavior

This behavior allow you to add file uploading logic with ActiveRecord behavior., (*6)

Usage

Attach the behavior to your model class:, (*7)

public function behaviors()
{
    return [
        [
            'class' => '\bajadev\upload\FileUploadBehavior',
            'attribute' => 'fileUpload',
            'filePath' => '@webroot/uploads/[[pk]].[[extension]]',
            'fileUrl' => '/uploads/[[pk]].[[extension]]',
        ],
    ];
}

You can use Yii path aliases like @app, @webroot, @web in your path template configuration., (*8)

You can also use additional placeholders in path templates. Placeholder [[foobar]] will be replaced with appropriate placeholder value., (*9)

Possible path/url placeholders:, (*10)

  • [[model]] - model class name
  • [[pk]] - value of the primary key
  • [[id]] - the same as [[pk]]
  • [[attribute_name]] - attribute value, for example [[attribute_ownerId]]
  • [[slug_attribute_name]] - generate attribute value by Inflector, for example [[attribute_ownerId]]
  • [[md5_attribute_name]] - generate md5 value, for example [[attribute_ownerId]]
  • [[id_path]] - id subdirectories structure (if model primary key is 12345, placeholder value will be 1/2/3/4/5
  • [[basename]] - original filename with extension
  • [[filename]] - original filename without extension
  • [[extension]] - original extension

Add validation rule:, (*11)

public function rules()
{
    return [
        ['fileUpload', 'file'],   
    ];
}

Setup proper form enctype:, (*12)

$form = \yii\bootstrap\ActiveForm::begin([
    'enableClientValidation' => false,
    'options' => [
        'enctype' => 'multipart/form-data',
    ],
]);

File should be uploading fine., (*13)

You can get uploaded file url using model call:, (*14)

echo $model->getUploadedFileUrl('fileUpload');

ImageUploadBehavior

Image upload behavior extends file upload behavior with image thumbnails generation. You can configure set of different thumbnail profiles to generate., (*15)

Usage

Attach the behavior to your model class:, (*16)

public function behaviors()
{
    return [
        [
             'class' => '\bajadev\upload\ImageUploadBehavior',
             'attribute' => 'imageUpload',
             'thumbs' => [
                 'thumb' => ['width' => 400, 'height' => 300, 'crop' => true, 'quality' => 80, 'inset' => true],
             ],
             'deleteOriginalFile' => true,
             'rotateImageByExif' => true,
             'filePath' => '@webroot/images/[[pk]].[[extension]]',
             'fileUrl' => '@web/images/[[pk]].[[extension]]',
             'thumbPath' => '@webroot/images/[[profile]]_[[pk]].[[extension]]',
             'thumbUrl' => '@web/images/[[profile]]_[[pk]].[[extension]]',
        ],
    ];
}

You can use Yii path aliases like @app, @webroot, @web in your path template configuration., (*17)

You can also use additional placeholders in path templates. Placeholder [[foobar]] will be replaced with appropriate placeholder value., (*18)

Possible path/url placeholders:, (*19)

  • [[model]] - model class name
  • [[pk]] - value of the primary key
  • [[id]] - the same as [[pk]]
  • [[attribute_name]] - attribute value, for example [[attribute_ownerId]]
  • [[slug_attribute_name]] - generate attribute value by Inflector, for example [[attribute_ownerId]]
  • [[md5_attribute_name]] - generate md5 value, for example [[attribute_ownerId]]
  • [[id_path]] - id subdirectories structure (if model primary key is 12345, placeholder value will be 1/2/3/4/5/
  • [[basename]] - original filename with extension
  • [[filename]] - original filename without extension
  • [[extension]] - original extension
  • [[profile]] - thumbnail profile name, use it in thumbnail path/url

Add validation rule:, (*20)

public function rules()
{
    return [
        ['imageUpload', 'file', 'extensions' => 'jpeg, gif, png'],   
    ];
}

Setup proper form enctype:, (*21)

$form = \yii\bootstrap\ActiveForm::begin([
    'options' => [
        'enctype' => 'multipart/form-data',
    ],
]);

File should be uploading fine., (*22)

You can get uploaded image url using model call:, (*23)

echo $model->getImageFileUrl('imageUpload');

You can specify default image for models without uploaded image:, (*24)

echo $model->getImageFileUrl('imageUpload', '/images/empty.jpg');

You can also get generated thumbnail image url:, (*25)

echo $model->getThumbFileUrl('imageUpload', 'thumb');

You can specify default thumbnail image for models without uploaded image:, (*26)

echo $model->getThumbFileUrl('imageUpload', 'thumb', '/images/thumb_empty.jpg');

Licence

MIT, (*27)

The Versions

30/01 2017

dev-master

9999999-dev http://bajadev.hu

Yii2 file/image upload behavior for ActiveRecord

  Sources   Download

MIT

The Requires

 

file upload behavior image resize thumb

30/01 2017

2.0.3

2.0.3.0 http://bajadev.hu

Yii2 file/image upload behavior for ActiveRecord

  Sources   Download

MIT

The Requires

 

file upload behavior image resize thumb

30/01 2017

2.0.2

2.0.2.0 http://bajadev.hu

Yii2 file/image upload behavior for ActiveRecord

  Sources   Download

MIT

The Requires

 

file upload behavior image resize thumb

30/01 2017

2.0.1

2.0.1.0 http://bajadev.hu

Yii2 file/image upload behavior for ActiveRecord

  Sources   Download

MIT

The Requires

 

file upload behavior image resize thumb

24/01 2017

2.0

2.0.0.0 http://bajadev.hu

Yii2 file/image upload behavior for ActiveRecord

  Sources   Download

MIT

The Requires

 

file upload behavior image resize thumb

25/03 2016

1.1.1

1.1.1.0 http://yiidreamteam.com/yii2/upload-behavior

Yii2 file/image upload behavior for ActiveRecord

  Sources   Download

MIT

The Requires

 

file upload behavior image resize thumb

13/02 2016

1.1.0

1.1.0.0 http://yiidreamteam.com/yii2/upload-behavior

Yii2 file/image upload behavior for ActiveRecord

  Sources   Download

MIT

The Requires

 

file upload behavior image resize thumb

13/02 2015

1.0.0

1.0.0.0 http://yiidreamteam.com/yii2/upload-behavior

Yii2 file/image upload behavior for ActiveRecord

  Sources   Download

MIT

The Requires

 

file upload behavior image resize thumb