PHPackages                             circulon/yii2-images - 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. [File &amp; Storage](/categories/file-storage)
4. /
5. circulon/yii2-images

ActiveYii2-extension[File &amp; Storage](/categories/file-storage)

circulon/yii2-images
====================

yii2 images module for storing images

1.3.1(10y ago)1881[1 PRs](https://github.com/circulon/yii2-images/pulls)BSD-3-ClausePHP

Since Jul 17Pushed 10y ago1 watchersCompare

[ Source](https://github.com/circulon/yii2-images)[ Packagist](https://packagist.org/packages/circulon/yii2-images)[ RSS](/packages/circulon-yii2-images/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (6)Versions (11)Used By (0)

yii2-images
===========

[](#yii2-images)

Yii2-images is yii2 module that allows attachment of images to any model, you can also retrieve images in any sizes. Additionally you can set the main (default) image of a group of images.

Module supports Imagick and GD libraries, you can set up it in module settings.

Features

- single action which can be attached to any controller offering cleaner urls per controller
- optional output of base64 encoded data for use in ![]() tags
- optimised searching of db for image references per action
- customisable id attribute
- Handles UploadedFile classes internally so no need to save uploads first before attaching to model

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

[](#installation)

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

Either run

`php composer.phar require --prefer-dist circulon/yii2-images "*"`

or add

`"circulon/yii2-images": "*"`

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

Run the migration

```
php yii migrate/up --migrationPath=@vendor/circulon/yii2-images/migrations

```

Setup
-----

[](#setup)

add the module setup to your app config

```
'modules' => [
	...
	'images' => [
    	'class' => 'circulon\images\Module',
        // be sure, that permissions ok
        // if you cant avoid permission errors you have to create "images" folder in web root manually and set 777 permissions
        'imagesStorePath' => 'images/store', //path to origin images
        'imagesCachePath' => 'images/cache', //path to resized copies
        'graphicsLibrary' => 'GD', //but really its better to use 'Imagick'
        'placeholderPath' => '@webroot/images/placeholder.png', // if you want to get placeholder when image not exists, string will be processed by Yii::getAlias
    ],
],

```

optionally add the url route to the UrlManager

NOTE : you may need to add a similar rule to your module/s that have this attached action

```
'components' => [
    ...
    'urlManager' => [
      'enablePrettyUrl' => true,
      'showScriptName' => false,
      'rules' => [
          ...

          '///' => '/',

          ...
       ],
    ],
    ...
]

```

attach the behavior to your model/s

```
 	public function behaviors(){
    	return [
        	'image' => [
            	'class' => 'circlulon\images\behaviors\ImageBehavior',
              	'idAttribute' => 'id' // set the models id column , default : 'id'
          	]
      	];
  	}

```

add the action to the required controllers

```
	public function actions(){
    	return [
        	'image' => [
          		'class' => 'circulon\images\actions\ImageAction',

              // all the model classes to be searched by this action.
              // Can be fully qualified namespace or alias
          		'models' => ['User', ...]
	        ]
	    ];
	}
```

Usage
-----

[](#usage)

```
    $model = Model::findOne(12); //Model must have id

    //If an image is first it will be main image for this model
    $model->attachImage('../../image.png');

    //But if you need set another image as main, use second arg
    $model->attachImage('../../image2.png', true);

    //get all images
    $images = $model->getImages();
    foreach($images as $img){
        //retun url to full image
        echo $img->getUrl();

        //return url to proportionally resized image by width
        echo $img->getUrl('300x');

        //return url to proportionally resized image by height
        echo $img->getUrl('x300');

        //return url to resized and cropped (center) image by width and height
        echo $img->getUrl('200x300');
    }

    // get image model
    $image = $model->getImage();

    if($image){
        //get path to resized image
        echo $image->getPath('400x300');

        //path to original image
        $image->getPathToOrigin();

        //will remove this image and all cache files
        $model->removeImage($image);

        // get the content of the image
        $model->getContent();
    }

```

with an img tag

```
