Added $Id$ keywords, cleaned up comment tags
[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 * 128: function init()
42 * 155: function main()
43 *
44 * SECTION: OTHER FUNCTIONS:
45 * 258: function errorGif($l1,$l2,$l3)
46 * 310: 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 $file; // Holds the input filename (GET: file)
118 var $size; // Holds the input size (GET: size)
119 var $imageList; // Coming from $TYPO3_CONF_VARS['GFX']['imagefile_ext']
120 var $input; // Contains the absolute path to the file for which to make a thumbnail (after init())
121
122 /**
123 * Initialize; reading parameters with GPvar and checking file path
124 * Results in internal var, $this->input, being set to the absolute path of the file for which to make the thumbnail.
125 *
126 * @return void
127 */
128 function init() {
129 global $TYPO3_CONF_VARS,$HTTP_GET_VARS;
130
131 $this->imageList = $TYPO3_CONF_VARS['GFX']['imagefile_ext']; // valid extensions. OBS: No spaces in the list, all lowercase...
132 $this->file = t3lib_div::GPvar('file');
133 $this->size = t3lib_div::GPvar('size');
134
135 // if the filereference $this->file is relative, we correct the path
136 if (substr($this->file,0,3)=='../') {
137 $this->input = PATH_site.ereg_replace('^\.\./','',$this->file);
138 } else {
139 $this->input = $this->file;
140 }
141
142 // Now the path is absolute.
143 // Checking for backpath and double slashes + the thumbnail can be made from files which are in the PATH_site OR the lockRootPath only!
144 if (!t3lib_div::isAllowedAbsPath($this->input)) {
145 $this->input='';
146 }
147 }
148
149 /**
150 * Create the thumbnail
151 * Will exit before return if all is well.
152 *
153 * @return void
154 */
155 function main() {
156 global $TYPO3_CONF_VARS;
157
158 // If file exists, we make a thumbsnail of the file.
159 if ($this->input && @file_exists($this->input)) {
160
161 // Check file extension:
162 if (ereg('(.*)\.([^\.]*$)',$this->input,$reg)) {
163 $ext=strtolower($reg[2]);
164 $ext=($ext=='jpeg')?'jpg':$ext;
165 if ($ext=='ttf') {
166 $this->fontGif($this->input); // Make font preview... (will not return)
167 } elseif (!t3lib_div::inList($this->imageList, $ext)) {
168 $this->errorGif('Not imagefile!',$ext,$this->input);
169 }
170 } else {
171 $this->errorGif('Not imagefile!','No ext!',$this->input);
172 }
173
174 // ... so we passed the extension test meaning that we are going to make a thumbnail here:
175 $this->size = $this->size ? $this->size : $this->sizeDefault; // default
176
177 //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.
178 $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)
179 $sizeParts = array(t3lib_div::intInRange($sizeParts[0],1,1000),t3lib_div::intInRange($sizeParts[1],1,1000)); // Cleaning it up, only two parameters now.
180 $this->size = implode('x',$sizeParts); // Imploding the cleaned size-value back to the internal variable
181 $sizeMax = max($sizeParts); // Getting max value
182
183 // Init
184 $mtime = filemtime($this->input);
185 $outpath = PATH_site.$this->outdir;
186
187 // Should be - ? 'png' : 'gif' - , but doesn't work (ImageMagick prob.?)
188 // René: png work for me
189 $thmMode = t3lib_div::intInRange($TYPO3_CONF_VARS['GFX']['thumbnails_png'],0);
190 $outext = ($ext!='jpg' || ($thmMode & 2)) ? ($thmMode & 1 ? 'png' : 'gif') : 'jpg';
191
192 $outfile = 'tmb_'.substr(md5($this->input.$mtime.$this->size),0,10).'.'.$outext;
193 $this->output = $outpath.$outfile;
194
195 if ($TYPO3_CONF_VARS['GFX']['im']) {
196 // If thumbnail does not exist, we generate it
197 if (!@file_exists($this->output)) {
198 if (strstr($this->input,' ') || strstr($this->output,' ')) {
199 $this->errorGif('Spaces in','filepath',$this->input);
200 }
201 // 16 colors for small (56) thumbs, 64 for bigger and all for jpegs
202 if ($outext=='jpg') {
203 $colors = '';
204 } else {
205 $colors = ($sizeMax>56)?'-colors 64':'-colors 16';
206 }
207 $cmd = ($TYPO3_CONF_VARS['GFX']['im_path_lzw'] ? $TYPO3_CONF_VARS['GFX']['im_path_lzw'] : $TYPO3_CONF_VARS['GFX']['im_path']).
208 'convert -sample '.$this->size.' '.$colors.' '.$this->input.'[0] '.$this->output;
209
210 // echo $cmd;
211 exec($cmd);
212 if (!@file_exists($this->output)) {
213 $this->errorGif('No thumb','generated!',$this->input);
214 }
215 }
216 // The thumbnail is read and output to the browser
217 if($fd = @fopen($this->output,'rb')) {
218 Header('Content-type: image/'.$outext);
219 while (!feof($fd)) {
220 echo fread( $fd, 10000 );
221 }
222 fclose( $fd );
223 } else {
224 $this->errorGif('Read problem!','',$this->output);
225 }
226 } else exit;
227 } else {
228 $this->errorGif('No valid','inputfile!',$this->input);
229 }
230 }
231
232
233
234
235
236
237
238
239
240
241
242 /***************************
243 *
244 * OTHER FUNCTIONS:
245 *
246 ***************************/
247
248 /**
249 * Creates error image based on gfx/notfound_thumb.png
250 * Requires GD lib enabled, otherwise it will exit with the three textstrings outputted as text.
251 * Outputs the image stream to browser and exits!
252 *
253 * @param string Text line 1
254 * @param string Text line 2
255 * @param string Text line 3
256 * @return void
257 */
258 function errorGif($l1,$l2,$l3) {
259 global $TYPO3_CONF_VARS;
260
261 if (!$TYPO3_CONF_VARS['GFX']['gdlib']) die($l1.' '.$l2.' '.$l3);
262
263 // Creates the basis for the error image
264 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
265 Header('Content-type: image/png');
266 $im = imagecreatefrompng(PATH_t3lib.'gfx/notfound_thumb.png');
267 } else {
268 Header('Content-type: image/gif');
269 $im = imagecreatefromgif(PATH_t3lib.'gfx/notfound_thumb.gif');
270 }
271 // Sets background color and print color.
272 $white = ImageColorAllocate($im, 0,0,0);
273 $black = ImageColorAllocate($im, 255,255,0);
274
275 // Prints the text strings with the build-in font functions of GD
276 $x=0;
277 $font=0;
278 if ($l1) {
279 imagefilledrectangle($im, $x, 9, 56, 16, $black);
280 ImageString($im,$font,$x,9,$l1,$white);
281 }
282 if ($l2) {
283 imagefilledrectangle($im, $x, 19, 56, 26, $black);
284 ImageString($im,$font,$x,19,$l2,$white);
285 }
286 if ($l3) {
287 imagefilledrectangle($im, $x, 29, 56, 36, $black);
288 ImageString($im,$font,$x,29,substr($l3,-14),$white);
289 }
290
291 // Outputting the image stream and exit
292 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
293 imagePng($im);
294 } else {
295 imageGif($im);
296 }
297 imagedestroy($im);
298 exit;
299 }
300
301 /**
302 * Creates a font-preview thumbnail.
303 * 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
304 * Requires GD lib enabled.
305 * Outputs the image stream to browser and exits!
306 *
307 * @param string The filepath to the font file (absolute, probably)
308 * @return void
309 */
310 function fontGif($font) {
311 global $TYPO3_CONF_VARS;
312
313 if (!$TYPO3_CONF_VARS['GFX']['gdlib']) die('');
314
315 // Create image and set background color to white.
316 $im = ImageCreate(250,76);
317 $white = ImageColorAllocate($im, 255,255,255);
318 $col = ImageColorAllocate($im, 0,0,0);
319
320 // The test string and offset in x-axis.
321 $string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzÆæØøÅåÄäÖöÜüß';
322 $x=13;
323
324 // Print (with non-ttf font) the size displayed
325 imagestring ($im, 1, 0, 2, '10', $col);
326 imagestring ($im, 1, 0, 15, '12', $col);
327 imagestring ($im, 1, 0, 30, '14', $col);
328 imagestring ($im, 1, 0, 47, '18', $col);
329 imagestring ($im, 1, 0, 68, '24', $col);
330
331 // Print with ttf-font the test string
332 imagettftext ($im, t3lib_div::freetypeDpiComp(10), 0, $x, 8, $col, $font, $string);
333 imagettftext ($im, t3lib_div::freetypeDpiComp(12), 0, $x, 21, $col, $font, $string);
334 imagettftext ($im, t3lib_div::freetypeDpiComp(14), 0, $x, 36, $col, $font, $string);
335 imagettftext ($im, t3lib_div::freetypeDpiComp(18), 0, $x, 53, $col, $font, $string);
336 imagettftext ($im, t3lib_div::freetypeDpiComp(24), 0, $x, 74, $col, $font, $string);
337
338 // Output PNG or GIF based on $TYPO3_CONF_VARS['GFX']['gdlib_png']
339 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
340 Header('Content-type: image/png');
341 imagePng($im);
342 } else {
343 Header('Content-type: image/gif');
344 imageGif($im);
345 }
346 imagedestroy($im);
347 exit;
348 }
349 }
350
351 // Include extension class?
352 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']) {
353 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/thumbs.php']);
354 }
355
356
357
358
359 // Make instance:
360 $SOBE = t3lib_div::makeInstance('SC_t3lib_thumbs');
361 $SOBE->init();
362 $SOBE->main();
363 ?>