Download the PHP package godzie44/yii2-image-behavior without Composer
On this page you can find all versions of the php package godzie44/yii2-image-behavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download godzie44/yii2-image-behavior
More information about godzie44/yii2-image-behavior
Files in godzie44/yii2-image-behavior
Package yii2-image-behavior
Short Description Behavior that will help you to save the modified copy of the uploaded image and give you easy access to them
License MIT
Informations about the package yii2-image-behavior
Yii 2 Image Behavior
This behavior will help you to:
- 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
Test
Usage
In controller.
Just put in image attribute FileUpload object (like in official guide) and call $model->save() method
In model (example)
Now in 'saveDirectory' directory we have 6 images with names like "\<timestapm>\<default/small/fliped/...>.\<file extension>"
Get path to this images by calling $model->getImage('default') - where default is postfix of needed image
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:
[string image_postfix => [string modifiers => array $params, ...]]
where:
- 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.
- options (optional, array) where:
- 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.
Simple example of usage (user profile with avatar)
In controller
User model like:
In view