Yii 1.1: img

Img - Image management made easy!

Yii-Img on Ohloh

Current version 1.2.2c

Img is an image manager which allows for saving, loading, deleting and versioning of images in your Yii application. Images are stored both on the hard-drive and in the database. The module utilizes the PHPThumb library to provide a wide range of image transformation options.

Img comes with an installer so it's very easy to set up and you should have it up and running in no time.


  • Image management through one single application component
  • Saving, loading and deleting of images easily
  • A wide variety of image transformation options
  • Active record for storing the image data
  • Installer for quick and easy set up
  • Extensive configuration options



Here's a step by step guide on how to set up the module:

Download the module and extract it under your modules folder. (If your application doesn't have a modules folder create it under protected.)

Configure your application according to the following example:

                'createOnDemand'=>true, // requires apache mod_rewrite enabled
                'install'=>true, // allows you to run the installer

Go to the following URL to run the installer:

(index.php?r=image if your URL format is not set to 'path')

Save CUploadedFile objects by calling the following method:


Where the first argument is the CUploadedFile, the second parameter is the optional name for the image and the last parameter is the optional sub-directory where to place the image.

If you configured the image manager according to the example above you can go to the following URL to create a new version of the image you've saved:

(index.php?r=image/default/create&id=1&version=small if your URL format is not set to 'path')

By default the new version of your image will be saved under /files/images/versions/small/.

Attaching the image record behavior

To easily be able to save and render images you can attach the ImgRecordBehavior to your active record:

public function behaviors()
    return array(
            'attribute'=>'imageId', // default value

Now if your active record has an attribute called 'imageId' and you use $model->saveImage(...) the image id will be automatically saved to your record and to render the associated image for a model you can use $model->renderImage(...). There will probably be added more features to this behavior in the future versions.

Image transformation

You can load images by calling the following method:


Where the id is the image id. This will return an ImgThumb object on which you can apply options directly or by using the ImgOptions. Here's a few examples:

$options=new ImgOptions();

On-demand image generation

Img also supports generation of image versions on-demand.

To enable on-demand generation you will need to enable the 'createOnDemand'-option for the module and the Apache mod_rewrite module on your server.

Once you've set everything up correctly image version will be created automatically when they are requested so you don't need to worry about creating all different images when you upload the image.

Change log

Oct 23, 2011

  • Release 1.2.2c
    • Fixed the rewrite regex for backwards compatibility

Oct 21, 2011

  • Release 1.2.2b
    • Fixed the database schema

Oct 20, 2011

  • Release 1.2.2
    • Added support for replacing invalid character in image filenames
    • Fixed the rewrite regex

Oct 18, 2011

  • Release 1.2.1b
    • Fixed an issue with the image record validation

Oct 18, 2011

  • Release 1.2.1
    • Added support for saving images in sub-directories

Oct 17, 2011

  • Release 1.2.0
    • Added support for naming images
    • Improved the image behavior
    • Improved the rewrite regex

Oct 12, 2011

  • Release 1.1.0
    • Added ImgRecordBehavior
    • Removed the Image parent, parents should now keep track of their image ids themselves

Aug 12, 2011

  • Release 1.0.2
    • Added functionality for deleting images

Jun 23, 2011

  • Release 1.0.1
    • Image file extensions will now be handled automatically
    • Removed the extension property from ImgOptions

Jun 22, 2011

  • Release 1.0
    • Official release

Total 12 comments

#12275 report it
EndErr at 2013/03/11 02:24am
image size
  1. How to get the image width and height?
  2. How to crop the image given a width or height to fit a preset block?
#10127 report it
antoncpu at 2012/10/05 09:09am
Missed column

Hi Chris,

Could you check schema.sql? Seems column "path" is missed.

#8496 report it
florin p at 2012/06/07 09:54am
Image versions save to database table

Hi Chris83,

It is possible to save image versions in images table? Thanks

#6909 report it
rrbot at 2012/02/12 02:34pm

I required the direct path for a PDF extension that didn't like file names without image extensions .php was bad, nothing was worse. :D

#5708 report it
Gia at 2011/11/05 03:03am
File format problems


Hey Chris83, great jobs with this extension. One little input, can you please save all the file format onto Unix format, i having trouble using this extension on Unix environment, cause of this file format. Thank you very much.

Ps: Sorry for my bad English.

#5621 report it
Chris83 at 2011/10/26 02:28am

Since version 1.2.0 you can name your images however you want but you can get the name from the Image table. Unfortunately the ImgManager::resolveFileName is protected so you cannot use that.

Could you tell me specifically why you need the image name because if necessary I could make the method public in future releases.

Thanks in advance.

#5618 report it
rrbot at 2011/10/25 09:00pm
Building an image file name

I needed to build a local path to the original image, given only the id of the image stored in the database. This is what worked for me:

$img = Yii::app()->image->loadModel($id);

$file_name = $img->id.".".$img->extension

#5504 report it
Chris83 at 2011/10/17 08:32am

I haven't implemented support for this yet but I've been brainstorming this and I'm quite sure I have a good solution. I will include it in the next release if there aren't any unexpected issues.

EDIT: This feature is now supported in version 1.2.1 ->

#4719 report it
yort at 2011/08/07 02:12pm
How to Set Image Save Path by Model

Hi Chris83,

First, nice extension.

And B, could you post a quick example of how to set the image path in the model (I guess?) so that one could, for example:

  • save menu item images into /files/images/menu/
  • save user profile images into /files/images/profile/
  • etc...

Or am I thinking about this in the wrong way since the model info is stored in the database... so directory organization is a moot point? Thanks

#4301 report it
Chris83 at 2011/06/23 04:05am
@bonnie continued

I forgot to mention a few things...

The product model's $this->images is a relation that is configured like so:

return array(
    'images' => array(self::HAS_MANY,'Image','parentId',

And the product model's $this->image is a property where the temporary CUploadedFile object is stored (by the controller).

Also the IMAGE_VERSION_SMALL is a constant defined in the application configuration (it's value is 'small'). The idea with using a constant is to allow for auto-completion in the IDE.

I did cut some corners in my application because I didn't need support for multiple images but there isn't any limitation that you couldn't implement support for that.

#4299 report it
Chris83 at 2011/06/23 03:44am

Hey Bonnie,

First you need to create a file upload field for uploading the images. Once you have uploaded an image you get a CUploadedFile object which you then can save by calling:


Where the first parameter is the CUploadedFile object, the second parameter is an identifier string which normally is the class name of the object that you're uploading the image for and the last parameter is the id of the object that you're saving the image for.

In other words the second parameter could in your case be 'Auto' and last parameter the id of the Auto.

Here's the method I use for saving the image in one of my applications:

 * Saves the image for this product.
 * @return Image the saved image.
 * @throws CException if the image could not be saved.
public function saveImage()
        foreach($this->images as $image)
    if($this->image instanceof CUploadedFile)
        return Yii::app()->image->save($this->image,get_class($this),$this->id);
        throw new CException('Could not save image. The uploaded image is not valid or empty!');

And here's the controller action that calls the saveImage method:

 * Updates a particular model.
 * If update is successful, the browser will be redirected to the 'view' page.
 * @param integer $id the ID of the model to be updated
public function actionUpdate($id)
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

Here's the method I use for rendering images:

 * Renders the image for this product.
 * @param string $version the image version name.
public function renderImage($version)
        // TODO: Implement support for multiple images.

And here's how I call the renderImage method from the product view:

<?php $data->product->renderImage(IMAGE_VERSION_SMALL); ?>

I hope this helps.


#4291 report it
bonnie at 2011/06/22 08:12pm
This seems to what I need

Thanks for this module. Can you please explain how I can use this in an application. I have an auto listing website and I need to have images with thumbs in that when clicked they give you the details and all the images. Thanks

Leave a comment

Please to leave your comment.

Create extension