Download the PHP package nedarta/yii2-upload-behavior without Composer
On this page you can find all versions of the php package nedarta/yii2-upload-behavior. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2-upload-behavior
UploadBehavior for Yii2
Full-featured Yii2 image upload behavior with advanced processing capabilities.
Features
- Automatic file uploads via
UploadedFile - Multiple image uploads - one-to-many gallery storage in a related table via
MultipleUploadBehavior - Nested directory creation with proper permissions
- EXIF auto-rotation - automatically corrects image orientation from cameras/phones
- Multiple image variants with flexible processing options:
- Resize (maintain aspect ratio)
- Thumbnail (crop to exact dimensions)
- Smart crop (intelligent cropping with optional nedarta/yii2-smart-cropper)
- Copy (fallback option)
- Dependency-based variant pipeline - create variants from other variants
- Format conversion - force output to JPG, PNG, or WebP
- Automatic cleanup - removes old variants on update and delete
- Random filename generation - prevents naming conflicts
Requirements
- Yii2
- yii2-imagine extension
- PHP GD or Imagick extension
- PHP EXIF extension (for auto-rotation)
Installation
Install via Composer:
The extension will automatically install the required dependency yiisoft/yii2-imagine.
Configure the @upload alias in your application config:
Basic Usage
Simple Configuration
Form Example
Controller Example
Multiple Image Uploads
Use MultipleUploadBehavior to store multiple images per model in a related table (one-to-many gallery). It reuses the full processing pipeline (EXIF auto-rotation, variants, format conversion) for every uploaded file.
Behavior Configuration
Related Model and Migration
Add a relation on the owner model for display:
Form
The file input name must end with []:
Displaying the Gallery
Update Semantics
- Update with new files - existing image records and files (including all variants) are deleted, then the new set is inserted (related row inserts run in a transaction).
- Update without files - the existing gallery is kept unchanged.
- Parent delete - all related image records and their files are removed.
Configuration Options
Basic Properties
| Property | Type | Default | Description |
|---|---|---|---|
uploadAttribute |
string | 'upload' |
Virtual attribute that receives the UploadedFile |
imageAttribute |
string | 'image' |
Database attribute where filename is stored |
uploadAlias |
string | required | Yii alias for storage folder |
baseName |
string|Closure | 'image' |
Base filename before random number |
autoRotate |
bool | true |
Reserved flag (auto-rotation currently always runs) |
forceConvert |
string|null | null |
Force output format: 'jpg', 'png', 'webp', 'jpeg' |
variants |
array | ['' => ['resize' => [2500, 2500]]] |
Variant definitions |
Base Name with Closure
EXIF Auto-Rotation
By default, images are automatically rotated based on their EXIF orientation data (common with phone cameras).
Note: Auto-rotation only works with JPEG files and requires the PHP EXIF extension. The autoRotate flag is present for future control, but in the current version rotation always runs when EXIF orientation is detected.
Format Conversion
Force all images (original and variants) to a specific format:
Supported formats: jpg, jpeg, png, webp
Image Variants
Simple Variants
This creates:
image-1234.jpg(original, resized to max 2500x2500)thumb_image-1234.jpg(300x300 thumbnail)medium_image-1234.jpg(resized to max 800x600)
Variant Options
Resize
Maintains aspect ratio, scales down to fit within dimensions:
Thumbnail
Crops to exact dimensions:
Smart Crop
Intelligent cropping (requires nedarta/yii2-smart-cropper):
Copy
Simply copies the source image:
Dependency Pipeline
Create variants from other variants for optimized processing:
Benefits:
- Faster processing (work from smaller images)
- Better quality chain
- Efficient resource usage
Advanced Examples
Blog Post with Multiple Variants
Product Image with Watermark Pipeline
User Avatar
Displaying Images
In Views
Helper Method
Add to your model:
File Structure Example
After uploading with variants, your directory will look like:
How It Works
Event Flow
- Before Validation - Captures the uploaded file
- After Insert/Update - Processes the upload:
- Creates upload directories if needed
- Removes old variants if updating
- Saves uploaded file to temporary location
- Applies EXIF auto-rotation
- Converts format if
forceConvertis set - Generates all variants in dependency order
- Updates model with new filename
- Before Delete - Removes all variants
Variant Processing Pipeline
- Determine source for each variant (original or another variant)
- Process variants in order
- Apply transformation (resize/thumbnail/smartcrop/copy)
- Save with specified quality
- Use result as source for dependent variants
Troubleshooting
Images not rotating correctly
- Ensure PHP EXIF extension is installed:
php -m | grep exif - Check that images are JPEG format (EXIF only works with JPEG)
- If you need to fully disable rotation, you will need to customize
UploadBehavior::autoRotateImage()(theautoRotateflag is currently not enforced)
Variants not created
- Check directory permissions (775 or 777)
- Verify GD or Imagick extension is installed
- Check error logs for specific errors
"Saving image in 'tmp' format is not supported"
- This is fixed in the current version
- Ensure you're using the latest UploadBehavior code
Old images not deleted
- Verify
deleteVariants()is being called - Check file permissions on upload directory
- Ensure variant prefixes match your configuration
License
Free to use and modify.
Credits
Created by nedarta