imagefly

Extension which helps generation of thumbnails according to need. On the Fly.
4 followers

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

  1. Unzip the file inside the extension folder.

  2. 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();
}

Resources

Total 3 comments

#16307 report it
Carlos Adrián at 2014/02/08 09:54am
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.

#16231 report it
Vinny.freire at 2014/02/01 02:16pm
Size of Image

You're right Adrian, I'm sorry! in fact, you can take any integer value.

#16230 report it
Carlos Adrián at 2014/02/01 11:57am
Size of the image

The size must be ImageFly::MEDIUM instead of Image::MEDIUM

Leave a comment

Please to leave your comment.

Create extension