PHPackages                             lyhoshva/yii2-cover-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. [Image &amp; Media](/categories/media)
4. /
5. lyhoshva/yii2-cover-behavior

ActiveYii2-extension[Image &amp; Media](/categories/media)

lyhoshva/yii2-cover-behavior
============================

Yii 2 behavior for image uploading

v1.2.3(9y ago)23761[1 PRs](https://github.com/lyhoshva/yii2-cover-behavior/pulls)MITPHPPHP &gt;=5.4

Since Oct 7Pushed 8y ago2 watchersCompare

[ Source](https://github.com/lyhoshva/yii2-cover-behavior)[ Packagist](https://packagist.org/packages/lyhoshva/yii2-cover-behavior)[ RSS](/packages/lyhoshva-yii2-cover-behavior/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (4)Dependencies (2)Versions (11)Used By (0)

yii2-cover-behavior
===================

[](#yii2-cover-behavior)

Yii 2 behavior for image uploading

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
composer require --prefer-dist lyhoshva/yii2-cover-behavior

```

or add

```
"lyhoshva/yii2-cover-behavior": "*"
```

to the require section of your composer.json.

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

[](#basic-usage)

This extension provides support for ActiveRecord image uploading. This support is granted via \[\[\\lyhoshva\\Cover\\CoverBehavior\]\] ActiveRecord behavior. You'll need to attach it to your ActiveRecord class and point next options for it:

```
class Article extends ActiveRecord
{
    public function behaviors()
    {
        return [
            'image' => [
                'class' => CoverBehavior::className(),
                'modelAttribute' => 'image_name', // attribute, which will contains image_name
                // virtual attribute, which is used for image uploading ("image" by default)
                'relationReferenceAttribute' => 'image',
                'path' => 'uploads/articles/', // path to upload directory
            ],
        ];
    }

    public function rules()
    {
        return [
            [['image'], 'required', 'on' => 'create'],
            [['image', 'cover_image'], 'file', 'extensions' => ['png', 'jpg', 'jpeg', 'gif']],
        ];
    }
}
```

> Attention: do NOT declare `relationReferenceAttribute` attribute in the owner ActiveRecord class. Make sure it does not conflict with any existing owner field or virtual property.

Virtual property declared via `relationReferenceAttribute` serves not only for saving. It also contains existing references for the uploaded image

Advanced usage
--------------

[](#advanced-usage)

Behavior can generate **path** (`'path'`) or **image name** (`'fileNameGenerator'`) automatically by writing generation rule using [callback function](http://php.net/manual/en/language.types.callable.php#example-71). Or you can set static **path** or **image name** specifying String values. Callback function get ActiveRecord model object as argument.

You can specify **image path** (`'modelAttributeFilePath'`) model attribute to save generated path in Data Base. It use only **path** configured as callback function. If this option not set and **path** is callback, image path will be saved to `'modelAttribute'` as *image path* + *image name* string.

```
public function behaviors()
    {
        'cover' => [
                'class' => CoverBehavior::className(),
                'modelAttribute' => 'cover_name',

                'relationAttribute' => 'cover',
                // not required options
                'path' => function ($model) {
                    return 'web/uploads/' . Inflector::slug($model->name) . '/';
                    // generated path should have forward slash at the end;
                },
                'modelAttributeFilePath' => 'cover_path',
                'fileNameGenerator' => function ($model) {
                    return Inflector::slug($model->name);
                },
            ],
    }
```

Thumbnails
----------

[](#thumbnails)

If you want to use thumbnails:

```
public function behaviors()
    {
        return [
            'image' => [
                'class' => CoverBehavior::className(),
                'modelAttribute' => 'image_name', // attribute, which will be handled
                // virtual attribute, which is used for image uploading ("image" by default)
                'relationReferenceAttribute' => 'image',
                'path' => 'uploads/articles/', // path to upload directory
                'thumbnails' => [
                    [
                        'prefix' => 'thumb_', //image prefix for thumbnail
                        'width' => 220, // max width
                        'height' => 160, // max height
                        'mode' => CoverBehavior::THUMBNAIL_INSET,
                    ]
                ],
            ],
        ];
    }
```

To see information about mods read \[Imagine\] ()

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~18 days

Recently: every ~11 days

Total

10

Last Release

3343d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/39836f1c229994bd8854f5a851c59bfb61c749b6aab68044f2d1206606caa5ed?d=identicon)[lyhoshva](/maintainers/lyhoshva)

---

Top Contributors

[![vlykhoshva](https://avatars.githubusercontent.com/u/9845083?v=4)](https://github.com/vlykhoshva "vlykhoshva (21 commits)")

---

Tags

imageyii2extensionBehavioruploadingyii2-cover-behavior

### Embed Badge

![Health badge](/badges/lyhoshva-yii2-cover-behavior/health.svg)

```
[![Health](https://phpackages.com/badges/lyhoshva-yii2-cover-behavior/health.svg)](https://phpackages.com/packages/lyhoshva-yii2-cover-behavior)
```

###  Alternatives

[yurkinx/yii2-image

Yii2 extension for image manipulating using Kohana Image Library.

66412.9k9](/packages/yurkinx-yii2-image)[noam148/yii2-image-resize

A Yii2 component for resizing images (on the fly)

1144.6k7](/packages/noam148-yii2-image-resize)[raoul2000/yii2-jcrop-widget

This extension is a wrapper for the jQuery Image Cropping Plugin (jcrop)

10248.3k2](/packages/raoul2000-yii2-jcrop-widget)[maxmirazh33/yii2-uploadable-cropable-image

Yii2 extension for upload and crop images

1020.8k](/packages/maxmirazh33-yii2-uploadable-cropable-image)[liyunfang/yii2-upload-behavior

Upload behavior for Yii 2

161.7k](/packages/liyunfang-yii2-upload-behavior)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
