- Features
- Links
- Setup
- Attaching the image record behavior
- Image transformation
- On-demand image generation
- Change log
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.
Features ¶
- 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
Links ¶
Setup ¶
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:
~
`
php
'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/.
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(
.....
'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.
Image transformation ¶
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');
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
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
@bonnie
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:
Yii::app()->image->save($file,get_class($this),$this->id);
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() { if($this->images!==array()) foreach($this->images as $image) $image->delete(); if($this->image instanceof CUploadedFile) return Yii::app()->image->save($this->image,get_class($this),$this->id); else 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) { $model=$this->loadModel($id); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['Product'])) { $model->attributes=$_POST['Product']; $model->image=CUploadedFile::getInstance($model,'image'); if($model->save()) { if($model->image!==null) $model->saveImage(); $this->redirect(array('admin')); } } $this->render('update',array( 'model'=>$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) { if($this->images!==array()) { // TODO: Implement support for multiple images. $image=$this->images[0]; $image->render($version); } }
And here's how I call the renderImage method from the product view:
<?php $data->product->renderImage(IMAGE_VERSION_SMALL); ?>
I hope this helps.
-Chris
@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', 'condition'=>'parent=:parent', 'params'=>array(':parent'=>get_class($this)) )), );
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.
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:
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
@tsmith108
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 ->
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
@rrbot
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.
File format problems
@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.
@chris83
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
Image versions save to database table
Hi Chris83,
It is possible to save image versions in images table? Thanks
Missed column
Hi Chris,
Could you check schema.sql? Seems column "path" is missed.
image size
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.