2017 © Pedro PelĂĄez
 

yii2-extension yii2-image-behavior

Behavior that will help you to save the modified copy of the uploaded image and give you easy access to them

image

godzie44/yii2-image-behavior

Behavior that will help you to save the modified copy of the uploaded image and give you easy access to them

  • Wednesday, June 15, 2016
  • by godzie44
  • Repository
  • 1 Watchers
  • 0 Stars
  • 45 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 41 % Grown

The README.md

Yii 2 Image Behavior

This behavior will help you to:, (*1)

  • easy save uploaded image
  • easy save modified copy of the initial image by apply modifiers like resize, flip and others. Also, you can apply more modifiers in an arbitrary sequence.
  • get access to uploaded images
  • delete images when delete record, update images when update record

Installation

composer require godzie44/yii2-image-behavior

Test

$ codecept run

Usage

In controller., (*2)

Just put in image attribute FileUpload object (like in official guide) and call $model->save() method, (*3)

In model (example), (*4)

 public function behaviors()
    {
        return [
             [
                'class' => \godzie44\yii\behaviors\image\ImageBehavior::className(),

                'imageAttr' => 'avatar', //attribute in model, instance of FileUploaded

                'images' => [ // array of images that we whant to save
                   'default' => ['default' => []],
                   'small' => ['resize' => [150,200]],
                   'fliped' => ['flip' => [30]],
                   'sharped' => ['sharpen' => [30]],
                   'croped' => ['crop' => [30,40,20,10]],
                   'medium-rotate' => ['resize' => [300,400], 'rotate' => [40]],
                ],

                'saveDirectory' => Yii::getAlias('@webroot/uploads/avatars/'),
            ],
            ...
        ];
    }

Now in 'saveDirectory' directory we have 6 images with names like "<timestapm><default/small/fliped/...>.<file extension>", (*5)

Get path to this images by calling $model->getImage('default') - where default is postfix of needed image, (*6)

Parameters

 Behavior parameters

  • imageAttr (required, string) Name of model attribute that contains FileUploaded object.
  • images (required, array[]) List of output images. Fields in this array must be format:, (*7)

    [string image_postfix => [string modifiers => array $params, ...]], (*8)

    where:, (*9)

    • image_postfix string, postfix of concrete image
    • modifiers string, modificator that will be applied to the image. (see modificator list and their params in modificators section)
    • params array, params of modificator
  • saveDirectory (required, string) The directory where the images are saved., (*10)

  • options (optional, array) where:, (*11)

    • deleteOldWhenUpdate (optional, boolean) Default True. True - delete old images when upload new file in existing field, false - don't delete.
    • ifNullBehavior (optional, string) Default ImageBehavior::DELETE_IF_NULL. ImageBehavior::DELETE_IF_NULL - when attribute=NULL old images will be deleted, ImageBehavior::DO_NOTHING_IF_NULL - when attribute=NULL old images dont be deleted and field don be rewrite.

 Modifiers

  • default default image, params is empty array [].
  • resize resize image, params [int width,int height].
  • flip flip image, params [int direction].
  • sharpen sharpen image, params [int amount].
  • crop crop image, params [int width, int height,int offset_x,int offset_y].
  • rotate rotate image, params [int degrees].

You can use this modifiers in any number and combinations., (*12)

Simple example of usage (user profile with avatar)

In controller, (*13)

public function actionProfile() {
        $model =  User::findOne(Yii::$app->user->id);
        if ($model->load(Yii::$app->request->post())) {
            $model->avatar = UploadedFile::getInstance($model, 'avatar');
            $model->save();
        }
        return $this->render('profile', ['model' => $model,]);
    }

User model like:, (*14)

class User extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%user}}';
    }

    public function rules()
    {
        return [
            [['name', 'id'], 'safe'],
            [['avatar'], 'file', 'extensions' => 'png, jpg'],
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => ImageBehavior::className(),
                'saveDirectory' => Yii::getAlias('@webroot/uploads/avatars/'),
                'imageAttr' => 'avatar',
                'images' => [
                  '_default' => ['default' => []], //save default upload image
                  '_small' => ['resize' => [150,200]], //and save resized copy
                ],
                'options' => [
                    'ifNullBehavior' => ImageBehavior::DO_NOTHING_IF_NULL, 
                    //when avatar attribute contains null, don't need to deleted images and rewrite avatar field
                ]
            ],
        ];
    }


   //getter of resized image
    public function getSmallAvatar(){
        return = $this->getImage('_small');
    }

}

In view, (*15)

$form = ActiveForm::begin();

echo $form->field($model, 'name')->textInput();

echo Html::img($model->smallAvatar);
echo $form->field($model, 'avatar')->fileInput()->label('change avatar');

echo Html::submitButton('Save', ['class' => 'btn btn-primary']);
ActiveForm::end();

The Versions

15/06 2016

dev-master

9999999-dev

Behavior that will help you to save the modified copy of the uploaded image and give you easy access to them

  Sources   Download

MIT

The Requires

 

by Derevtsov Konstantin

yii behavior image upload image

05/06 2016

v1.0.0

1.0.0.0

Behavior that will help you to save the modified copy of the uploaded image and give you easy access to them

  Sources   Download

MIT

The Requires

 

by Derevtsov Konstantin

yii behavior image upload image