dev-master
9999999-devProvides behaviors for attach files to model and manipulate it if files is image
MIT
The Requires
by Alexander Kireev
extension yii2 upload image thumb attach
Provides behaviors for attach files to model and manipulate it if files is image
Provides behaviors for attach files to model and manipulate it if files is image, (*1)
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist salopot/yii2-attach-image "dev-master"
or add, (*4)
"salopot/yii2-attach-image": "dev-master"
to the require section of your composer.json
file., (*5)
Once the extension is installed, simply use it in your code by :, (*6)
```php //in model, (*7)
use salopot\attach\behaviors\AttachFileBehavior; use salopot\attach\behaviors\AttachImageBehavior; ..., (*8)
public function rules() { return [ ... [['origin_name'], 'string', 'max' => 255], [['image'], 'image'], ]; }, (*9)
public function behaviors() { return [ 'image' => [ 'class' => AttachImageBehavior::className(), 'attributeName' => 'image', //'relativeTypeDir' => '/upload/images/test/', 'types' => array( 'thumb' => array( //'format' => 'gif', //"gif", "jpeg", "png", "wbmp", "xbm" 'process' => function($behavior, $image) { return $image->thumbnail(new \Imagine\Image\Box(150, 150)); } ), 'background' => array( 'process' => function($behavior, $image) { $image = $image->thumbnail(new \Imagine\Image\Box(150, 150)); $image->effects()->grayscale(); return $image; }, ), 'main' => array( //'processOn' => AttachImageBehavior::PT_DEMAND, //PT_RENDER, PT_BASE64_ENCODED, 'process' => function($behavior, $image) { $watermark = \yii\imagine\Image::getImagine()->open(Yii::$app->params['watermark']); $size = $image->getSize(); $wSize = $watermark->getSize(); $bottomRight = new \Imagine\Image\Point($size->getWidth() - $wSize->getWidth(), $size->getHeight() - $wSize->getHeight()); $image->paste($watermark, $bottomRight); $image = $image->thumbnail(new \Imagine\Image\Box(150, 150)); return $image; }, ), ), ] ]; }, (*10)
//Also can store extended info, (*11)
public function init() { parent::init(); $this->on(AttachFileBehavior::EVENT_AFTER_ATTACH_DATA, [$this, 'afterAttachData']); }, (*12)
public function afterAttachData($event) { $this->origin_name = $event->uploadedFile->name; }, (*13)
//in form edit view: = $form->field($model, 'image')->fileInput() ?>, (*14)
//in view: = Html::img($model->getBehavior('image')->getUrl('thumb')); ?> = Html::img($model->getBehavior('image')->getUrl('background')); ?> = Html::img($model->getBehavior('image')->getUrl('main')); ?>, (*15)
Provides behaviors for attach files to model and manipulate it if files is image
MIT
extension yii2 upload image thumb attach