Documentation TYPO3 par Ameos

thumbs.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *  A copy is found in the textfile GPL.txt and important notices to the license
00017 *  from the author is found in LICENSE.txt distributed with these scripts.
00018 *
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00055 // *******************************
00056 // Set error reporting
00057 // *******************************
00058 error_reporting (E_ALL ^ E_NOTICE);
00059 
00060 
00061 
00062 // ******************
00063 // Constants defined
00064 // ******************
00065 define('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'');
00066 define('TYPO3_MODE','BE');
00067 define('PATH_thisScript',str_replace('//','/', str_replace('\\','/', (php_sapi_name()=='cgi'||php_sapi_name()=='isapi' ||php_sapi_name()=='cgi-fcgi')&&($_SERVER['ORIG_PATH_TRANSLATED']?$_SERVER['ORIG_PATH_TRANSLATED']:$_SERVER['PATH_TRANSLATED'])? ($_SERVER['ORIG_PATH_TRANSLATED']?$_SERVER['ORIG_PATH_TRANSLATED']:$_SERVER['PATH_TRANSLATED']):($_SERVER['ORIG_SCRIPT_FILENAME']?$_SERVER['ORIG_SCRIPT_FILENAME']:$_SERVER['SCRIPT_FILENAME']))));
00068 
00069 define('PATH_site', ereg_replace('[^/]*.[^/]*$','',PATH_thisScript));           // the path to the website folder (see init.php)
00070 define('PATH_typo3conf', PATH_site.'typo3conf/');
00071 define('PATH_t3lib', PATH_site.'t3lib/');
00072 define('TYPO3_mainDir', 'typo3/');              // This is the directory of the backend administration for the sites of this TYPO3 installation.
00073 
00074 // ******************
00075 // Including config
00076 // ******************
00077 require_once(PATH_t3lib.'class.t3lib_div.php');
00078 require_once(PATH_t3lib.'class.t3lib_extmgm.php');
00079 
00080 require(PATH_t3lib.'config_default.php');
00081 if (!defined ('TYPO3_db'))      die ('The configuration file was not included.');
00082 if (!$TYPO3_CONF_VARS['GFX']['image_processing'])       die ('ImageProcessing was disabled!');
00083 
00084 require_once(PATH_t3lib.'class.t3lib_db.php');          // The database library
00085 $TYPO3_DB = t3lib_div::makeInstance('t3lib_DB');
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00114 class SC_t3lib_thumbs {
00115         var $include_once=array();
00116 
00117         var $outdir = 'typo3temp/';             // The output directory of temporary files in PATH_site
00118         var $output = '';
00119         var $sizeDefault='56x56';
00120 
00121         var $imageList; // Coming from $TYPO3_CONF_VARS['GFX']['imagefile_ext']
00122         var $input;             // Contains the absolute path to the file for which to make a thumbnail (after init())
00123 
00124                 // Internal, static: GPvar:
00125         var $file;              // Holds the input filename (GET: file)
00126         var $size;              // Holds the input size (GET: size)
00127 
00128 
00135         function init() {
00136                 global $TYPO3_CONF_VARS;
00137 
00138                         // Setting GPvars:
00139                 $this->file = t3lib_div::_GP('file');
00140                 $this->size = t3lib_div::_GP('size');
00141 
00142                         // Image extension list is set:
00143                 $this->imageList = $TYPO3_CONF_VARS['GFX']['imagefile_ext'];                    // valid extensions. OBS: No spaces in the list, all lowercase...
00144 
00145                         // if the filereference $this->file is relative, we correct the path
00146                 if (substr($this->file,0,3)=='../')     {
00147                         $this->input = PATH_site.substr($this->file,3);
00148                 } else {
00149                         $this->input = $this->file;
00150                 }
00151 
00152                         // Now the path is absolute.
00153                         // Checking for backpath and double slashes + the thumbnail can be made from files which are in the PATH_site OR the lockRootPath only!
00154                 if (!t3lib_div::isAllowedAbsPath($this->input)) {
00155                         $this->input='';
00156                 }
00157         }
00158 
00165         function main() {
00166                 global $TYPO3_CONF_VARS;
00167 
00168                         // If file exists, we make a thumbsnail of the file.
00169                 if ($this->input && @file_exists($this->input)) {
00170 
00171                                 // Check file extension:
00172                         if (ereg('(.*)\.([^\.]*$)',$this->input,$reg))  {
00173                                 $ext=strtolower($reg[2]);
00174                                 $ext=($ext=='jpeg')?'jpg':$ext;
00175                                 if ($ext=='ttf')        {
00176                                         $this->fontGif($this->input);   // Make font preview... (will not return)
00177                                 } elseif (!t3lib_div::inList($this->imageList, $ext))   {
00178                                         $this->errorGif('Not imagefile!',$ext,basename($this->input));
00179                                 }
00180                         } else {
00181                                 $this->errorGif('Not imagefile!','No ext!',basename($this->input));
00182                         }
00183 
00184                                 // ... so we passed the extension test meaning that we are going to make a thumbnail here:
00185                         $this->size = $this->size ? $this->size : $this->sizeDefault;   // default
00186 
00187                                 // I added extra check, so that the size input option could not be fooled to pass other values. That means the value is exploded, evaluated to an integer and the imploded to [value]x[value]. Furthermore you can specify: size=340 and it'll be translated to 340x340.
00188                         $sizeParts = explode('x', $this->size.'x'.$this->size); // explodes the input size (and if no "x" is found this will add size again so it is the same for both dimensions)
00189                         $sizeParts = array(t3lib_div::intInRange($sizeParts[0],1,1000),t3lib_div::intInRange($sizeParts[1],1,1000));    // Cleaning it up, only two parameters now.
00190                         $this->size = implode('x',$sizeParts);          // Imploding the cleaned size-value back to the internal variable
00191                         $sizeMax = max($sizeParts);     // Getting max value
00192 
00193                                 // Init
00194                         $mtime = filemtime($this->input);
00195                         $outpath = PATH_site.$this->outdir;
00196 
00197                                 // Should be - ? 'png' : 'gif' - , but doesn't work (ImageMagick prob.?)
00198                                 // Ren: png work for me
00199                         $thmMode = t3lib_div::intInRange($TYPO3_CONF_VARS['GFX']['thumbnails_png'],0);
00200                         $outext = ($ext!='jpg' || ($thmMode & 2)) ? ($thmMode & 1 ? 'png' : 'gif') : 'jpg';
00201 
00202                         $outfile = 'tmb_'.substr(md5($this->input.$mtime.$this->size),0,10).'.'.$outext;
00203                         $this->output = $outpath.$outfile;
00204 
00205                         if ($TYPO3_CONF_VARS['GFX']['im'])      {
00206                                         // If thumbnail does not exist, we generate it
00207                                 if (!@file_exists($this->output))       {
00208 /*                                      if (strstr($this->input,' ') || strstr($this->output,' '))      {
00209                                                 $this->errorGif('Spaces in','filepath',basename($this->input));
00210                                         }
00211 */                                              // 16 colors for small (56) thumbs, 64 for bigger and all for jpegs
00212                                         if ($outext=='jpg')     {
00213                                                 $colors = '';
00214                                         } else {
00215                                                 $colors = ($sizeMax>56)?'-colors 64':'-colors 16';
00216                                         }
00217                                         $path = $TYPO3_CONF_VARS['GFX']['im_path_lzw'] ? $TYPO3_CONF_VARS['GFX']['im_path_lzw'] : $TYPO3_CONF_VARS['GFX']['im_path'];
00218                                         $parameters = '-sample '.$this->size.' '.$colors.' '.$this->wrapFileName($this->input.'[0]').' '.$this->wrapFileName($this->output);
00219                                         $cmd = t3lib_div::imageMagickCommand('convert', $parameters);
00220                                         exec($cmd);
00221                                         if (!@file_exists($this->output))       {
00222                                                 $this->errorGif('No thumb','generated!',basename($this->input));
00223                                         }
00224                                 }
00225                                         // The thumbnail is read and output to the browser
00226                                 if($fd = @fopen($this->output,'rb'))    {
00227                                         Header('Content-type: image/'.$outext);
00228                                         while (!feof($fd))      {
00229                                                 echo fread($fd, 10000);
00230                                         }
00231                                         fclose( $fd );
00232                                 } else {
00233                                         $this->errorGif('Read problem!','',$this->output);
00234                                 }
00235                         } else exit;
00236                 } else {
00237                         $this->errorGif('No valid','inputfile!',basename($this->input));
00238                 }
00239         }
00240 
00241 
00242 
00243 
00244 
00245 
00246 
00247 
00248 
00249 
00250 
00251         /***************************
00252          *
00253          * OTHER FUNCTIONS:
00254          *
00255          ***************************/
00256 
00267         function errorGif($l1,$l2,$l3)  {
00268                 global $TYPO3_CONF_VARS;
00269 
00270                 if (!$TYPO3_CONF_VARS['GFX']['gdlib'])  die($l1.' '.$l2.' '.$l3);
00271 
00272                         // Creates the basis for the error image
00273                 if ($TYPO3_CONF_VARS['GFX']['gdlib_png'])       {
00274                         Header('Content-type: image/png');
00275                         $im = imagecreatefrompng(PATH_t3lib.'gfx/notfound_thumb.png');
00276                 } else {
00277                         Header('Content-type: image/gif');
00278                         $im = imagecreatefromgif(PATH_t3lib.'gfx/notfound_thumb.gif');
00279                 }
00280                         // Sets background color and print color.
00281             $white = ImageColorAllocate($im, 0,0,0);
00282             $black = ImageColorAllocate($im, 255,255,0);
00283 
00284                         // Prints the text strings with the build-in font functions of GD
00285                 $x=0;
00286                 $font=0;
00287                 if ($l1)        {
00288                         imagefilledrectangle($im, $x, 9, 56, 16, $black);
00289                 ImageString($im,$font,$x,9,$l1,$white);
00290                 }
00291                 if ($l2)        {
00292                         imagefilledrectangle($im, $x, 19, 56, 26, $black);
00293                 ImageString($im,$font,$x,19,$l2,$white);
00294                 }
00295                 if ($l3)        {
00296                         imagefilledrectangle($im, $x, 29, 56, 36, $black);
00297                 ImageString($im,$font,$x,29,substr($l3,-14),$white);
00298                 }
00299 
00300                         // Outputting the image stream and exit
00301                 if ($TYPO3_CONF_VARS['GFX']['gdlib_png'])       {
00302                         imagePng($im);
00303                 } else {
00304                         imageGif($im);
00305                 }
00306                 imagedestroy($im);
00307                 exit;
00308         }
00309 
00319         function fontGif($font) {
00320                 global $TYPO3_CONF_VARS;
00321 
00322                 if (!$TYPO3_CONF_VARS['GFX']['gdlib'])  die('');
00323 
00324                         // Create image and set background color to white.
00325                 $im = ImageCreate(250,76);
00326             $white = ImageColorAllocate($im, 255,255,255);
00327             $col = ImageColorAllocate($im, 0,0,0);
00328 
00329                         // The test string and offset in x-axis.
00330                 $string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz';
00331                 $x=13;
00332 
00333                         // Print (with non-ttf font) the size displayed
00334                 imagestring ($im, 1, 0, 2, '10', $col);
00335                 imagestring ($im, 1, 0, 15, '12', $col);
00336                 imagestring ($im, 1, 0, 30, '14', $col);
00337                 imagestring ($im, 1, 0, 47, '18', $col);
00338                 imagestring ($im, 1, 0, 68, '24', $col);
00339 
00340                         // Print with ttf-font the test string
00341                 imagettftext ($im, t3lib_div::freetypeDpiComp(10), 0, $x, 8, $col, $font, $string);
00342                 imagettftext ($im, t3lib_div::freetypeDpiComp(12), 0, $x, 21, $col, $font, $string);
00343                 imagettftext ($im, t3lib_div::freetypeDpiComp(14), 0, $x, 36, $col, $font, $string);
00344                 imagettftext ($im, t3lib_div::freetypeDpiComp(18), 0, $x, 53, $col, $font, $string);
00345                 imagettftext ($im, t3lib_div::freetypeDpiComp(24), 0, $x, 74, $col, $font, $string);
00346 
00347                         // Output PNG or GIF based on $TYPO3_CONF_VARS['GFX']['gdlib_png']
00348                 if ($TYPO3_CONF_VARS['GFX']['gdlib_png'])       {
00349                         Header('Content-type: image/png');
00350                         imagePng($im);
00351                 } else {
00352                         Header('Content-type: image/gif');
00353                         imageGif($im);
00354                 }
00355                 imagedestroy($im);
00356                 exit;
00357         }
00358 
00366         function wrapFileName($inputName)       {
00367                 if (strstr($inputName,' '))     {
00368                         $inputName='"'.$inputName.'"';
00369                 }
00370                 return $inputName;
00371         }
00372 }
00373 
00374 // Include extension class?
00375 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php'])        {
00376         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']);
00377 }
00378 
00379 
00380 
00381 
00382 // Make instance:
00383 $SOBE = t3lib_div::makeInstance('SC_t3lib_thumbs');
00384 $SOBE->init();
00385 $SOBE->main();
00386 ?>


Gnr par Les spcialistes TYPO3 avec  doxygen 1.4.6