PageRenderTime 66ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/t3lib/thumbs.php

https://bitbucket.org/linxpinx/mercurial
PHP | 406 lines | 264 code | 39 blank | 103 comment | 24 complexity | c686ac6683a125e17b12c9a24eb787ff MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Unlicense, LGPL-2.1, Apache-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 1999-2010 Kasper Skaarhoj (kasperYYYY@typo3.com)
  6. * All rights reserved
  7. *
  8. * This script is part of the TYPO3 project. The TYPO3 project is
  9. * free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * The GNU General Public License can be found at
  15. * http://www.gnu.org/copyleft/gpl.html.
  16. * A copy is found in the textfile GPL.txt and important notices to the license
  17. * from the author is found in LICENSE.txt distributed with these scripts.
  18. *
  19. *
  20. * This script is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * This copyright notice MUST APPEAR in all copies of the script!
  26. ***************************************************************/
  27. /**
  28. * Generates a thumbnail and returns an image stream, either GIF/PNG or JPG
  29. *
  30. * $Id: thumbs.php 7905 2010-06-13 14:42:33Z ohader $
  31. * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
  32. *
  33. * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
  34. */
  35. /**
  36. * [CLASS/FUNCTION INDEX of SCRIPT]
  37. *
  38. *
  39. *
  40. * 113: class SC_t3lib_thumbs
  41. * 134: function init()
  42. * 164: function main()
  43. *
  44. * SECTION: OTHER FUNCTIONS:
  45. * 267: function errorGif($l1,$l2,$l3)
  46. * 319: function fontGif($font)
  47. * 366: function wrapFileName($inputName)
  48. *
  49. * TOTAL FUNCTIONS: 5
  50. * (This index is automatically created/updated by the extension "extdeveval")
  51. *
  52. */
  53. // *******************************
  54. // Set error reporting
  55. // *******************************
  56. if (defined('E_DEPRECATED')) {
  57. error_reporting(E_ALL ^ E_NOTICE ^ E_DEPRECATED);
  58. } else {
  59. error_reporting(E_ALL ^ E_NOTICE);
  60. }
  61. // ******************
  62. // Constants defined
  63. // ******************
  64. define('TYPO3_OS', stristr(PHP_OS,'win')&&!stristr(PHP_OS,'darwin')?'WIN':'');
  65. define('TYPO3_MODE','BE');
  66. 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']))));
  67. if(!defined('PATH_site')) define('PATH_site', preg_replace('/[^\/]*.[^\/]*$/','',PATH_thisScript)); // the path to the website folder (see init.php)
  68. if(!defined('PATH_t3lib')) define('PATH_t3lib', PATH_site.'t3lib/');
  69. define('PATH_typo3conf', PATH_site.'typo3conf/');
  70. define('TYPO3_mainDir', 'typo3/'); // This is the directory of the backend administration for the sites of this TYPO3 installation.
  71. define('PATH_typo3', PATH_site.TYPO3_mainDir);
  72. // ******************
  73. // Including config
  74. // ******************
  75. require_once(PATH_t3lib.'class.t3lib_div.php');
  76. require_once(PATH_t3lib.'class.t3lib_extmgm.php');
  77. require(PATH_t3lib.'config_default.php');
  78. if (!defined ('TYPO3_db')) die ('The configuration file was not included.');
  79. if (!$TYPO3_CONF_VARS['GFX']['image_processing']) die ('ImageProcessing was disabled!');
  80. /**
  81. * Class for generating a thumbnail from the input parameters given to the script
  82. *
  83. * Input GET var, &file: relative or absolute reference to an imagefile. WILL be validated against PATH_site / lockRootPath
  84. * Input GET var, &size: integer-values defining size of thumbnail, format '[int]' or '[int]x[int]'
  85. *
  86. * Relative paths MUST BE the first two characters ONLY: eg: '../dir/file.gif', otherwise it is expect to be absolute
  87. *
  88. * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
  89. * @package TYPO3
  90. * @subpackage t3lib
  91. */
  92. class SC_t3lib_thumbs {
  93. var $include_once = array();
  94. var $outdir = 'typo3temp/'; // The output directory of temporary files in PATH_site
  95. var $output = '';
  96. var $sizeDefault='56x56';
  97. var $imageList; // Coming from $TYPO3_CONF_VARS['GFX']['imagefile_ext']
  98. var $input; // Contains the absolute path to the file for which to make a thumbnail (after init())
  99. // Internal, static: GPvar:
  100. var $file; // Holds the input filename (GET: file)
  101. var $size; // Holds the input size (GET: size)
  102. var $mtime = 0; // Last modification time of the supplied file
  103. /**
  104. * Initialize; reading parameters with GPvar and checking file path
  105. * Results in internal var, $this->input, being set to the absolute path of the file for which to make the thumbnail.
  106. *
  107. * @return void
  108. */
  109. function init() {
  110. global $TYPO3_CONF_VARS;
  111. // Setting GPvars:
  112. $file = t3lib_div::_GP('file');
  113. $size = t3lib_div::_GP('size');
  114. $md5sum = t3lib_div::_GP('md5sum');
  115. // Image extension list is set:
  116. $this->imageList = $TYPO3_CONF_VARS['GFX']['imagefile_ext']; // valid extensions. OBS: No spaces in the list, all lowercase...
  117. // If the filereference $this->file is relative, we correct the path
  118. if (substr($file,0,3)=='../') {
  119. $file = PATH_site.substr($file,3);
  120. }
  121. // Now the path is absolute.
  122. // Checking for backpath and double slashes + the thumbnail can be made from files which are in the PATH_site OR the lockRootPath only!
  123. if (t3lib_div::isAllowedAbsPath($file)) {
  124. $mtime = filemtime($file);
  125. }
  126. // Do an MD5 check to prevent viewing of images without permission
  127. $OK = FALSE;
  128. if ($mtime) {
  129. // Always use the absolute path for this check!
  130. $check = basename($file).':'.$mtime.':'.$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
  131. $md5_real = t3lib_div::shortMD5($check);
  132. if (!strcmp($md5_real,$md5sum)) {
  133. $OK = TRUE;
  134. }
  135. }
  136. if ($OK) {
  137. $this->input = $file;
  138. $this->size = $size;
  139. $this->mtime = $mtime;
  140. } else {
  141. throw new RuntimeException(
  142. 'TYPO3 Fatal Error: Image does not exist and/or MD5 checksum did not match.',
  143. 1270853950
  144. );
  145. }
  146. }
  147. /**
  148. * Create the thumbnail
  149. * Will exit before return if all is well.
  150. *
  151. * @return void
  152. */
  153. function main() {
  154. global $TYPO3_CONF_VARS;
  155. // If file exists, we make a thumbsnail of the file.
  156. if ($this->input && file_exists($this->input)) {
  157. // Check file extension:
  158. $reg = array();
  159. if (preg_match('/(.*)\.([^\.]*$)/',$this->input,$reg)) {
  160. $ext=strtolower($reg[2]);
  161. $ext=($ext=='jpeg')?'jpg':$ext;
  162. if ($ext=='ttf') {
  163. $this->fontGif($this->input); // Make font preview... (will not return)
  164. } elseif (!t3lib_div::inList($this->imageList, $ext)) {
  165. $this->errorGif('Not imagefile!',$ext,basename($this->input));
  166. }
  167. } else {
  168. $this->errorGif('Not imagefile!','No ext!',basename($this->input));
  169. }
  170. // ... so we passed the extension test meaning that we are going to make a thumbnail here:
  171. if (!$this->size) $this->size = $this->sizeDefault; // default
  172. // 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.
  173. $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)
  174. $sizeParts = array(t3lib_div::intInRange($sizeParts[0],1,1000),t3lib_div::intInRange($sizeParts[1],1,1000)); // Cleaning it up, only two parameters now.
  175. $this->size = implode('x',$sizeParts); // Imploding the cleaned size-value back to the internal variable
  176. $sizeMax = max($sizeParts); // Getting max value
  177. // Init
  178. $outpath = PATH_site.$this->outdir;
  179. // Should be - ? 'png' : 'gif' - , but doesn't work (ImageMagick prob.?)
  180. // René: png work for me
  181. $thmMode = t3lib_div::intInRange($TYPO3_CONF_VARS['GFX']['thumbnails_png'],0);
  182. $outext = ($ext!='jpg' || ($thmMode & 2)) ? ($thmMode & 1 ? 'png' : 'gif') : 'jpg';
  183. $outfile = 'tmb_'.substr(md5($this->input.$this->mtime.$this->size),0,10).'.'.$outext;
  184. $this->output = $outpath.$outfile;
  185. if ($TYPO3_CONF_VARS['GFX']['im']) {
  186. // If thumbnail does not exist, we generate it
  187. if (!file_exists($this->output)) {
  188. $parameters = '-sample ' . $this->size . ' ' . $this->wrapFileName($this->input) . '[0] ' . $this->wrapFileName($this->output);
  189. $cmd = t3lib_div::imageMagickCommand('convert', $parameters);
  190. exec($cmd);
  191. if (!file_exists($this->output)) {
  192. $this->errorGif('No thumb','generated!',basename($this->input));
  193. }
  194. }
  195. // The thumbnail is read and output to the browser
  196. if($fd = @fopen($this->output,'rb')) {
  197. header('Content-type: image/'.$outext);
  198. fpassthru($fd);
  199. fclose($fd);
  200. } else {
  201. $this->errorGif('Read problem!','',$this->output);
  202. }
  203. } else exit;
  204. } else {
  205. $this->errorGif('No valid','inputfile!',basename($this->input));
  206. }
  207. }
  208. /***************************
  209. *
  210. * OTHER FUNCTIONS:
  211. *
  212. ***************************/
  213. /**
  214. * Creates error image based on gfx/notfound_thumb.png
  215. * Requires GD lib enabled, otherwise it will exit with the three textstrings outputted as text.
  216. * Outputs the image stream to browser and exits!
  217. *
  218. * @param string Text line 1
  219. * @param string Text line 2
  220. * @param string Text line 3
  221. * @return void
  222. */
  223. function errorGif($l1,$l2,$l3) {
  224. global $TYPO3_CONF_VARS;
  225. if (!$TYPO3_CONF_VARS['GFX']['gdlib']) {
  226. throw new RuntimeException(
  227. 'TYPO3 Fatal Error: No gdlib. ' . $l1 . ' ' . $l2 . ' ' . $l3,
  228. 1270853952
  229. );
  230. }
  231. // Creates the basis for the error image
  232. if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
  233. header('Content-type: image/png');
  234. $im = imagecreatefrompng(PATH_typo3.'gfx/notfound_thumb.png');
  235. } else {
  236. header('Content-type: image/gif');
  237. $im = imagecreatefromgif(PATH_typo3.'gfx/notfound_thumb.gif');
  238. }
  239. // Sets background color and print color.
  240. $white = imageColorAllocate($im, 0,0,0);
  241. $black = imageColorAllocate($im, 255,255,0);
  242. // Prints the text strings with the build-in font functions of GD
  243. $x=0;
  244. $font=0;
  245. if ($l1) {
  246. imagefilledrectangle($im, $x, 9, 56, 16, $black);
  247. imageString($im,$font,$x,9,$l1,$white);
  248. }
  249. if ($l2) {
  250. imagefilledrectangle($im, $x, 19, 56, 26, $black);
  251. imageString($im,$font,$x,19,$l2,$white);
  252. }
  253. if ($l3) {
  254. imagefilledrectangle($im, $x, 29, 56, 36, $black);
  255. imageString($im,$font,$x,29,substr($l3,-14),$white);
  256. }
  257. // Outputting the image stream and exit
  258. if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
  259. imagePng($im);
  260. } else {
  261. imageGif($im);
  262. }
  263. imagedestroy($im);
  264. exit;
  265. }
  266. /**
  267. * Creates a font-preview thumbnail.
  268. * 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
  269. * Requires GD lib enabled.
  270. * Outputs the image stream to browser and exits!
  271. *
  272. * @param string The filepath to the font file (absolute, probably)
  273. * @return void
  274. */
  275. function fontGif($font) {
  276. global $TYPO3_CONF_VARS;
  277. if (!$TYPO3_CONF_VARS['GFX']['gdlib']) {
  278. throw new RuntimeException(
  279. 'TYPO3 Fatal Error: No gdlib.',
  280. 1270853953
  281. );
  282. }
  283. // Create image and set background color to white.
  284. $im = imageCreate(250,76);
  285. $white = imageColorAllocate($im, 255,255,255);
  286. $col = imageColorAllocate($im, 0,0,0);
  287. // The test string and offset in x-axis.
  288. $string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz??????ÄäÖöÜüß';
  289. $x=13;
  290. // Print (with non-ttf font) the size displayed
  291. imagestring ($im, 1, 0, 2, '10', $col);
  292. imagestring ($im, 1, 0, 15, '12', $col);
  293. imagestring ($im, 1, 0, 30, '14', $col);
  294. imagestring ($im, 1, 0, 47, '18', $col);
  295. imagestring ($im, 1, 0, 68, '24', $col);
  296. // Print with ttf-font the test string
  297. imagettftext ($im, t3lib_div::freetypeDpiComp(10), 0, $x, 8, $col, $font, $string);
  298. imagettftext ($im, t3lib_div::freetypeDpiComp(12), 0, $x, 21, $col, $font, $string);
  299. imagettftext ($im, t3lib_div::freetypeDpiComp(14), 0, $x, 36, $col, $font, $string);
  300. imagettftext ($im, t3lib_div::freetypeDpiComp(18), 0, $x, 53, $col, $font, $string);
  301. imagettftext ($im, t3lib_div::freetypeDpiComp(24), 0, $x, 74, $col, $font, $string);
  302. // Output PNG or GIF based on $TYPO3_CONF_VARS['GFX']['gdlib_png']
  303. if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
  304. header('Content-type: image/png');
  305. imagePng($im);
  306. } else {
  307. header('Content-type: image/gif');
  308. imageGif($im);
  309. }
  310. imagedestroy($im);
  311. exit;
  312. }
  313. /**
  314. * Escapes a file name so it can safely be used on the command line.
  315. *
  316. * @param string $inputName filename to safeguard, must not be empty
  317. *
  318. * @return string $inputName escaped as needed
  319. */
  320. protected function wrapFileName($inputName) {
  321. return escapeshellarg($inputName);
  322. }
  323. }
  324. if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']) {
  325. include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']);
  326. }
  327. // Make instance:
  328. $SOBE = t3lib_div::makeInstance('SC_t3lib_thumbs');
  329. $SOBE->init();
  330. $SOBE->main();
  331. ?>