PHPackages                             oxy-coach/yii2-image-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. oxy-coach/yii2-image-behavior

ActiveLibrary[File &amp; Storage](/categories/file-storage)

oxy-coach/yii2-image-behavior
=============================

yii2 image upload behavior

v1.2(7y ago)0641BSD-2-ClausePHPPHP &gt;=5.4.0

Since Aug 17Pushed 7y agoCompare

[ Source](https://github.com/oxy-coach/yii2-image-behavior)[ Packagist](https://packagist.org/packages/oxy-coach/yii2-image-behavior)[ Docs](https://github.com/oxy-coach/yii2-image-behavior)[ RSS](/packages/oxy-coach-yii2-image-behavior/feed)WikiDiscussions master Synced today

READMEChangelog (3)DependenciesVersions (4)Used By (1)

yii2-image-behavior
===================

[](#yii2-image-behavior)

Yii 2 image uploading

Install via Composer
--------------------

[](#install-via-composer)

Run the following command

```
$ composer require oxy-coach/yii2-image-behavior "*"
```

or add

```
$ "oxy-coach/yii2-image-behavior": "*"
```

to the require section of your `composer.json` file.

Migrations
----------

[](#migrations)

Create migration by following command

```
$ yii migrate/create images
```

Open the `/path/to/migrations/m_xxxxxx_xxxxxx_images.php` file and add following code to `up()` method

```
        $this->createTable('images', [
            'id' => $this->primaryKey(),
            'itemId' => $this->integer(11)->notNull(),
            'path' => $this->string(255),
            'extension' => $this->string(255)->notNull(),
            'sort' => $this->integer()->defaultValue(0),
        ]);
```

Create model
------------

[](#create-model)

Generate Active Record model for new `images` table

Configuring
-----------

[](#configuring)

Attach the behavior to your model class:

```
use oxycoach\imagebehavior\ImageBehavior;

\\ ...

    public $file;

    public function behaviors()
    {
        return [
            'ImageBehavior' => [
                'class' => ImageBehavior::class,
                'imageModel' => Images::class,
                'imageVariable' => 'file',
                'uploadPath' => '@upload',
                'webUpload' => '@webupload',
                'noImagePath' => '@webupload/no-photo.png',
                'multiple' => true,
                'sizes' => [
                    'original' => [
                        'folder' => 'images/original'
                    ],
                    'preview' => [
                        'folder' => 'images/preview',
                        'width' => 350,
                        'height' => 0, // "0" means auto-height
                    ],
                ],
            ],
        ];
    }

    public function rules()
    {
        [['file'], 'file', 'extensions' => ['png', 'jpg', 'jpeg', 'gif'], 'maxSize' => 1024*1024*1024, 'maxFiles' => 3],
    }
```

Add relation for Images model

```
    /**
     * Images model relation
     * @return \yii\db\ActiveQuery
     */
    public function getImages()
    {
        return $this->hasMany(Images::class, ['itemId' => 'id'])
            ->alias('img')
            ->orderBy('img.sort ASC');
    }
```

> Note that relation name **MUST** be `images`,

With that configuration, if file hash will be like `"6e3c797abee0ff2803ef1f952f187d2f"` there would be 2 images:

- `@upload/images/original/6e/3c/{id from image table}.jpg`
- `@upload/images/preview/6e/3c/{id from image table}.jpg`

for each image that would be uploaded

View file
---------

[](#view-file)

Example of view file

```
 ['enctype' => 'multipart/form-data']]) ?>
    field($model, 'file[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>

         'btn btn-success']) ?>

```

> Note that if you need a single image uploading, you have to change `multiple` property for behavior to false, change your model file rule `maxFiles` property to 1, and also change your view form field to

```
