[ Index ]

PHP Cross Reference of Mambo 4.6.5

[ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/mambots/editors/mostlyce/jscripts/tiny_mce/filemanager/connectors/php/Commands/ -> Thumbnail.php (source)

   1  <?php 
   2  /*
   3   * FCKeditor - The text editor for internet
   4   * Copyright (C) 2003-2005 Frederico Caldeira Knabben
   5   * 
   6   * Licensed under the terms of the GNU Lesser General Public License:
   7   *         http://www.opensource.org/licenses/lgpl-license.php
   8   * 
   9   * For further information visit:
  10   *         http://www.fckeditor.net/
  11   * 
  12   * File Name: Thumbnail.php
  13   *     Implements the Thumbnail command, to return
  14   *     a thumbnail to the browser for the sent file,
  15   *     if the file is an image an attempt is made to
  16   *     generate a thumbnail, otherwise an appropriate
  17   *     icon is returned.
  18   *     Output is image data
  19   * 
  20   * File Authors:
  21   *         Grant French (grant@mcpuk.net)
  22   */
  23  
  24  include  "helpers/iconlookup.php";
  25  
  26  class Thumbnail {
  27      var $fckphp_config;
  28      var $type;
  29      var $cwd;
  30      var $actual_cwd;
  31      var $filename;
  32      
  33  	function Thumbnail($fckphp_config,$type,$cwd) {
  34          $this->fckphp_config=$fckphp_config;
  35          $this->type=$type;
  36          $this->raw_cwd=$cwd;
  37          $this->actual_cwd=str_replace("//","/",($fckphp_config['UserFilesPath']."/$type/".$this->raw_cwd));
  38          $this->real_cwd=str_replace("//","/",($this->fckphp_config['basedir']."/".$this->actual_cwd));
  39          if (isset($_GET['FileName'])) {
  40              $this->filename=str_replace(array("..","/"),"",$_GET['FileName']);
  41          } else {
  42              $this->filename='';
  43          }
  44      }
  45      
  46  	function run() {
  47          //$mimeIcon=getMimeIcon($mime);
  48          $fullfile=$this->real_cwd.'/'.$this->filename;
  49          $thumbfile=$this->real_cwd.'/.thumb_'.$this->filename;
  50          $icon=false;
  51          
  52          if (file_exists($thumbfile)) {
  53              $icon=$thumbfile;
  54          } else {
  55              $mime=$this->getMIME($fullfile);
  56              $ext=strtolower($this->getExtension($this->filename));    
  57              
  58              if ($this->isImage($mime,$ext))
  59                  {
  60                  
  61                  //Try and find a thumbnail, else try to generate one
  62                  //    else send generic picture icon.
  63                  
  64                  if ($this->isJPEG($mime,$ext)) {
  65                      $result=$this->resizeFromJPEG($fullfile);
  66                      
  67                  } elseif ($this->isGIF($mime,$ext)) {
  68                      $result=$this->resizeFromGIF($fullfile);
  69                      
  70                  } elseif ($this->isPNG($mime,$ext)) {
  71                      $result=$this->resizeFromPNG($fullfile);
  72                  }
  73                  
  74                  if ($result!==false) {
  75                      if (function_exists("imagejpeg")) {
  76                          imagejpeg($result,$thumbfile,70);
  77                          chmod($thumbfile,0777);
  78                          $icon=$thumbfile;
  79                      } elseif (function_exists("imagepng")) {
  80                          imagepng($result,$thumbfile);
  81                          chmod($thumbfile,0777);
  82                          $icon=$thumbfile;
  83                      } elseif (function_exists("imagegif")) {
  84                          imagegif($result,$thumbfile);
  85                          chmod($thumbfile,0777);
  86                          $icon=$thumbfile;
  87                      } else {
  88                          $icon=iconLookup($mime,$ext);
  89                      }
  90                      
  91                  } else {
  92                      $icon=iconLookup($mime,$ext);
  93                  }
  94              } else {
  95                  $icon=iconLookup($mime,$ext);
  96              }
  97          }
  98          
  99          
 100          $iconMime=$this->image2MIME($icon);
 101          if ($iconMime==false) $iconMime="image/jpeg";
 102          
 103          header("Content-type: $iconMime",true);
 104          readfile($icon);
 105          
 106      }
 107      
 108  	function getMIME($file) {
 109          $mime="text/plain";
 110          
 111          //If mime magic is installed
 112          if (function_exists("mime_content_type")) {
 113              $mime=mime_content_type($file);
 114          } else {
 115              $mime=$this->image2MIME($file);
 116          }
 117          
 118          return strtolower($mime);
 119      }
 120      
 121  	function image2MIME($file) {
 122          if (!file_exists($file)) {
 123              return false;
 124          }
 125          
 126          $fh=fopen($file,"r");
 127          if ($fh) {
 128              $start4=fread($fh,4);
 129              $start3=substr($start4,0,3);
 130              
 131              if ($start4=="\x89PNG") {
 132                  return "image/png";
 133              } elseif ($start3=="GIF") {
 134                  return "image/gif";
 135              } elseif ($start3=="\xFF\xD8\xFF") {
 136                  return "image/jpeg";
 137              } elseif ($start4=="hsi1") {
 138                  return "image/jpeg";
 139              } else {
 140                  return false;
 141              }
 142              
 143              unset($start3);unset($start4);
 144              fclose($fh);
 145          } else {
 146              return false;
 147          }
 148      }
 149      
 150      
 151  	function isImage($mime,$ext) {
 152          if (
 153              ($mime=="image/gif")||
 154              ($mime=="image/jpeg")||
 155              ($mime=="image/jpg")||
 156              ($mime=="image/pjpeg")||
 157              ($mime=="image/png")||
 158              ($ext=="jpg")||
 159              ($ext=="jpeg")||
 160              ($ext=="png")||
 161              ($ext=="gif") ) {
 162          
 163              return true;
 164          } else {
 165              return false;
 166          }
 167      }
 168      
 169  	function isJPEG($mime,$ext) {
 170          if (($mime=="image/jpeg")||($mime=="image/jpg")||($mime=="image/pjpeg")||($ext=="jpg")||($ext=="jpeg")) {
 171              return true; 
 172          } else {
 173              return false;
 174          }
 175      }
 176  
 177  	function isGIF($mime,$ext) {
 178          if (($mime=="image/gif")||($ext=="gif")) {
 179              return true; 
 180          } else {
 181              return false;
 182          }
 183      }
 184      
 185  	function isPNG($mime,$ext) {
 186          if (($mime=="image/png")||($ext=="png")) {
 187              return true; 
 188          } else {
 189              return false;
 190          }
 191      }    
 192      
 193  	function getExtension($filename) {
 194          //Get Extension
 195          $ext=""; 
 196          $lastpos=strrpos($this->filename,'.'); 
 197          if ($lastpos!==false) $ext=substr($this->filename,($lastpos+1));
 198          return strtolower($ext);
 199      }
 200      
 201  	function resizeFromJPEG($file) {
 202          if (function_exists("imagecreatefromjpeg")) {
 203              $img=@imagecreatefromjpeg($this->real_cwd.'/'.$this->filename);
 204              return (($img)?$this->resizeImage($img):false);
 205          } else { return false; }
 206      }
 207      
 208  	function resizeFromGIF($file) {
 209          if (function_exists("imagecreatefromgif")) {
 210              $img=@imagecreatefromgif($this->real_cwd.'/'.$this->filename);
 211              return (($img)?$this->resizeImage($img):false);
 212          } else { return false; }
 213      }
 214      
 215  	function resizeFromPNG($file) {
 216          if (function_exists("imagecreatefrompng")) {
 217              $img=@imagecreatefrompng($this->real_cwd.'/'.$this->filename);
 218              return (($img)?$this->resizeImage($img):false);
 219          } else { return false; }
 220      }
 221      
 222  	function resizeImage($img) {
 223          //Get size for thumbnail
 224          $width=imagesx($img); $height=imagesy($img);
 225          if ($width>$height) { $n_height=$height*(96/$width); $n_width=96; } else { $n_width=$width*(96/$height); $n_height=96; }
 226          
 227          $x=0;$y=0;
 228          if ($n_width<96) $x=round((96-$n_width)/2);
 229          if ($n_height<96) $y=round((96-$n_height)/2);
 230          
 231          $thumb=imagecreatetruecolor(96,96);
 232          
 233          #Background colour fix by:
 234          #Ben Lancaster (benlanc@ster.me.uk)
 235          $bgcolor = imagecolorallocate($thumb,255,255,255);
 236          imagefill($thumb, 0, 0, $bgcolor);
 237          
 238          if (function_exists("imagecopyresampled")) {
 239              if (!($result=@imagecopyresampled($thumb,$img,$x,$y,0,0,$n_width,$n_height,$width,$height))) {
 240                  $result=imagecopyresized($thumb,$img,$x,$y,0,0,$n_width,$n_height,$width,$height);
 241              }    
 242          } else {
 243              $result=imagecopyresized($thumb,$img,$x,$y,0,0,$n_width,$n_height,$width,$height);
 244          }
 245  
 246          return ($result)?$thumb:false;
 247      }
 248  }
 249  
 250  ?>