PHPackages                             voskobovich/yii2-dynamic-image - 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. voskobovich/yii2-dynamic-image

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

voskobovich/yii2-dynamic-image
==============================

Tools for generating image sizes upon customer request and upload image on server for Yii2

v1.0.1(10y ago)81311MITPHPPHP &gt;=5.4.0

Since Jan 7Pushed 10y ago1 watchersCompare

[ Source](https://github.com/voskobovich/yii2-dynamic-image)[ Packagist](https://packagist.org/packages/voskobovich/yii2-dynamic-image)[ Docs](https://github.com/voskobovich/yii2-dynamic-image)[ RSS](/packages/voskobovich-yii2-dynamic-image/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (3)Used By (0)

Yii2 Dynamic Image
==================

[](#yii2-dynamic-image)

A toolkit to resize and crop images on the fly as specified in a GET request.

[![License](https://camo.githubusercontent.com/9e108126ff1bd9bcf216a065209f8e6490bea77016fb30af56dd401ceb29aa58/68747470733a2f2f706f7365722e707567782e6f72672f766f736b6f626f766963682f796969322d64796e616d69632d696d6167652f6c6963656e73652e737667)](https://packagist.org/packages/voskobovich/yii2-dynamic-image)[![Latest Stable Version](https://camo.githubusercontent.com/318ef262f9b0452a2836269e5a8bb4333e0d4aca29a675f7830cab30a225d2f5/68747470733a2f2f706f7365722e707567782e6f72672f766f736b6f626f766963682f796969322d64796e616d69632d696d6167652f762f737461626c652e737667)](https://packagist.org/packages/voskobovich/yii2-dynamic-image)[![Latest Unstable Version](https://camo.githubusercontent.com/3d3521997da34a64ad7f67cdb4a93c3d7fd5867a42557f93fb91f0db238d9bca/68747470733a2f2f706f7365722e707567782e6f72672f766f736b6f626f766963682f796969322d64796e616d69632d696d6167652f762f756e737461626c652e737667)](https://packagist.org/packages/voskobovich/yii2-dynamic-image)[![Total Downloads](https://camo.githubusercontent.com/d0c144fe4a1b6c29c56430b4d205e29c07cb4f5220c573823db4d81c9e37d58d/68747470733a2f2f706f7365722e707567782e6f72672f766f736b6f626f766963682f796969322d64796e616d69632d696d6167652f646f776e6c6f6164732e737667)](https://packagist.org/packages/voskobovich/yii2-dynamic-image)

Support
-------

[](#support)

[GutHub issues](https://github.com/voskobovich/yii2-dynamic-image/issues).

See example
-----------

[](#see-example)

### Directory structure

[](#directory-structure)

This toolkit requires your images to be organized in a specific manner.

A certain directory is the root:

```
uploads/images

```

Images that belong to certain entities (articles, users, etc.) are located in directories that correspond to those entities and their ids:

```
uploads/images/article/123
uploads/images/user/456

```

Every image should have an alphanumeric name (usually some kind of hash) and a proper extension:

```
uploads/images/article/123/acbd18db4cc2f85cedef654fccc4a4d8.jpg
uploads/images/user/456/37b51d194a7513e45b56f6524f2d51f2.png

```

### Web Package

[](#web-package)

#### Getting images

[](#getting-images)

Suppose the original image is available at this URL:

```
http://example.com/uploads/images/article/2/XXXXXXXXXX.png

```

and you need it resized to 300x400 pixels. Simply add the desired dimensions to the name of the image as shown below:

```
http://example.com/uploads/images/article/2/300x400_XXXXXXXXXX.png

```

The image will be resized proportionately and excess cropped. Not bad, right?

But what if only one of the dimensions is significant, and the other one is not? In this case, you need to set the non-significant dimension to zero:

```
http://example.com/uploads/images/article/2/300x0_XXXXXXXXXX.png

```

or

```
http://example.com/uploads/images/article/2/0x400_XXXXXXXXXX.png

```

##### Placeholder

[](#placeholder)

The system can be configured to work with placeholders to serve when the image is not available. Placeholder file is usually named `placeholder.png`. There are two kinds of placeholder - general and entity-specific. General placeholder is placed into the root directory, entity-specific placeholder is placed into the entity directory:

```
http://example.com/uploads/images/placeholder.png
http://example.com/uploads/images/article/placeholder.png

```

With this configuration, article images have their own specific placeholder, while user images fall back to the general placeholder. Requesting a non-existent user image will return the general placeholder (`http://domain.com/uploads/images/placeholder.png`), while requesting a non-existent article image will return the entity-specific article placeholder (`http://domain.com/uploads/images/article/placeholder.png`).

#### Uploading images

[](#uploading-images)

To upload a image you need to send a **multipart/form-data** POST request:

```
POST: http://example.com/image/upload
Attribute name: file

```

The response will contain

```
{
    "name": "a26b9e822d962f1c7ebf6c255b170820.jpg",
    "url": "http://static.example.com/uploads/images/temp/20150908",
    "width": 300,
    "height": 400
}

```

### API Package

[](#api-package)

#### Getting images

[](#getting-images-1)

After configuration the answer API you get 4 new attribute.

```
{
    ...
    "image_small": "namesmallimage.png",
    "image_small__url": "http://static.example.com/uploads/images/article/2",
    "image_big": "namebigimage.png",
    "image_big__url": "http://static.example.com/uploads/images/article/2"
}

```

Now on the client you can do so

```
var bigImageUrl = answer.image_big__url + '/300x400_' + answer.image_big;

```

#### Uploading images

[](#uploading-images-1)

To upload a image you need to send a **multipart/form-data** POST request:

```
POST: http://api.example.com/image/upload
Attribute name: file

```

The response will contain

```
{
    "name": "a26b9e822d962f1c7ebf6c255b170820.jpg",
    "url": "http://static.example.com/uploads/images/temp/20150908",
    "width": 300,
    "height": 400
}

```

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

[](#installation)

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

Either run

```
php composer.phar require --prefer-dist voskobovich/yii2-dynamic-image "~1.0"

```

or add

```
"voskobovich/yii2-dynamic-image": "~1.0"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

#### Web Package

[](#web-package-1)

Use the package for **voskobovich\\image\\dynamic\\web**.

Create and configure your controller.

```
class ImageController extends Frontend
{
   /**
    * @inheritdoc
    */
   public function behaviors()
   {
       $behaviors = parent::behaviors();
       $behaviors['verbs'] = [
           'class' => VerbFilter::className(),
           'actions' => [
               'index' => ['GET'],
               'upload' => ['POST'],
           ],
       ];
       return $behaviors;
   }

   /**
    * @inheritdoc
    */
   public function actions()
   {
       $actions = parent::actions();

       $actions['index'] = [
           'class' => 'voskobovich\image\dynamic\web\actions\IndexAction',
           'basePath' => Yii::getAlias("@webroot/uploads/images"),
           'baseUrl' => '/uploads/images',
           'placeholder' => 'placeholder.png'
       ];
       $actions['upload'] = [
           'class' => 'voskobovich\image\dynamic\web\actions\UploadAction',
           'basePath' => Yii::getAlias('@webroot/uploads/images'),
           'baseUrl' => '/uploads/images',
       ];

       return $actions;
   }
}

```

#### API Package

[](#api-package-1)

Use the package for **voskobovich\\image\\dynamic\\rest**.

Configure your controller as described above.

```
class Post extends ActiveRecord
{
    //...

    public function rules()
    {
        $rules = parent::rules();

        $rules[] = ['image_small', 'string', 'max' => 255];
        $rules[] = ['image_small', ImageValidator::className()];

        $rules[] = ['image_big', 'string', 'max' => 255];
        $rules[] = ['image_big', ImageValidator::className()];

        return $rules;
    }

    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors[] = [
            'class' => ImageBehavior::className(),
            'basePath' => '@webroot/uploads/images/article/{id}',
            'baseUrl' => '/uploads/images/article/{id}',
            'fields' => [
                'image_small' => 'image_small_name',
                'image_big' => 'image_big_name',
            ]
        ];

        return $behaviors;
    }

    public function fields()
    {
        return [
            ...
            'image_small',
            'image_small__url',
            'image_big',
            'image_big__url',
        ];
    }

    //...
}

```

Configure ImagePathMap component in your config file.

```
return [
    ...
    'components' => [
        //...
        'imagePathMap' => [
            'class' => 'voskobovich\image\dynamic\components\ImagePathMap'
        ],
    ],
];

```

#### Web server configuration

[](#web-server-configuration)

##### Nginx

[](#nginx)

```
location /uploads {
    # For https://github.com/voskobovich/yii2-dynamic-image
    if (!-f $request_filename) {
        # uploads/images/(entity)/(id)/(width)x(height)_(original name) -> image server action
        rewrite ^/uploads/images/([a-z0-9-]+)/([0-9]+)/([0-9]+)x([0-9]+)_(.*)$ /image?folder=$1&id=$2&width=$3&height=$4&name=$5 redirect;

        # uploads/images/(entity)/(id)/(original name) -> entity-specific placeholder
        rewrite ^/uploads/images/([a-z0-9-]+)/([0-9]+)/(.+)$ /uploads/images/$1/placeholder.png redirect;

        # uploads/images/(entity)/(id)/placeholder.png -> general placeholder
        rewrite ^/uploads/images/([a-z0-9-_]*)/placeholder.png$ /uploads/images/placeholder.png redirect;
    }
}

```

##### Apache

[](#apache)

```
# For https://github.com/voskobovich/yii2-dynamic-image

# uploads/images/(entity)/(id)/(width)x(height)_(original name) -> image server action
RewriteRule ^uploads/images/([a-z0-9-]+)/([0-9]+)/([0-9]+)x([0-9]+)_(.*)$ /image?folder=$1&id=$2&width=$3&height=$4&name=$5 [R=302,L]

# uploads/images/(entity)/(id)/(original name) -> entity-specific placeholder
RewriteRule ^uploads/images/([a-z0-9-]+)/([0-9]+)/(.+)$ /uploads/images/$1/placeholder.png [R=302,L]

# uploads/images/(entity)/(id)/placeholder.png -> general placeholder
RewriteRule ^uploads/images/([a-z0-9-]+)/placeholder.png$ /uploads/images/placeholder.png [R=302,L]

```

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% 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 ~6 days

Total

2

Last Release

3824d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4267178fdbc890dad02ce0e600f7f5b9bbb54f5824b90feb76724af8cebb710f?d=identicon)[voskobovich](/maintainers/voskobovich)

---

Top Contributors

[![beowulfenator](https://avatars.githubusercontent.com/u/5413344?v=4)](https://github.com/beowulfenator "beowulfenator (1 commits)")[![voskobovich](https://avatars.githubusercontent.com/u/4508703?v=4)](https://github.com/voskobovich "voskobovich (1 commits)")

---

Tags

imageyii2generationdynamic

### Embed Badge

![Health badge](/badges/voskobovich-yii2-dynamic-image/health.svg)

```
[![Health](https://phpackages.com/badges/voskobovich-yii2-dynamic-image/health.svg)](https://phpackages.com/packages/voskobovich-yii2-dynamic-image)
```

###  Alternatives

[sadovojav/yii2-image-thumbnail

Yii2 image thumbnail component

2244.5k](/packages/sadovojav-yii2-image-thumbnail)[noam148/yii2-image-resize

A Yii2 component for resizing images (on the fly)

1045.1k7](/packages/noam148-yii2-image-resize)

PHPackages © 2026

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