thumbs.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2010 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 ***************************************************************/
00027 /**
00028  * Generates a thumbnail and returns an image stream, either GIF/PNG or JPG
00029  *
00030  * $Id: thumbs.php 7905 2010-06-13 14:42:33Z ohader $
00031  * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
00032  *
00033  * @author      Kasper Skaarhoj <kasperYYYY@typo3.com>
00034  */
00035 /**
00036  * [CLASS/FUNCTION INDEX of SCRIPT]
00037  *
00038  *
00039  *
00040  *  113: class SC_t3lib_thumbs
00041  *  134:     function init()
00042  *  164:     function main()
00043  *
00044  *              SECTION: OTHER FUNCTIONS:
00045  *  267:     function errorGif($l1,$l2,$l3)
00046  *  319:     function fontGif($font)
00047  *  366:     function wrapFileName($inputName)
00048  *
00049  * TOTAL FUNCTIONS: 5
00050  * (This index is automatically created/updated by the extension "extdeveval")
00051  *
00052  */
00053 
00054 
00055 // *******************************
00056 // Set error reporting
00057 // *******************************
00058 if (defined('E_DEPRECATED')) {
00059     error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);
00060 } else {
00061     error_reporting(E_ALL ^ E_NOTICE);
00062 }
00063 
00064 
00065 
00066 // ******************
00067 // Constants defined
00068 // ******************
00069 define('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'');
00070 define('TYPO3_MODE','BE');
00071 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']))));
00072 if(!defined('PATH_site'))       define('PATH_site', preg_replace('/[^\/]*.[^\/]*$/','',PATH_thisScript));       // the path to the website folder (see init.php)
00073 if(!defined('PATH_t3lib'))      define('PATH_t3lib', PATH_site.'t3lib/');
00074 define('PATH_typo3conf', PATH_site.'typo3conf/');
00075 define('TYPO3_mainDir', 'typo3/');      // This is the directory of the backend administration for the sites of this TYPO3 installation.
00076 define('PATH_typo3', PATH_site.TYPO3_mainDir);
00077 
00078 
00079 // ******************
00080 // Including config
00081 // ******************
00082 require_once(PATH_t3lib.'class.t3lib_div.php');
00083 require_once(PATH_t3lib.'class.t3lib_extmgm.php');
00084 
00085 require(PATH_t3lib.'config_default.php');
00086 if (!defined ('TYPO3_db'))  die ('The configuration file was not included.');
00087 if (!$TYPO3_CONF_VARS['GFX']['image_processing'])   die ('ImageProcessing was disabled!');
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 /**
00106  * Class for generating a thumbnail from the input parameters given to the script
00107  *
00108  * Input GET var, &file:        relative or absolute reference to an imagefile. WILL be validated against PATH_site / lockRootPath
00109  * Input GET var, &size:        integer-values defining size of thumbnail, format '[int]' or '[int]x[int]'
00110  *
00111  * Relative paths MUST BE the first two characters ONLY: eg: '../dir/file.gif', otherwise it is expect to be absolute
00112  *
00113  * @author      Kasper Skaarhoj <kasperYYYY@typo3.com>
00114  * @package TYPO3
00115  * @subpackage t3lib
00116  */
00117 class SC_t3lib_thumbs {
00118     var $include_once = array();
00119 
00120     var $outdir = 'typo3temp/';     // The output directory of temporary files in PATH_site
00121     var $output = '';
00122     var $sizeDefault='56x56';
00123 
00124     var $imageList;     // Coming from $TYPO3_CONF_VARS['GFX']['imagefile_ext']
00125     var $input;     // Contains the absolute path to the file for which to make a thumbnail (after init())
00126 
00127         // Internal, static: GPvar:
00128     var $file;      // Holds the input filename (GET: file)
00129     var $size;      // Holds the input size (GET: size)
00130     var $mtime = 0;     // Last modification time of the supplied file
00131 
00132 
00133     /**
00134      * Initialize; reading parameters with GPvar and checking file path
00135      * Results in internal var, $this->input, being set to the absolute path of the file for which to make the thumbnail.
00136      *
00137      * @return  void
00138      */
00139     function init() {
00140         global $TYPO3_CONF_VARS;
00141 
00142             // Setting GPvars:
00143         $file = t3lib_div::_GP('file');
00144         $size = t3lib_div::_GP('size');
00145         $md5sum = t3lib_div::_GP('md5sum');
00146 
00147             // Image extension list is set:
00148         $this->imageList = $TYPO3_CONF_VARS['GFX']['imagefile_ext'];            // valid extensions. OBS: No spaces in the list, all lowercase...
00149 
00150             // If the filereference $this->file is relative, we correct the path
00151         if (substr($file,0,3)=='../')   {
00152             $file = PATH_site.substr($file,3);
00153         }
00154 
00155             // Now the path is absolute.
00156             // Checking for backpath and double slashes + the thumbnail can be made from files which are in the PATH_site OR the lockRootPath only!
00157         if (t3lib_div::isAllowedAbsPath($file)) {
00158             $mtime = filemtime($file);
00159         }
00160 
00161             // Do an MD5 check to prevent viewing of images without permission
00162         $OK = FALSE;
00163         if ($mtime) {
00164                 // Always use the absolute path for this check!
00165             $check = basename($file).':'.$mtime.':'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
00166             $md5_real = t3lib_div::shortMD5($check);
00167             if (!strcmp($md5_real,$md5sum)) {
00168                 $OK = TRUE;
00169             }
00170         }
00171 
00172         if ($OK)    {
00173             $this->input = $file;
00174             $this->size = $size;
00175             $this->mtime = $mtime;
00176         } else {
00177             throw new RuntimeException(
00178                 'TYPO3 Fatal Error: Image does not exist and/or MD5 checksum did not match.',
00179                 1270853950
00180             );
00181         }
00182     }
00183 
00184     /**
00185      * Create the thumbnail
00186      * Will exit before return if all is well.
00187      *
00188      * @return  void
00189      */
00190     function main() {
00191         global $TYPO3_CONF_VARS;
00192 
00193             // If file exists, we make a thumbsnail of the file.
00194         if ($this->input && file_exists($this->input))  {
00195 
00196                 // Check file extension:
00197             $reg = array();
00198             if (preg_match('/(.*)\.([^\.]*$)/',$this->input,$reg))  {
00199                 $ext=strtolower($reg[2]);
00200                 $ext=($ext=='jpeg')?'jpg':$ext;
00201                 if ($ext=='ttf')    {
00202                     $this->fontGif($this->input);   // Make font preview... (will not return)
00203                 } elseif (!t3lib_div::inList($this->imageList, $ext))   {
00204                     $this->errorGif('Not imagefile!',$ext,basename($this->input));
00205                 }
00206             } else {
00207                 $this->errorGif('Not imagefile!','No ext!',basename($this->input));
00208             }
00209 
00210                 // ... so we passed the extension test meaning that we are going to make a thumbnail here:
00211             if (!$this->size)   $this->size = $this->sizeDefault;   // default
00212 
00213                 // 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.
00214             $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)
00215             $sizeParts = array(t3lib_div::intInRange($sizeParts[0],1,1000),t3lib_div::intInRange($sizeParts[1],1,1000));    // Cleaning it up, only two parameters now.
00216             $this->size = implode('x',$sizeParts);      // Imploding the cleaned size-value back to the internal variable
00217             $sizeMax = max($sizeParts); // Getting max value
00218 
00219                 // Init
00220             $outpath = PATH_site.$this->outdir;
00221 
00222                 // Should be - ? 'png' : 'gif' - , but doesn't work (ImageMagick prob.?)
00223                 // René: png work for me
00224             $thmMode = t3lib_div::intInRange($TYPO3_CONF_VARS['GFX']['thumbnails_png'],0);
00225             $outext = ($ext!='jpg' || ($thmMode & 2)) ? ($thmMode & 1 ? 'png' : 'gif') : 'jpg';
00226 
00227             $outfile = 'tmb_'.substr(md5($this->input.$this->mtime.$this->size),0,10).'.'.$outext;
00228             $this->output = $outpath.$outfile;
00229 
00230             if ($TYPO3_CONF_VARS['GFX']['im'])  {
00231                     // If thumbnail does not exist, we generate it
00232                 if (!file_exists($this->output))    {
00233                     $parameters = '-sample ' . $this->size . ' ' . $this->wrapFileName($this->input) . '[0] ' . $this->wrapFileName($this->output);
00234                     $cmd = t3lib_div::imageMagickCommand('convert', $parameters);
00235                     exec($cmd);
00236                     if (!file_exists($this->output))    {
00237                         $this->errorGif('No thumb','generated!',basename($this->input));
00238                     }
00239                 }
00240                     // The thumbnail is read and output to the browser
00241                 if($fd = @fopen($this->output,'rb'))    {
00242                     header('Content-type: image/'.$outext);
00243                     fpassthru($fd);
00244                     fclose($fd);
00245                 } else {
00246                     $this->errorGif('Read problem!','',$this->output);
00247                 }
00248             } else exit;
00249         } else {
00250             $this->errorGif('No valid','inputfile!',basename($this->input));
00251         }
00252     }
00253 
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264     /***************************
00265      *
00266      * OTHER FUNCTIONS:
00267      *
00268      ***************************/
00269 
00270     /**
00271      * Creates error image based on gfx/notfound_thumb.png
00272      * Requires GD lib enabled, otherwise it will exit with the three textstrings outputted as text.
00273      * Outputs the image stream to browser and exits!
00274      *
00275      * @param   string      Text line 1
00276      * @param   string      Text line 2
00277      * @param   string      Text line 3
00278      * @return  void
00279      */
00280     function errorGif($l1,$l2,$l3)  {
00281         global $TYPO3_CONF_VARS;
00282 
00283         if (!$TYPO3_CONF_VARS['GFX']['gdlib']) {
00284             throw new RuntimeException(
00285                 'TYPO3 Fatal Error: No gdlib. ' . $l1 . ' ' . $l2 . ' ' . $l3,
00286                 1270853952
00287             );
00288         }
00289 
00290             // Creates the basis for the error image
00291         if ($TYPO3_CONF_VARS['GFX']['gdlib_png'])   {
00292             header('Content-type: image/png');
00293             $im = imagecreatefrompng(PATH_typo3.'gfx/notfound_thumb.png');
00294         } else {
00295             header('Content-type: image/gif');
00296             $im = imagecreatefromgif(PATH_typo3.'gfx/notfound_thumb.gif');
00297         }
00298             // Sets background color and print color.
00299         $white = imageColorAllocate($im, 0,0,0);
00300         $black = imageColorAllocate($im, 255,255,0);
00301 
00302             // Prints the text strings with the build-in font functions of GD
00303         $x=0;
00304         $font=0;
00305         if ($l1)    {
00306             imagefilledrectangle($im, $x, 9, 56, 16, $black);
00307             imageString($im,$font,$x,9,$l1,$white);
00308         }
00309         if ($l2)    {
00310             imagefilledrectangle($im, $x, 19, 56, 26, $black);
00311             imageString($im,$font,$x,19,$l2,$white);
00312         }
00313         if ($l3)    {
00314             imagefilledrectangle($im, $x, 29, 56, 36, $black);
00315             imageString($im,$font,$x,29,substr($l3,-14),$white);
00316         }
00317 
00318             // Outputting the image stream and exit
00319         if ($TYPO3_CONF_VARS['GFX']['gdlib_png'])   {
00320             imagePng($im);
00321         } else {
00322             imageGif($im);
00323         }
00324         imagedestroy($im);
00325         exit;
00326     }
00327 
00328     /**
00329      * Creates a font-preview thumbnail.
00330      * This means a PNG/GIF file with the text "AaBbCc...." set with the font-file given as input and in various sizes to show how the font looks
00331      * Requires GD lib enabled.
00332      * Outputs the image stream to browser and exits!
00333      *
00334      * @param   string      The filepath to the font file (absolute, probably)
00335      * @return  void
00336      */
00337     function fontGif($font) {
00338         global $TYPO3_CONF_VARS;
00339 
00340         if (!$TYPO3_CONF_VARS['GFX']['gdlib']) {
00341             throw new RuntimeException(
00342                 'TYPO3 Fatal Error: No gdlib.',
00343                 1270853953
00344             );
00345         }
00346 
00347             // Create image and set background color to white.
00348         $im = imageCreate(250,76);
00349         $white = imageColorAllocate($im, 255,255,255);
00350         $col = imageColorAllocate($im, 0,0,0);
00351 
00352             // The test string and offset in x-axis.
00353         $string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzÆæØøÅåÄäÖöÜüß';
00354         $x=13;
00355 
00356             // Print (with non-ttf font) the size displayed
00357         imagestring ($im, 1, 0, 2, '10', $col);
00358         imagestring ($im, 1, 0, 15, '12', $col);
00359         imagestring ($im, 1, 0, 30, '14', $col);
00360         imagestring ($im, 1, 0, 47, '18', $col);
00361         imagestring ($im, 1, 0, 68, '24', $col);
00362 
00363             // Print with ttf-font the test string
00364         imagettftext ($im, t3lib_div::freetypeDpiComp(10), 0, $x, 8, $col, $font, $string);
00365         imagettftext ($im, t3lib_div::freetypeDpiComp(12), 0, $x, 21, $col, $font, $string);
00366         imagettftext ($im, t3lib_div::freetypeDpiComp(14), 0, $x, 36, $col, $font, $string);
00367         imagettftext ($im, t3lib_div::freetypeDpiComp(18), 0, $x, 53, $col, $font, $string);
00368         imagettftext ($im, t3lib_div::freetypeDpiComp(24), 0, $x, 74, $col, $font, $string);
00369 
00370             // Output PNG or GIF based on $TYPO3_CONF_VARS['GFX']['gdlib_png']
00371         if ($TYPO3_CONF_VARS['GFX']['gdlib_png'])   {
00372             header('Content-type: image/png');
00373             imagePng($im);
00374         } else {
00375             header('Content-type: image/gif');
00376             imageGif($im);
00377         }
00378         imagedestroy($im);
00379         exit;
00380     }
00381 
00382     /**
00383      * Escapes a file name so it can safely be used on the command line.
00384      *
00385      * @param string $inputName filename to safeguard, must not be empty
00386      *
00387      * @return string $inputName escaped as needed
00388      */
00389     protected function wrapFileName($inputName) {
00390         return escapeshellarg($inputName);
00391     }
00392 }
00393 
00394 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php'])    {
00395     include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']);
00396 }
00397 
00398 
00399 
00400 
00401 // Make instance:
00402 $SOBE = t3lib_div::makeInstance('SC_t3lib_thumbs');
00403 $SOBE->init();
00404 $SOBE->main();
00405 
00406 ?>

Generated on Sat Jul 24 04:17:27 2010 for TYPO3 API by  doxygen 1.4.7