Tons of changes made to the CORE. All scripts has more a less been modified. Primaril...
[Packages/TYPO3.CMS.git] / t3lib / thumbs.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2003 Kasper Skaarhoj (kasper@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$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
32 *
33 * @author Kasper Skaarhoj <kasper@typo3.com>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 110: class SC_t3lib_thumbs
41 * 131: function init()
42 * 161: function main()
43 *
44 * SECTION: OTHER FUNCTIONS:
45 * 264: function errorGif($l1,$l2,$l3)
46 * 316: function fontGif($font)
47 *
48 * TOTAL FUNCTIONS: 4
49 * (This index is automatically created/updated by the extension "extdeveval")
50 *
51 */
52
53
54 // *******************************
55 // Set error reporting
56 // *******************************
57 error_reporting (E_ALL ^ E_NOTICE);
58
59
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 define('PATH_thisScript',str_replace('//','/', str_replace('\\','/', php_sapi_name()=='cgi'||php_sapi_name()=='isapi' ? $HTTP_SERVER_VARS['PATH_TRANSLATED']:$HTTP_SERVER_VARS['SCRIPT_FILENAME'])));
67
68 define('PATH_site', ereg_replace('[^/]*.[^/]*$','',PATH_thisScript)); // the path to the website folder (see init.php)
69 define('PATH_typo3conf', PATH_site.'typo3conf/');
70 define('PATH_t3lib', PATH_site.'t3lib/');
71 define('TYPO3_mainDir', 'typo3/'); // This is the directory of the backend administration for the sites of this TYPO3 installation.
72
73 // ******************
74 // Including config
75 // ******************
76 require(PATH_t3lib.'class.t3lib_div.php');
77 require(PATH_t3lib.'class.t3lib_extmgm.php');
78 require(PATH_t3lib.'config_default.php');
79 if (!defined ('TYPO3_db')) die ('The configuration file was not included.');
80 if (!$TYPO3_CONF_VARS['GFX']['image_processing']) die ('ImageProcessing was disabled!');
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 /**
99 * Class for generating a thumbnail from the input parameters given to the script
100 *
101 * Input GET var, &file: relative or absolute reference to an imagefile. WILL be validated against PATH_site / lockRootPath
102 * Input GET var, &size: integer-values defining size of thumbnail, format '[int]' or '[int]x[int]'
103 *
104 * Relative paths MUST BE the first two characters ONLY: eg: '../dir/file.gif', otherwise it is expect to be absolute
105 *
106 * @author Kasper Skaarhoj <kasper@typo3.com>
107 * @package TYPO3
108 * @subpackage t3lib
109 */
110 class SC_t3lib_thumbs {
111 var $include_once=array();
112
113 var $outdir = 'typo3temp/'; // The output directory of temporary files in PATH_site
114 var $output = '';
115 var $sizeDefault='56x56';
116
117 var $imageList; // Coming from $TYPO3_CONF_VARS['GFX']['imagefile_ext']
118 var $input; // Contains the absolute path to the file for which to make a thumbnail (after init())
119
120 // Internal, static: GPvar:
121 var $file; // Holds the input filename (GET: file)
122 var $size; // Holds the input size (GET: size)
123
124
125 /**
126 * Initialize; reading parameters with GPvar and checking file path
127 * Results in internal var, $this->input, being set to the absolute path of the file for which to make the thumbnail.
128 *
129 * @return void
130 */
131 function init() {
132 global $TYPO3_CONF_VARS;
133
134 // Setting GPvars:
135 $this->file = t3lib_div::GPvar('file');
136 $this->size = t3lib_div::GPvar('size');
137
138 // Image extension list is set:
139 $this->imageList = $TYPO3_CONF_VARS['GFX']['imagefile_ext']; // valid extensions. OBS: No spaces in the list, all lowercase...
140
141 // if the filereference $this->file is relative, we correct the path
142 if (substr($this->file,0,3)=='../') {
143 $this->input = PATH_site.ereg_replace('^\.\./','',$this->file);
144 } else {
145 $this->input = $this->file;
146 }
147
148 // Now the path is absolute.
149 // Checking for backpath and double slashes + the thumbnail can be made from files which are in the PATH_site OR the lockRootPath only!
150 if (!t3lib_div::isAllowedAbsPath($this->input)) {
151 $this->input='';
152 }
153 }
154
155 /**
156 * Create the thumbnail
157 * Will exit before return if all is well.
158 *
159 * @return void
160 */
161 function main() {
162 global $TYPO3_CONF_VARS;
163
164 // If file exists, we make a thumbsnail of the file.
165 if ($this->input && @file_exists($this->input)) {
166
167 // Check file extension:
168 if (ereg('(.*)\.([^\.]*$)',$this->input,$reg)) {
169 $ext=strtolower($reg[2]);
170 $ext=($ext=='jpeg')?'jpg':$ext;
171 if ($ext=='ttf') {
172 $this->fontGif($this->input); // Make font preview... (will not return)
173 } elseif (!t3lib_div::inList($this->imageList, $ext)) {
174 $this->errorGif('Not imagefile!',$ext,$this->input);
175 }
176 } else {
177 $this->errorGif('Not imagefile!','No ext!',$this->input);
178 }
179
180 // ... so we passed the extension test meaning that we are going to make a thumbnail here:
181 $this->size = $this->size ? $this->size : $this->sizeDefault; // default
182
183 //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.
184 $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)
185 $sizeParts = array(t3lib_div::intInRange($sizeParts[0],1,1000),t3lib_div::intInRange($sizeParts[1],1,1000)); // Cleaning it up, only two parameters now.
186 $this->size = implode('x',$sizeParts); // Imploding the cleaned size-value back to the internal variable
187 $sizeMax = max($sizeParts); // Getting max value
188
189 // Init
190 $mtime = filemtime($this->input);
191 $outpath = PATH_site.$this->outdir;
192
193 // Should be - ? 'png' : 'gif' - , but doesn't work (ImageMagick prob.?)
194 // René: png work for me
195 $thmMode = t3lib_div::intInRange($TYPO3_CONF_VARS['GFX']['thumbnails_png'],0);
196 $outext = ($ext!='jpg' || ($thmMode & 2)) ? ($thmMode & 1 ? 'png' : 'gif') : 'jpg';
197
198 $outfile = 'tmb_'.substr(md5($this->input.$mtime.$this->size),0,10).'.'.$outext;
199 $this->output = $outpath.$outfile;
200
201 if ($TYPO3_CONF_VARS['GFX']['im']) {
202 // If thumbnail does not exist, we generate it
203 if (!@file_exists($this->output)) {
204 if (strstr($this->input,' ') || strstr($this->output,' ')) {
205 $this->errorGif('Spaces in','filepath',$this->input);
206 }
207 // 16 colors for small (56) thumbs, 64 for bigger and all for jpegs
208 if ($outext=='jpg') {
209 $colors = '';
210 } else {
211 $colors = ($sizeMax>56)?'-colors 64':'-colors 16';
212 }
213 $cmd = ($TYPO3_CONF_VARS['GFX']['im_path_lzw'] ? $TYPO3_CONF_VARS['GFX']['im_path_lzw'] : $TYPO3_CONF_VARS['GFX']['im_path']).
214 'convert -sample '.$this->size.' '.$colors.' '.$this->input.'[0] '.$this->output;
215
216 // echo $cmd;
217 exec($cmd);
218 if (!@file_exists($this->output)) {
219 $this->errorGif('No thumb','generated!',$this->input);
220 }
221 }
222 // The thumbnail is read and output to the browser
223 if($fd = @fopen($this->output,'rb')) {
224 Header('Content-type: image/'.$outext);
225 while (!feof($fd)) {
226 echo fread( $fd, 10000 );
227 }
228 fclose( $fd );
229 } else {
230 $this->errorGif('Read problem!','',$this->output);
231 }
232 } else exit;
233 } else {
234 $this->errorGif('No valid','inputfile!',$this->input);
235 }
236 }
237
238
239
240
241
242
243
244
245
246
247
248 /***************************
249 *
250 * OTHER FUNCTIONS:
251 *
252 ***************************/
253
254 /**
255 * Creates error image based on gfx/notfound_thumb.png
256 * Requires GD lib enabled, otherwise it will exit with the three textstrings outputted as text.
257 * Outputs the image stream to browser and exits!
258 *
259 * @param string Text line 1
260 * @param string Text line 2
261 * @param string Text line 3
262 * @return void
263 */
264 function errorGif($l1,$l2,$l3) {
265 global $TYPO3_CONF_VARS;
266
267 if (!$TYPO3_CONF_VARS['GFX']['gdlib']) die($l1.' '.$l2.' '.$l3);
268
269 // Creates the basis for the error image
270 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
271 Header('Content-type: image/png');
272 $im = imagecreatefrompng(PATH_t3lib.'gfx/notfound_thumb.png');
273 } else {
274 Header('Content-type: image/gif');
275 $im = imagecreatefromgif(PATH_t3lib.'gfx/notfound_thumb.gif');
276 }
277 // Sets background color and print color.
278 $white = ImageColorAllocate($im, 0,0,0);
279 $black = ImageColorAllocate($im, 255,255,0);
280
281 // Prints the text strings with the build-in font functions of GD
282 $x=0;
283 $font=0;
284 if ($l1) {
285 imagefilledrectangle($im, $x, 9, 56, 16, $black);
286 ImageString($im,$font,$x,9,$l1,$white);
287 }
288 if ($l2) {
289 imagefilledrectangle($im, $x, 19, 56, 26, $black);
290 ImageString($im,$font,$x,19,$l2,$white);
291 }
292 if ($l3) {
293 imagefilledrectangle($im, $x, 29, 56, 36, $black);
294 ImageString($im,$font,$x,29,substr($l3,-14),$white);
295 }
296
297 // Outputting the image stream and exit
298 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
299 imagePng($im);
300 } else {
301 imageGif($im);
302 }
303 imagedestroy($im);
304 exit;
305 }
306
307 /**
308 * Creates a font-preview thumbnail.
309 * 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
310 * Requires GD lib enabled.
311 * Outputs the image stream to browser and exits!
312 *
313 * @param string The filepath to the font file (absolute, probably)
314 * @return void
315 */
316 function fontGif($font) {
317 global $TYPO3_CONF_VARS;
318
319 if (!$TYPO3_CONF_VARS['GFX']['gdlib']) die('');
320
321 // Create image and set background color to white.
322 $im = ImageCreate(250,76);
323 $white = ImageColorAllocate($im, 255,255,255);
324 $col = ImageColorAllocate($im, 0,0,0);
325
326 // The test string and offset in x-axis.
327 $string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzÆæØøÅåÄäÖöÜüß';
328 $x=13;
329
330 // Print (with non-ttf font) the size displayed
331 imagestring ($im, 1, 0, 2, '10', $col);
332 imagestring ($im, 1, 0, 15, '12', $col);
333 imagestring ($im, 1, 0, 30, '14', $col);
334 imagestring ($im, 1, 0, 47, '18', $col);
335 imagestring ($im, 1, 0, 68, '24', $col);
336
337 // Print with ttf-font the test string
338 imagettftext ($im, t3lib_div::freetypeDpiComp(10), 0, $x, 8, $col, $font, $string);
339 imagettftext ($im, t3lib_div::freetypeDpiComp(12), 0, $x, 21, $col, $font, $string);
340 imagettftext ($im, t3lib_div::freetypeDpiComp(14), 0, $x, 36, $col, $font, $string);
341 imagettftext ($im, t3lib_div::freetypeDpiComp(18), 0, $x, 53, $col, $font, $string);
342 imagettftext ($im, t3lib_div::freetypeDpiComp(24), 0, $x, 74, $col, $font, $string);
343
344 // Output PNG or GIF based on $TYPO3_CONF_VARS['GFX']['gdlib_png']
345 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
346 Header('Content-type: image/png');
347 imagePng($im);
348 } else {
349 Header('Content-type: image/gif');
350 imageGif($im);
351 }
352 imagedestroy($im);
353 exit;
354 }
355 }
356
357 // Include extension class?
358 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']) {
359 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']);
360 }
361
362
363
364
365 // Make instance:
366 $SOBE = t3lib_div::makeInstance('SC_t3lib_thumbs');
367 $SOBE->init();
368 $SOBE->main();
369 ?>