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.
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:
'import'=>array( ..... 'application.modules.image.components.*', 'application.modules.image.models.Image', ), 'modules'=>array( ..... 'image'=>array( 'createOnDemand'=>true, // requires apache mod_rewrite enabled 'install'=>true, // allows you to run the installer ), ), 'components'=>array( ..... 'image'=>array( 'class'=>'ImgManager', 'versions'=>array( 'small'=>array('width'=>120,'height'=>120), 'medium'=>array('width'=>320,'height'=>320), 'large'=>array('width'=>640,'height'=>640), ), ), ),
Go to the following URL to run the installer:
index.php/image/ (index.php?r=image if your URL format is not set to 'path')
Save CUploadedFile objects by calling the following method:
Yii::app()->image->save($file,$model->name,'subdirectory');
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?image/default/create?id=1&version=small (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/.
To easily be able to save and render images you can attach the ImgRecordBehavior to your active record:
public function behaviors() { return array( ..... 'image'=>array( 'class'=>'image.components.ImgRecordBehavior', '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.
You can load images by calling the following method:
Yii::app()->image->loadThumb($id);
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:
$thumb=Yii::app()->image->loadThumb($id); $thumb->resize(640,320); $options=new ImgOptions(); $options->setCropFromCenter(180); $thumb=Yii::app()->image->loadThumb($id); $thumb->applyOptions($options); $config=array('width'=>320,'height'=>160); $options=ImgOptions::create($config); $thumb->applyOptions($options); $thumb->save('/tmp/sample-transformed-image.jpg');
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.
Total 12 comments
Hi Chris,
Could you check schema.sql? Seems column "path" is missed.
Hi Chris83,
It is possible to save image versions in images table? Thanks
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
@Chris83
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.
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.
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
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 ->
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:
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
I forgot to mention a few things...
The product model's $this->images is a relation that is configured like so:
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.
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:
And here's the controller action that calls the saveImage method:
Here's the method I use for rendering images:
And here's how I call the renderImage method from the product view:
I hope this helps.
-Chris
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 login to leave your comment.