Skip to content

artkost/yii2-image-style-behavior

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Image style generate behavior for Yii 2

Create sets of styles for your images

Installation

The preferred way to install this extension is through composer.

Either run

$ composer require artkost/yii2-image-style-behavior

or add

"artkost/yii2-image-style-behavior": "*"

to the require section of your composer.json file.

Configuring

Configure model as follows

use artkost\imagestyle\ImageStyleBehavior;

class ImageFile extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            'styles' => [
                'class' => ImageStyleBehavior::className(),
                'path' => '@webroot/uploads/styles',
                'url' => '@web/uploads/styles',
                'attribute' => 'uri',
                'styles' => [
                    'big' => [$this, 'styleBig'], //can be any valid callable
                    'small' => [$this, 'styleSmall']
                ]
            ]
        ];
    }

    /**
     * @return \Imagine\Image\ManipulatorInterface
     */
    public function styleBig()
    {
        return Image::thumbnail($this->filePath, 814, 458)->save($this->style('big')->path);
    }
}

Usage

$file = ImageFile::findOne($id);
echo $file->style('big')->url;

About

Yii2 Images styles behavior

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages