Advertisement
Guest User

Untitled

a guest
Jan 15th, 2012
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | None | 0 0
  1. <?php
  2.  
  3. class EJustInTimeR extends CWidget {
  4.  
  5.     private $original;
  6.     private $src;
  7.     private $src_big;
  8.     private $thumbPath;
  9.     private $bigPath;
  10.     private $newName = '';
  11.     public $image = null;
  12.     public $display = 'thumb';
  13.     public $title = null;
  14.     public $rel = null;
  15.     public $alt = 'image';
  16.     public $bigWidth = 640;
  17.     public $bigHeight = 480;
  18.     public $thumbWidth = 100;
  19.     public $thumbHeight = 80;
  20.  
  21.  
  22.     public function init() {
  23.         $this->checkFolders();
  24.         $this->createUniqueName();
  25.         $this->initPathVars();
  26.         $this->createImagesIfNotExists();
  27.         $this->defineSrc();
  28.     }
  29.  
  30.     public function run() {
  31.    
  32.         echo '<a href="'.$this->src_big.'" rel="' . $this->getRel() .  '"><img src="' . $this->src . '" title="' . $this->getTitle() . '" alt="' . $this->getAlt() . '"/></a>';
  33.     }
  34.  
  35.     private function defineSrc() {
  36.         $this->src = ($this->display == 'thumb' ? '/images/jitr/thumb/' : '/images/jitr/big/') . $this->newName;
  37.     $this->src_big = ('/images/jitr/big/') . $this->newName;
  38.  
  39.     }
  40.  
  41.     private function createImagesIfNotExists() {
  42.         if (!file_exists($this->bigPath) || !file_exists($this->thumbPath)) {
  43.             Yii::import('application.extensions.image.Image');
  44.             $image = new Image($this->original);
  45.             if (!file_exists($this->bigPath)) {
  46.                 $image->resize($this->bigWidth, $this->bigHeight);
  47.                 $image->save($this->bigPath);
  48.             }
  49.             if (!file_exists($this->thumbPath)) {
  50.                 $image->resize($this->thumbWidth, $this->thumbHeight);
  51.                 $image->save($this->thumbPath);
  52.             }
  53.         }
  54.     }
  55.  
  56.     private function initPathVars() {
  57.         $this->original = __DIR__ . '/../../../images/jitr/originals/' . ($this->image);
  58.         $this->bigPath = __DIR__ . '/../../../images/jitr/big/' . $this->newName;
  59.         $this->thumbPath = __DIR__ . '/../../../images/jitr/thumb/' . $this->newName;
  60.     }
  61.  
  62.     private function createUniqueName() {
  63.         $ext = explode(".", $this->image);
  64.         $ext = $ext[count($ext) - 1];
  65.         $this->newName = md5($this->image) . "." . $ext;
  66.     }
  67.  
  68.     private function checkFolders() {
  69.         foreach (array(
  70.     __DIR__ . '/../../images/jitr',
  71.     __DIR__ . '/../../images/jitr/originals',
  72.     __DIR__ . '/../../images/jitr/big',
  73.     __DIR__ . '/../../images/jitr/thumb',
  74.         ) as $path)
  75.             if (!file_exists($path))
  76.                 throw new Exception($path . ' do not exists!');
  77.         if (!$this->image)
  78.             throw new Exception('Image cannot be null!');
  79.     }
  80.  
  81.     private function getTitle() {
  82.         return $this->title ? $this->title : $this->image;
  83.     }
  84.      private function getRel() {
  85.         return $this->rel ? $this->rel : $this->image;
  86.     }
  87.     private function getAlt() {
  88.         return $this->alt ? $this->alt : $this->image;
  89.     }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement