Postado em January 8, 2014 por admin



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', Image::MEDIUM, Image::MEDIUM);?>'/>
---
<img src='<?php echo ImageFly::Instance()->get($model, 'attr', Image::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

Leave a Comment:

N
@
#
Por favor! insira o codigo acima para validar que você é um ser com alma! rs