PHPackages                             nedarta/yii2-upload-behavior - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. nedarta/yii2-upload-behavior

ActiveYii2-extension

nedarta/yii2-upload-behavior
============================

Configurable Yii2 image upload behavior with variants (resize, thumbnail, smartcrop) and automatic cleanup.

125PHP

Since Feb 7Pushed 3mo agoCompare

[ Source](https://github.com/nedarta/yii2-upload-behavior)[ Packagist](https://packagist.org/packages/nedarta/yii2-upload-behavior)[ RSS](/packages/nedarta-yii2-upload-behavior/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

UploadBehavior for Yii2
=======================

[](#uploadbehavior-for-yii2)

Full-featured Yii2 image upload behavior with advanced processing capabilities.

Features
--------

[](#features)

- **Automatic file uploads** via `UploadedFile`
- **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
------------

[](#requirements)

- Yii2
- yii2-imagine extension
- PHP GD or Imagick extension
- PHP EXIF extension (for auto-rotation)

Installation
------------

[](#installation)

Install via Composer:

```
composer require nedarta/yii2-upload-behavior
```

The extension will automatically install the required dependency `yiisoft/yii2-imagine`.

Configure the `@upload` alias in your application config:

```
'aliases' => [
    '@upload' => '@webroot/upload',
],
```

Basic Usage
-----------

[](#basic-usage)

### Simple Configuration

[](#simple-configuration)

```
use nedarta\behaviors\UploadBehavior;

class Event extends \yii\db\ActiveRecord
{
    public $upload; // Virtual attribute for file upload

    public function rules()
    {
        return [
            [['upload'], 'file', 'extensions' => 'png, jpg, jpeg', 'maxSize' => 1024 * 1024 * 10],
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => UploadBehavior::class,
                'uploadAttribute' => 'upload',
                'imageAttribute' => 'image',
                'uploadAlias' => '@upload/images/event',
            ],
        ];
    }
}
```

### Form Example

[](#form-example)

```
