Source for file image.class.php

Documentation is available at image.class.php

  1. <?php
  2.  
  3. /**
  4.  * TPLN Image Plugin
  5.  * @package Template Engine
  6.  */
  7. class Image
  8. {
  9.     public $new_name;
  10.     public $path;
  11.     protected $obj;
  12.  
  13.     /**
  14.      * Upload file to directory, if name is not empty rename file (no extension needed)
  15.      *
  16.      * @param string $obj 
  17.      * @param string $path 
  18.      * @param string $new_name 
  19.      * @return boolean 
  20.      */
  21.     public function fileUpload($obj$path$new_name='')
  22.     {
  23.         $this->path = $path;
  24.         $this->obj = $obj;
  25.         //if there is not new name we keep the same origin name
  26.         if(empty($new_name))
  27.         {
  28.             $name $_FILES[$obj]['name'];
  29.             $path .= "/$name";
  30.             $this->new_name = $name;
  31.         }
  32.         else
  33.         {
  34.             $extent pathinfo($_FILES[$obj]['name']);
  35.             $extent $extent['extension'];
  36.             $name $new_name.".$extent";
  37.             $this->new_name = $name;
  38.             $path .= "/$name";
  39.         }
  40.  
  41.         if(!move_uploaded_file($_FILES[$obj]['tmp_name']$path))
  42.             return false;
  43.  
  44.         return true;
  45.     }
  46.  
  47.  
  48.     /**
  49.      * Assign image to thumbnail file
  50.      *
  51.      * @param string path full path with image name
  52.      */
  53.     public function imgThumbnailSetOriginal($path)
  54.     {
  55.         $this->new_name = basename($path);
  56.         $this->path = str_replace('/'.$this->new_name''$path);
  57.     }
  58.  
  59.  
  60.  
  61.     /**
  62.      * Transform image to thumbnail file
  63.      *
  64.      * @param int $width image width
  65.      * @param int $height image height
  66.      * @param boolean $contraint 
  67.      * @param array $background_color 3 colors RGB
  68.      * @param string $suffix add a suffix in image name
  69.      * @param string $force_type convert original image: jpg, gif, png
  70.      */
  71.     public function imgThumbnail($width$height$constraint=false$background_color=array(0,0,0)$suffix=''$force_type='')
  72.     {
  73.  
  74.         //recover the extension
  75.         $exts explode('.'$this->new_name);
  76.  
  77.         $file_ext strtolower($exts[count($exts)-1]);
  78.  
  79.         if(empty($force_type))
  80.             $force_type $file_ext;
  81.  
  82.         $file_name_output $this->new_name;
  83.         $file_name_output str_replace(".$file_ext""$suffix.$force_type"$file_name_output);
  84.         $p $this->path;
  85.         $this->path .= "/$this->new_name";
  86.  
  87.         // apply size ?
  88.         if($file_ext == 'jpg')$srcImg imagecreatefromjpeg($this->path);
  89.         elseif($file_ext == 'gif')$srcImg imagecreatefromgif($this->path);
  90.         elseif($file_ext == 'png')$srcImg imagecreatefrompng($this->path);
  91.  
  92.         $old_x imagesx($srcImg);
  93.         $old_y imagesy($srcImg);
  94.  
  95.         // width is larger than height
  96.         if($old_x $old_y)
  97.         {
  98.             $thumb_w $width;
  99.             $thumb_h $old_y ($width $old_x);
  100.         }
  101.         else
  102.         {
  103.             $thumb_w $old_x ($height $old_y);
  104.             $thumb_h $height;
  105.         }
  106.  
  107.         $thumb_w = (int)$thumb_w;
  108.         $thumb_h = (int)$thumb_h;
  109.  
  110.         // verify constraint
  111.         #if($constraint)
  112.         #{
  113.             if($thumb_w $width)
  114.             {
  115.                 $thumb_h $thumb_h ($width $thumb_w);
  116.                 $thumb_w $width;
  117.             }
  118.  
  119.             if($thumb_h $height)
  120.             {
  121.                 $thumb_w $thumb_w ($height $thumb_h);
  122.                 $thumb_h $height;
  123.             }
  124.  
  125.             $thumb_w = (int)$thumb_w;
  126.             $thumb_h = (int)$thumb_h;
  127.         #}
  128.  
  129.  
  130.         $img_resampled @imagecreatetruecolor($thumb_w$thumb_h);
  131.         imagecopyresampled($img_resampled$srcImg0000$thumb_w$thumb_h$old_x$old_y);
  132.  
  133.  
  134.         if($constraint)
  135.         {
  136.             $posX = (int)(($width $thumb_w)/2);
  137.             $posY = (int)(($height $thumb_h)/2);
  138.             $img_resampled imagecreatetruecolor($width$height);
  139.             $red imagecolorallocate($img_resampled$background_color[0]$background_color[1]$background_color[2]);
  140.             imagefill($img_resampled00$red);
  141.  
  142.             //$background = imagecolorallocate($img_resampled, $background_color[0], $background_color[1], $background_color[2]);
  143.             imagecopyresampled($img_resampled$srcImg$posX$posY00$thumb_w$thumb_h$old_x$old_y);
  144.         }
  145.  
  146.         // output with new prefix ?
  147.         if($force_type == 'jpg')imagejpeg($img_resampled$p."/".$file_name_output100);
  148.         elseif($force_type == 'png')imagepng($img_resampled$p."/".$file_name_output);
  149.         elseif($force_type == 'gif')imagegif($img_resampled$p."/".$file_name_output);
  150.     }
  151.  
  152. }
  153.  
  154. ?>

Documentation generated on Sat, 06 Mar 2010 21:33:58 +0100 by phpDocumentor 1.4.3