2017 © Pedro Peláez
 

yii2-extension yii2-attach-image

Provides behaviors for attach files to model and manipulate it if files is image

image

salopot/yii2-attach-image

Provides behaviors for attach files to model and manipulate it if files is image

  • Wednesday, October 7, 2015
  • by salopot
  • Repository
  • 1 Watchers
  • 0 Stars
  • 55 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Model attach image behavior

Provides behaviors for attach files to model and manipulate it if files is image, (*1)

Features

  • Multilevel file store structure
  • Can generate thumbnail manual, by demand, after upload or return image as base64 encoded string
  • Automatic delete linked file when delete model
  • Allow use any image manipulation component. Default use imagine

Installation

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)

Usage

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)

The Versions

07/10 2015

dev-master

9999999-dev

Provides behaviors for attach files to model and manipulate it if files is image

  Sources   Download

MIT

The Requires

 

by Alexander Kireev

extension yii2 upload image thumb attach