2017 © Pedro Peláez
 

yii2-extension yii2-cover-behavior

Yii 2 behavior for image uploading

image

lyhoshva/yii2-cover-behavior

Yii 2 behavior for image uploading

  • Friday, March 17, 2017
  • by lyhoshva
  • Repository
  • 2 Watchers
  • 1 Stars
  • 300 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 11 Versions
  • 5 % Grown

The README.md

yii2-cover-behavior

Yii 2 behavior for image uploading, (*1)

Installation

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

Either run, (*3)

composer require --prefer-dist lyhoshva/yii2-cover-behavior

or add, (*4)

"lyhoshva/yii2-cover-behavior": "*"

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

Basic Usage

This extension provides support for ActiveRecord image uploading. This support is granted via [[\lyhoshva\Cover\CoverBehavior]] ActiveRecord behavior. You'll need to attach it to your ActiveRecord class and point next options for it:, (*6)

class Article extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'image' => [
                'class' => CoverBehavior::className(),
                'modelAttribute' => 'image_name', // attribute, which will contains image_name
                // virtual attribute, which is used for image uploading ("image" by default)
                'relationReferenceAttribute' => 'image', 
                'path' => 'uploads/articles/', // path to upload directory 
            ],
        ];
    }

    public function rules()
    {
        return [
            [['image'], 'required', 'on' => 'create'],
            [['image', 'cover_image'], 'file', 'extensions' => ['png', 'jpg', 'jpeg', 'gif']],
        ];
    }
}

Attention: do NOT declare relationReferenceAttribute attribute in the owner ActiveRecord class. Make sure it does not conflict with any existing owner field or virtual property., (*7)

Virtual property declared via relationReferenceAttribute serves not only for saving. It also contains existing references for the uploaded image, (*8)

Advanced usage

Behavior can generate path ('path') or image name ('fileNameGenerator') automatically by writing generation rule using callback function. Or you can set static path or image name specifying String values. Callback function get ActiveRecord model object as argument., (*9)

You can specify image path ('modelAttributeFilePath') model attribute to save generated path in Data Base. It use only path configured as callback function. If this option not set and path is callback, image path will be saved to 'modelAttribute' as image path + image name string., (*10)

public function behaviors()
    {
        'cover' => [
                'class' => CoverBehavior::className(),
                'modelAttribute' => 'cover_name',

                'relationAttribute' => 'cover',
                // not required options
                'path' => function ($model) {
                    return 'web/uploads/' . Inflector::slug($model->name) . '/'; 
                    // generated path should have forward slash at the end;
                },
                'modelAttributeFilePath' => 'cover_path',
                'fileNameGenerator' => function ($model) {
                    return Inflector::slug($model->name);
                },
            ],
    }

Thumbnails

If you want to use thumbnails:, (*11)

public function behaviors()
    {
        return [
            'image' => [
                'class' => CoverBehavior::className(),
                'modelAttribute' => 'image_name', // attribute, which will be handled
                // virtual attribute, which is used for image uploading ("image" by default)
                'relationReferenceAttribute' => 'image', 
                'path' => 'uploads/articles/', // path to upload directory 
                'thumbnails' => [
                    [
                        'prefix' => 'thumb_', //image prefix for thumbnail
                        'width' => 220, // max width
                        'height' => 160, // max height
                        'mode' => CoverBehavior::THUMBNAIL_INSET, 
                    ]
                ],
            ],
        ];
    }

To see information about mods read [Imagine] (https://imagine.readthedocs.io/), (*12)

The Versions

17/03 2017

dev-master

9999999-dev

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

17/03 2017

v1.2.3

1.2.3.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

13/03 2017

v1.2.2

1.2.2.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

07/03 2017

v1.2.1

1.2.1.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

03/03 2017

v1.2.0

1.2.0.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

31/01 2017

v1.1.3

1.1.3.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

09/01 2017

v1.1.2

1.1.2.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

21/11 2016

v1.1.1

1.1.1.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

08/11 2016

v1.1.0

1.1.0.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

07/10 2016

v1.0.1

1.0.1.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior

07/10 2016

v1.0.0

1.0.0.0

Yii 2 behavior for image uploading

  Sources   Download

MIT

The Requires

 

by Lyhoshva Vlad

extension yii2 behavior image uploading yii2-cover-behavior