Documentation TYPO3 par Ameos

thumbs.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2007 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 if(!defined('PATH_thisScript')) 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 if(!defined('PATH_site'))               define('PATH_site', ereg_replace('[^/]*.[^/]*$','',PATH_thisScript));           // the path to the website folder (see init.php)
00069 if(!defined('PATH_t3lib'))              define('PATH_t3lib', PATH_site.'t3lib/');
00070 define('PATH_typo3conf', PATH_site.'typo3conf/');
00071 define('TYPO3_mainDir', 'typo3/');              // This is the directory of the backend administration for the sites of this TYPO3 installation.
00072 define('PATH_typo3', PATH_site.TYPO3_mainDir);
00073 
00074 
00075 // ******************
00076 // Including config
00077 // ******************
00078 require_once(PATH_t3lib.'class.t3lib_div.php');
00079 require_once(PATH_t3lib.'class.t3lib_extmgm.php');
00080 
00081 require(PATH_t3lib.'config_default.php');
00082 if (!defined ('TYPO3_db'))      die ('The configuration file was not included.');
00083 if (!$TYPO3_CONF_VARS['GFX']['image_processing'])       die ('ImageProcessing was disabled!');
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00113 class SC_t3lib_thumbs {
00114         var $include_once = array();
00115 
00116         var $outdir = 'typo3temp/';             // The output directory of temporary files in PATH_site
00117         var $output = '';
00118         var $sizeDefault='56x56';
00119 
00120         var $imageList;         // Coming from $TYPO3_CONF_VARS['GFX']['imagefile_ext']
00121         var $input;             // Contains the absolute path to the file for which to make a thumbnail (after init())
00122 
00123                 // Internal, static: GPvar:
00124         var $file;              // Holds the input filename (GET: file)
00125         var $size;              // Holds the input size (GET: size)
00126         var $mtime = 0;         // Last modification time of the supplied file
00127 
00128 
00135         function init() {
00136                 global $TYPO3_CONF_VARS;
00137 
00138                         // Setting GPvars:
00139                 $file = t3lib_div::_GP('file');
00140                 $size = t3lib_div::_GP('size');
00141                 $md5sum = t3lib_div::_GP('md5sum');
00142 
00143                         // Image extension list is set:
00144                 $this->imageList = $TYPO3_CONF_VARS['GFX']['imagefile_ext'];                    // valid extensions. OBS: No spaces in the list, all lowercase...
00145 
00146                         // If the filereference $this->file is relative, we correct the path
00147                 if (substr($file,0,3)=='../')   {
00148                         $file = PATH_site.substr($file,3);
00149                 }
00150 
00151                         // Now the path is absolute.
00152                         // Checking for backpath and double slashes + the thumbnail can be made from files which are in the PATH_site OR the lockRootPath only!
00153                 if (t3lib_div::isAllowedAbsPath($file)) {
00154                         $mtime = filemtime($file);
00155                 }
00156 
00157                         // Do an MD5 check to prevent viewing of images without permission
00158                 $OK = FALSE;
00159                 if ($mtime)     {
00160                                 // Always use the absolute path for this check!
00161                         $check = basename($file).':'.$mtime.':'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
00162                         $md5_real = t3lib_div::shortMD5($check);
00163                         if (!strcmp($md5_real,$md5sum)) {
00164                                 $OK = TRUE;
00165                         }
00166                 }
00167 
00168                 if ($OK)        {
00169                         $this->input = $file;
00170                         $this->size = $size;
00171                         $this->mtime = $mtime;
00172                 } else {
00173                         die('Error: Image does not exist and/or MD5 checksum did not match.');
00174                 }
00175         }
00176 
00183         function main() {
00184                 global $TYPO3_CONF_VARS;
00185 
00186                         // If file exists, we make a thumbsnail of the file.
00187                 if ($this->input && @file_exists($this->input)) {
00188 
00189                                 // Check file extension:
00190                         $reg = array();
00191                         if (ereg('(.*)\.([^\.]*$)',$this->input,$reg))  {
00192                                 $ext=strtolower($reg[2]);
00193                                 $ext=($ext=='jpeg')?'jpg':$ext;
00194                                 if ($ext=='ttf')        {
00195                                         $this->fontGif($this->input);   // Make font preview... (will not return)
00196                                 } elseif (!t3lib_div::inList($this->imageList, $ext))   {
00197                                         $this->errorGif('Not imagefile!',$ext,basename($this->input));
00198                                 }
00199                         } else {
00200                                 $this->errorGif('Not imagefile!','No ext!',basename($this->input));
00201                         }
00202 
00203                                 // ... so we passed the extension test meaning that we are going to make a thumbnail here:
00204                         if (!$this->size)       $this->size = $this->sizeDefault;       // default
00205 
00206                                 // 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.
00207                         $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)
00208                         $sizeParts = array(t3lib_div::intInRange($sizeParts[0],1,1000),t3lib_div::intInRange($sizeParts[1],1,1000));    // Cleaning it up, only two parameters now.
00209                         $this->size = implode('x',$sizeParts);          // Imploding the cleaned size-value back to the internal variable
00210                         $sizeMax = max($sizeParts);     // Getting max value
00211 
00212                                 // Init
00213                         $outpath = PATH_site.$this->outdir;
00214 
00215                                 // Should be - ? 'png' : 'gif' - , but doesn't work (ImageMagick prob.?)
00216                                 // René: png work for me
00217                         $thmMode = t3lib_div::intInRange($TYPO3_CONF_VARS['GFX']['thumbnails_png'],0);
00218                         $outext = ($ext!='jpg' || ($thmMode & 2)) ? ($thmMode & 1 ? 'png' : 'gif') : 'jpg';
00219 
00220                         $outfile = 'tmb_'.substr(md5($this->input.$this->mtime.$this->size),0,10).'.'.$outext;
00221                         $this->output = $outpath.$outfile;
00222 
00223                         if ($TYPO3_CONF_VARS['GFX']['im'])      {
00224                                         // If thumbnail does not exist, we generate it
00225                                 if (!@file_exists($this->output))       {
00226 /*                                      if (strstr($this->input,' ') || strstr($this->output,' '))      {
00227                                                 $this->errorGif('Spaces in','filepath',basename($this->input));
00228                                         }
00229 */                                              // 16 colors for small (56) thumbs, 64 for bigger and all for jpegs
00230                                         if ($outext=='jpg')     {
00231                                                 $colors = '';
00232                                         } else {
00233                                                 $colors = ($sizeMax>56)?'-colors 64':'-colors 16';
00234                                         }
00235                                         $parameters = '-sample '.$this->size.' '.$colors.' '.$this->wrapFileName($this->input.'[0]').' '.$this->wrapFileName($this->output);
00236                                         $cmd = t3lib_div::imageMagickCommand('convert', $parameters);
00237                                         exec($cmd);
00238                                         if (!@file_exists($this->output))       {
00239                                                 $this->errorGif('No thumb','generated!',basename($this->input));
00240                                         }
00241                                 }
00242                                         // The thumbnail is read and output to the browser
00243                                 if($fd = @fopen($this->output,'rb'))    {
00244                                         header('Content-type: image/'.$outext);
00245                                         fpassthru($fd);
00246                                         fclose($fd);
00247                                 } else {
00248                                         $this->errorGif('Read problem!','',$this->output);
00249                                 }
00250                         } else exit;
00251                 } else {
00252                         $this->errorGif('No valid','inputfile!',basename($this->input));
00253                 }
00254         }
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266         /***************************
00267          *
00268          * OTHER FUNCTIONS:
00269          *
00270          ***************************/
00271 
00282         function errorGif($l1,$l2,$l3)  {
00283                 global $TYPO3_CONF_VARS;
00284 
00285                 if (!$TYPO3_CONF_VARS['GFX']['gdlib'])  die($l1.' '.$l2.' '.$l3);
00286 
00287                         // Creates the basis for the error image
00288                 if ($TYPO3_CONF_VARS['GFX']['gdlib_png'])       {
00289                         header('Content-type: image/png');
00290                         $im = imagecreatefrompng(PATH_typo3.'gfx/notfound_thumb.png');
00291                 } else {
00292                         header('Content-type: image/gif');
00293                         $im = imagecreatefromgif(PATH_typo3.'gfx/notfound_thumb.gif');
00294                 }
00295                         // Sets background color and print color.
00296                 $white = imageColorAllocate($im, 0,0,0);
00297                 $black = imageColorAllocate($im, 255,255,0);
00298 
00299                         // Prints the text strings with the build-in font functions of GD
00300                 $x=0;
00301                 $font=0;
00302                 if ($l1)        {
00303                         imagefilledrectangle($im, $x, 9, 56, 16, $black);
00304                         imageString($im,$font,$x,9,$l1,$white);
00305                 }
00306                 if ($l2)        {
00307                         imagefilledrectangle($im, $x, 19, 56, 26, $black);
00308                         imageString($im,$font,$x,19,$l2,$white);
00309                 }
00310                 if ($l3)        {
00311                         imagefilledrectangle($im, $x, 29, 56, 36, $black);
00312                         imageString($im,$font,$x,29,substr($l3,-14),$white);
00313                 }
00314 
00315                         // Outputting the image stream and exit
00316                 if ($TYPO3_CONF_VARS['GFX']['gdlib_png'])       {
00317                         imagePng($im);
00318                 } else {
00319                         imageGif($im);
00320                 }
00321                 imagedestroy($im);
00322                 exit;
00323         }
00324 
00334         function fontGif($font) {
00335                 global $TYPO3_CONF_VARS;
00336 
00337                 if (!$TYPO3_CONF_VARS['GFX']['gdlib'])  die('');
00338 
00339                         // Create image and set background color to white.
00340                 $im = imageCreate(250,76);
00341                 $white = imageColorAllocate($im, 255,255,255);
00342                 $col = imageColorAllocate($im, 0,0,0);
00343 
00344                         // The test string and offset in x-axis.
00345                 $string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzÆæØøÅåÄäÖöÜüß';
00346                 $x=13;
00347 
00348                         // Print (with non-ttf font) the size displayed
00349                 imagestring ($im, 1, 0, 2, '10', $col);
00350                 imagestring ($im, 1, 0, 15, '12', $col);
00351                 imagestring ($im, 1, 0, 30, '14', $col);
00352                 imagestring ($im, 1, 0, 47, '18', $col);
00353                 imagestring ($im, 1, 0, 68, '24', $col);
00354 
00355                         // Print with ttf-font the test string
00356                 imagettftext ($im, t3lib_div::freetypeDpiComp(10), 0, $x, 8, $col, $font, $string);
00357                 imagettftext ($im, t3lib_div::freetypeDpiComp(12), 0, $x, 21, $col, $font, $string);
00358                 imagettftext ($im, t3lib_div::freetypeDpiComp(14), 0, $x, 36, $col, $font, $string);
00359                 imagettftext ($im, t3lib_div::freetypeDpiComp(18), 0, $x, 53, $col, $font, $string);
00360                 imagettftext ($im, t3lib_div::freetypeDpiComp(24), 0, $x, 74, $col, $font, $string);
00361 
00362                         // Output PNG or GIF based on $TYPO3_CONF_VARS['GFX']['gdlib_png']
00363                 if ($TYPO3_CONF_VARS['GFX']['gdlib_png'])       {
00364                         header('Content-type: image/png');
00365                         imagePng($im);
00366                 } else {
00367                         header('Content-type: image/gif');
00368                         imageGif($im);
00369                 }
00370                 imagedestroy($im);
00371                 exit;
00372         }
00373 
00381         function wrapFileName($inputName)       {
00382                 if (strstr($inputName,' '))     {
00383                         $inputName='"'.$inputName.'"';
00384                 }
00385                 return $inputName;
00386         }
00387 }
00388 
00389 // Include extension class?
00390 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php'])        {
00391         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']);
00392 }
00393 
00394 
00395 
00396 
00397 // Make instance:
00398 $SOBE = t3lib_div::makeInstance('SC_t3lib_thumbs');
00399 $SOBE->init();
00400 $SOBE->main();
00401 ?>


Généré par Le spécialiste TYPO3 avec  doxygen 1.4.6