PHPackages                             demi/image - 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. demi/image

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

demi/image
==========

Yii2 behavior for upload image to model

1.3.4(8y ago)2115.2k11[3 issues](https://github.com/demisang/yii2-image-uploader/issues)GPL-3.0-or-laterPHPPHP &gt;=5.4.0

Since Jun 19Pushed 8y ago5 watchersCompare

[ Source](https://github.com/demisang/yii2-image-uploader)[ Packagist](https://packagist.org/packages/demi/image)[ Docs](https://github.com/demisang/yii2-image-uploader#readme)[ RSS](/packages/demi-image/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (3)Versions (15)Used By (0)

yii2-image-uploader
===================

[](#yii2-image-uploader)

Yii2 behavior for upload image to model

Installation
------------

[](#installation)

Add to composer.json in your project

```
{
    "require": {
        "yurkinx/yii2-image": "dev-master",
        "demi/image": "~1.0"
    }
}
```

then run command

```
composer update

```

Configuration
-------------

[](#configuration)

In model file add image uploader behavior:

```
/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        'imageUploaderBehavior' => [
            'class' => 'demi\image\ImageUploaderBehavior',
            'imageConfig' => [
                // Name of image attribute where the image will be stored
                'imageAttribute' => 'image',
                // Yii-alias to dir where will be stored subdirectories with images
                'savePathAlias' => '@frontend/web/images/products',
                // Yii-alias to root project dir, relative path to the image will exclude this part of the full path
                'rootPathAlias' => '@frontend/web',
                // Name of default image. Image placed to: webrooot/images/{noImageBaseName}
                // You must create all noimage files: noimage.jpg, medium_noimage.jpg, small_noimage.jpg, etc.
                'noImageBaseName' => 'noimage.jpg',
                // List of thumbnails sizes.
                // Format: [prefix=>max_width]
                // Thumbnails height calculated proportionally automatically
                // Prefix '' is special, it determines the max width of the main image
                'imageSizes' => [
                    '' => 1000,
                    'medium_' => 270,
                    'small_' => 70,
                    'my_custom_size' => 25,
                ],
                // This params will be passed to \yii\validators\ImageValidator
                'imageValidatorParams' => [
                    'minWidth' => 400,
                    'minHeight' => 300,
                    // Custom validation errors
                    // see more in \yii\validators\ImageValidator::init() and \yii\validators\FileValidator::init()
                    'tooBig' => Yii::t('yii', 'The file "{file}" is too big. Its size cannot exceed {formattedLimit}.'),
                ],
                // Cropper config
                'aspectRatio' => 4 / 3, // or 16/9(wide) or 1/1(square) or any other ratio. Null - free ratio
                // default config
                'imageRequire' => false,
                'fileTypes' => 'jpg,jpeg,gif,png',
                'maxFileSize' => 10485760, // 10mb
                // If backend is located on a subdomain 'admin.', and images are uploaded to a directory
                // located in the frontend, you can set this param and then getImageSrc() will be return
                // path to image without subdomain part even in backend part
                'backendSubdomain' => 'admin.',
                // or if backend located by route '/admin/*'
                'backendRoute' => '/admin',
            ],
        ],
    ];
}
```

PHPdoc for model:

```
/**
 * @method getImageSrc($size = null)
 * @property string $imageSrc
 */
```

Usage
-----

[](#usage)

In any view file:

```
// big(mainly) image
Html::img($model->getImageSrc());
// or shortly
Html::img($model->imageSrc);

// medium image
Html::img($model->getImageSrc('medium_'));

// small image
Html::img($model->getImageSrc('small_'));

// image size "my_custom_size"
Html::img($model->getImageSrc('my_custom_size'));
```

\_form.php

```
