Images Thumbnails on the Fly.
Extension which helps generation of thumbnails according to need.
Besides creating files dynamically.
Requirements ¶
Yii 1.1 or above.
Instalation ¶
Unzip the file inside the extension folder.
Add to your config / main.php the following line:
'import' => array (
...
'application.extensions.ImageFly.components.*'
...
);
Usage ¶
//Normal mode
<img src='<?php echo ImageFly::Instance()->get($model, 'attr', ImageFly::MEDIUM, Image::MEDIUM);?>'/>
---
<img src='<?php echo ImageFly::Instance()->get($model, 'attr', ImageFly::MEDIUM);?>'/>
---
<img src='<?php echo ImageFly::Instance()->get($model, 'attr', 600, 745);?>'/>
//Using Chtml
echo CHtml::image(ImageFly::Instance()->get($model, 'attr', $x, $y));
Controller ¶
Eg.
public function actionCreate()
{
$model=new Post;
if(isset($_POST['Post']))
{
$model->attributes=$_POST['Post'];
$model->image = CUploadedFile::getInstance($model, 'image');
if($model->validate())
{
$model->image->saveAs(ImageFly::Instance()->getPath($model, 'image'));
if($model->save())
{
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
Models ¶
Your model should have the following attributes
$model->imagePath
$model->imagePathThumb
Where the attribute ImagePath is the path of the original image and imagePathThumb is the path where the thumbnail will be created.
eg:
public $imagePath = 'user/';
public $imagePathThumb = 'user/thumb/';
...
protected function beforeSave() {
if ($this->image instanceof CUploadedFile) {
$this->image = md5($this->image->getName()) . "." . $this->image->getExtensionName();
}
return parent::beforeSave();
}
Size of the image
The size must be ImageFly::MEDIUM instead of Image::MEDIUM
Size of Image
You're right Adrian, I'm sorry! in fact, you can take any integer value.
Same image name
Imagine we use this extension to upload a profile picture. What would happen if two users upload "myprofile.jpg"? I mean each one uploads a different picture, but with the same name. Would the second one overwrite the first one?
I have tried to change this line in the model (beforeUpdate):
$this->image = md5($this->image->getName()) . "." . $this->image->getExtensionName();
To:
$this->image = md5(uniqid() . $this->image->getName()) . "." . $this->image->getExtensionName();
But it crashes.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.