Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_gifbuilder.php
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 * Generating gif/png-files from TypoScript
29 * Used by the menu-objects and imgResource in TypoScript.
30 *
31 * $Id$
32 * Revised for TYPO3 3.6 June/2003 by Kasper Skaarhoj
33 *
34 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
35 */
36 /**
37 * [CLASS/FUNCTION INDEX of SCRIPT]
38 *
39 *
40 *
41 * 102: class tslib_gifBuilder extends t3lib_stdGraphic
42 * 129: function start($conf,$data)
43 * 315: function gifBuild()
44 * 343: function make()
45 *
46 * SECTION: Various helper functions
47 * 486: function checkTextObj($conf)
48 * 566: function calcOffset($string)
49 * 615: function getResource($file,$fileArray)
50 * 632: function checkFile($file)
51 * 643: function fileName($pre)
52 * 659: function extension()
53 *
54 * TOTAL FUNCTIONS: 9
55 * (This index is automatically created/updated by the extension "extdeveval")
56 *
57 */
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 /**
80 * GIFBUILDER extension class.
81 * This class allows for advanced rendering of images with various layers of images, text and graphical primitives.
82 * The concept is known from TypoScript as "GIFBUILDER" where you can define a "numerical array" (TypoScript term as well) of "GIFBUILDER OBJECTS" (like "TEXT", "IMAGE", etc.) and they will be rendered onto an image one by one.
83 * The name "GIFBUILDER" comes from the time where GIF was the only file format supported. PNG is just as well to create today (configured with TYPO3_CONF_VARS[GFX])
84 * Not all instances of this class is truely building gif/png files by layers; You may also see the class instantiated for the purpose of using the scaling functions in the parent class, t3lib_stdGraphic.
85 *
86 * Here is an example of how to use this class (from tslib_content.php, function getImgResource):
87 *
88 * $gifCreator = t3lib_div::makeInstance('tslib_gifbuilder');
89 * $gifCreator->init();
90 * $theImage='';
91 * if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
92 * $gifCreator->start($fileArray,$this->data);
93 * $theImage = $gifCreator->gifBuild();
94 * }
95 * return $gifCreator->getImageDimensions($theImage);
96 *
97 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
98 * @package TYPO3
99 * @subpackage tslib
100 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=377&cHash=e00ac666f3
101 */
102 class tslib_gifBuilder extends t3lib_stdGraphic {
103
104 // Internal
105 var $im = ''; // the main image
106 var $w = 0; // the image-width
107 var $h = 0; // the image-height
108 var $map; // map-data
109 var $workArea;
110 var $setup = Array (); // This holds the operational setup for gifbuilder. Basically this is a TypoScript array with properties.
111 var $combinedTextStrings = array(); // Contains all text strings used on this image
112 var $combinedFileNames = array(); // Contains all filenames (basename without extension) used on this image
113 var $data = Array(); // This is the array from which data->field: [key] is fetched. So this is the current record!
114 var $objBB = Array();
115 var $myClassName = 'gifbuilder';
116 var $charRangeMap=array();
117
118 /**
119 * Initialization of the GIFBUILDER objects, in particular TEXT and IMAGE. This includes finding the bounding box, setting dimensions and offset values before the actual rendering is started.
120 * Modifies the ->setup, ->objBB internal arrays
121 * Should be called after the ->init() function which initializes the parent class functions/variables in general.
122 * The class tslib_gmenu also uses gifbuilder and here there is an interesting use since the function findLargestDims() from that class calls the init() and start() functions to find the total dimensions before starting the rendering of the images.
123 *
124 * @param array TypoScript properties for the GIFBUILDER session. Stored internally in the variable ->setup
125 * @param array The current data record from tslib_cObj. Stored internally in the variable ->data
126 * @return void
127 * @see tslib_cObj::getImgResource(), tslib_gmenu::makeGifs(), tslib_gmenu::findLargestDims()
128 */
129 function start($conf,$data) {
130
131 if (is_array($conf)) {
132 $this->setup = $conf;
133 $this->data = $data;
134
135
136 /* Hook preprocess gifbuilder conf
137 * Added by Julle for 3.8.0
138 *
139 * Let's you pre-process the gifbuilder configuration. for
140 * example you can split a string up into lines and render each
141 * line as TEXT obj, see extension julle_gifbconf
142 */
143
144 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_gifbuilder.php']['gifbuilder-ConfPreProcess'])) {
145 foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_gifbuilder.php']['gifbuilder-ConfPreProcess'] as $_funcRef) {
146 $_params = $this->setup;
147 $this->setup = t3lib_div::callUserFunction($_funcRef,$_params,$this);
148 }
149 }
150
151 // Initializing global Char Range Map
152 $this->charRangeMap = array();
153 if (is_array($GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'])) {
154 foreach($GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'] as $cRMcfgkey => $cRMcfg) {
155 if (is_array($cRMcfg)) {
156
157 // Initializing:
158 $cRMkey = $GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'][substr($cRMcfgkey,0,-1)];
159 $this->charRangeMap[$cRMkey] = array();
160 $this->charRangeMap[$cRMkey]['charMapConfig'] = $cRMcfg['charMapConfig.'];
161 $this->charRangeMap[$cRMkey]['cfgKey'] = substr($cRMcfgkey,0,-1);
162 $this->charRangeMap[$cRMkey]['multiplicator'] = (double)$cRMcfg['fontSizeMultiplicator'];
163 $this->charRangeMap[$cRMkey]['pixelSpace'] = intval($cRMcfg['pixelSpaceFontSizeRef']);
164 }
165 }
166 }
167
168 // Getting sorted list of TypoScript keys from setup.
169 $sKeyArray=t3lib_TStemplate::sortedKeyList($this->setup);
170
171 // Setting the background color, passing it through stdWrap
172 if ($conf['backColor.'] || $conf['backColor']) {
173 $cObj =t3lib_div::makeInstance('tslib_cObj');
174 $cObj->start($this->data);
175 $this->setup['backColor'] = trim($cObj->stdWrap($this->setup['backColor'], $this->setup['backColor.']));
176 }
177 if (!$this->setup['backColor']) { $this->setup['backColor']='white'; }
178
179 if ($conf['transparentColor.'] || $conf['transparentColor']) {
180 $cObj =t3lib_div::makeInstance('tslib_cObj');
181 $cObj->start($this->data);
182 $this->setup['transparentColor_array'] = explode('|', trim($cObj->stdWrap($this->setup['transparentColor'], $this->setup['transparentColor.'])));
183 }
184
185 // Transparency does not properly work when, GIFs or 8-bit PNGs are generated or reduceColors is set -- disable truecolor flag so they get generated "natively" in 8-bit.
186 // not working with reduceColors and truecolor images
187 if (($this->setup['transparentBackground'] || is_array($this->setup['transparentColor_array'])) && ($this->gifExtension=='gif' || !$this->png_truecolor || isset($this->setup['reduceColors']))) {
188 $this->truecolor = false;
189 }
190
191 // Set default dimensions
192 if (!$this->setup['XY']) {$this->setup['XY']='120,50';}
193
194
195 // Checking TEXT and IMAGE objects for files. If any errors the objects are cleared.
196 // The Bounding Box for the objects is stored in an array
197 foreach($sKeyArray as $theKey) {
198 $theValue = $this->setup[$theKey];
199
200 if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
201 // Swipes through TEXT and IMAGE-objects
202 switch($theValue) {
203 case 'TEXT':
204 if ($this->setup[$theKey.'.'] = $this->checkTextObj($conf)) {
205
206 // Adjust font width if max size is set:
207 if ($this->setup[$theKey.'.']['maxWidth']) {
208 $this->setup[$theKey.'.']['fontSize'] = $this->fontResize($this->setup[$theKey.'.']); //RTF - this has to be done before calcBBox
209 }
210
211 // Calculate bounding box:
212 $txtInfo=$this->calcBBox($this->setup[$theKey.'.']);
213 $this->setup[$theKey.'.']['BBOX'] = $txtInfo;
214 $this->objBB[$theKey] = $txtInfo;
215 $this->setup[$theKey.'.']['imgMap'] = 0;
216 }
217 break;
218 case 'IMAGE':
219 $fileInfo = $this->getResource($conf['file'],$conf['file.']);
220 if ($fileInfo) {
221 $this->combinedFileNames[] = preg_replace('/\.[[:alnum:]]+$/','',basename($fileInfo[3]));
222 $this->setup[$theKey.'.']['file'] = $fileInfo[3];
223 $this->setup[$theKey.'.']['BBOX'] = $fileInfo;
224 $this->objBB[$theKey] = $fileInfo;
225 if ($conf['mask']) {
226 $maskInfo = $this->getResource($conf['mask'],$conf['mask.']);
227 if ($maskInfo) {
228 $this->setup[$theKey.'.']['mask'] = $maskInfo[3];
229 } else {
230 $this->setup[$theKey.'.']['mask'] = '';
231 }
232 }
233 } else {
234 unset($this->setup[$theKey.'.']);
235 }
236 break;
237 }
238 // Checks if disabled is set... (this is also done in menu.php / imgmenu!!)
239 if ($conf['if.']) {
240 $cObj =t3lib_div::makeInstance('tslib_cObj');
241 $cObj->start($this->data);
242
243 if (!$cObj->checkIf($conf['if.'])) {
244 unset($this->setup[$theKey]);
245 unset($this->setup[$theKey.'.']);
246 }
247 }
248 }
249 }
250
251 // Calculate offsets on elements
252 $this->setup['XY'] = $this->calcOffset($this->setup['XY']);
253 $this->setup['offset'] = $this->calcOffset($this->setup['offset']);
254 $this->setup['workArea'] = $this->calcOffset($this->setup['workArea']);
255
256 foreach ($sKeyArray as $theKey) {
257 $theValue=$this->setup[$theKey];
258
259 if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
260 switch($theValue) {
261 case 'TEXT':
262 case 'IMAGE':
263 if ($this->setup[$theKey.'.']['offset']) {
264 $this->setup[$theKey.'.']['offset'] = $this->calcOffset($this->setup[$theKey.'.']['offset']);
265 }
266 break;
267 case 'BOX':
268 if ($this->setup[$theKey.'.']['dimensions']) {
269 $this->setup[$theKey.'.']['dimensions'] = $this->calcOffset($this->setup[$theKey.'.']['dimensions']);
270 }
271 break;
272 case 'WORKAREA':
273 if ($this->setup[$theKey.'.']['set']) {
274 $this->setup[$theKey.'.']['set'] = $this->calcOffset($this->setup[$theKey.'.']['set']);
275 }
276 break;
277 case 'CROP':
278 if ($this->setup[$theKey.'.']['crop']) {
279 $this->setup[$theKey.'.']['crop'] = $this->calcOffset($this->setup[$theKey.'.']['crop']);
280 }
281 break;
282 case 'SCALE':
283 if ($this->setup[$theKey.'.']['width']) {
284 $this->setup[$theKey.'.']['width'] = $this->calcOffset($this->setup[$theKey.'.']['width']);
285 }
286 if ($this->setup[$theKey.'.']['height']) {
287 $this->setup[$theKey.'.']['height'] = $this->calcOffset($this->setup[$theKey.'.']['height']);
288 }
289 break;
290 }
291 }
292 }
293 // Get trivial data
294 $XY = t3lib_div::intExplode(',',$this->setup['XY']);
295 $maxWidth = intval($this->setup['maxWidth']);
296 $maxHeight = intval($this->setup['maxHeight']);
297
298 $XY[0] = t3lib_div::intInRange($XY[0],1, $maxWidth?$maxWidth:2000);
299 $XY[1] = t3lib_div::intInRange($XY[1],1, $maxHeight?$maxHeight:2000);
300 $this->XY = $XY;
301 $this->w = $XY[0];
302 $this->h = $XY[1];
303 $this->OFFSET = t3lib_div::intExplode(',',$this->setup['offset']);
304
305 $this->setWorkArea($this->setup['workArea']); // this sets the workArea
306 $this->defaultWorkArea = $this->workArea; // this sets the default to the current;
307 }
308 }
309
310 /**
311 * Initiates the image file generation if ->setup is true and if the file did not exist already.
312 * Gets filename from fileName() and if file exists in typo3temp/ dir it will - of course - not be rendered again.
313 * Otherwise rendering means calling ->make(), then ->output(), then ->destroy()
314 *
315 * @return string The filename for the created GIF/PNG file. The filename will be prefixed "GB_"
316 * @see make(), fileName()
317 */
318 function gifBuild() {
319 if ($this->setup) {
320 $gifFileName = $this->fileName('GB/'); // Relative to PATH_site
321 if (!file_exists($gifFileName)) { // File exists
322
323 // Create temporary directory if not done:
324 $this->createTempSubDir('GB/');
325
326 // Create file:
327 $this->make();
328 $this->output($gifFileName);
329 $this->destroy();
330 }
331 return $gifFileName;
332 }
333 }
334
335 /**
336 * The actual rendering of the image file.
337 * Basically sets the dimensions, the background color, the traverses the array of GIFBUILDER objects and finally setting the transparent color if defined.
338 * Creates a GDlib resource in $this->im and works on that
339 * Called by gifBuild()
340 *
341 * @return void
342 * @access private
343 * @see gifBuild()
344 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=378&cHash=3c2ae4a1ab
345 */
346 function make() {
347 // Get trivial data
348 $XY = $this->XY;
349
350 // Gif-start
351 $this->im = $this->imagecreate($XY[0],$XY[1]);
352 $this->w = $XY[0];
353 $this->h = $XY[1];
354
355 // backColor is set
356 $BGcols = $this->convertColor($this->setup['backColor']);
357 $Bcolor = ImageColorAllocate($this->im, $BGcols[0],$BGcols[1],$BGcols[2]);
358 ImageFilledRectangle($this->im, 0, 0, $XY[0], $XY[1], $Bcolor);
359
360 // Traverse the GIFBUILDER objects an render each one:
361 if (is_array($this->setup)) {
362 $sKeyArray=t3lib_TStemplate::sortedKeyList($this->setup);
363 foreach($sKeyArray as $theKey) {
364 $theValue=$this->setup[$theKey];
365
366 if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
367 switch($theValue) {
368 // Images
369 case 'IMAGE':
370 if ($conf['mask']) {
371 $this->maskImageOntoImage($this->im,$conf,$this->workArea);
372 } else {
373 $this->copyImageOntoImage($this->im,$conf,$this->workArea);
374 }
375 break;
376
377 // Text
378 case 'TEXT':
379 if (!$conf['hide']) {
380 if (is_array($conf['shadow.'])) {
381 $this->makeShadow($this->im,$conf['shadow.'],$this->workArea,$conf);
382 }
383 if (is_array($conf['emboss.'])) {
384 $this->makeEmboss($this->im,$conf['emboss.'],$this->workArea,$conf);
385 }
386 if (is_array($conf['outline.'])) {
387 $this->makeOutline($this->im,$conf['outline.'],$this->workArea,$conf);
388 }
389 $conf['imgMap']=1;
390 $this->makeText($this->im,$conf,$this->workArea);
391 }
392 break;
393
394 // Text effects:
395 case 'OUTLINE':
396 if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.'])) {
397 $this->makeOutline($this->im,$conf,$this->workArea,$txtConf);
398 }
399 break;
400 case 'EMBOSS':
401 if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.'])) {
402 $this->makeEmboss($this->im,$conf,$this->workArea,$txtConf);
403 }
404 break;
405 case 'SHADOW':
406 if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.'])) {
407 $this->makeShadow($this->im,$conf,$this->workArea,$txtConf);
408 }
409 break;
410
411 // Other
412 case 'BOX':
413 $this->makeBox($this->im,$conf,$this->workArea);
414 break;
415 case 'EFFECT':
416 $this->makeEffect($this->im,$conf);
417 break;
418 case 'ADJUST':
419 $this->adjust($this->im,$conf);
420 break;
421 case 'CROP':
422 $this->crop($this->im,$conf);
423 break;
424 case 'SCALE':
425 $this->scale($this->im,$conf);
426 break;
427 case 'WORKAREA':
428 if ($conf['set']) {
429 $this->setWorkArea($conf['set']); // this sets the workArea
430 }
431 if (isset($conf['clear'])) {
432 $this->workArea = $this->defaultWorkArea; // this sets the current to the default;
433 }
434 break;
435 }
436 }
437 }
438 }
439
440
441 if ($this->setup['transparentBackground']) {
442 // Auto transparent background is set
443 $Bcolor = ImageColorExact($this->im, $BGcols[0],$BGcols[1],$BGcols[2]);
444 imagecolortransparent($this->im, $Bcolor);
445 } elseif (is_array($this->setup['transparentColor_array'])) {
446 // Multiple transparent colors are set. This is done via the trick that all transparent colors get converted to one color and then this one gets set as transparent as png/gif can just have one transparent color.
447 $Tcolor = $this->unifyColors($this->im, $this->setup['transparentColor_array'], intval($this->setup['transparentColor.']['closest']));
448 if ($Tcolor>=0) {
449 imagecolortransparent($this->im, $Tcolor);
450 }
451 }
452
453 }
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472 /*********************************************
473 *
474 * Various helper functions
475 *
476 ********************************************/
477
478
479 /**
480 * Initializing/Cleaning of TypoScript properties for TEXT GIFBUILDER objects
481 *
482 * 'cleans' TEXT-object; Checks fontfile and other vital setup
483 * Finds the title if its a 'variable' (instantiates a cObj and loads it with the ->data record)
484 * Performs caseshift if any.
485 *
486 * @param array GIFBUILDER object TypoScript properties
487 * @return array Modified $conf array IF the "text" property is not blank
488 * @access private
489 */
490 function checkTextObj($conf) {
491 $conf['fontFile']=$this->checkFile($conf['fontFile']);
492 if (!$conf['fontFile']){$conf['fontFile']='t3lib/fonts/nimbus.ttf';}
493 if (!$conf['iterations']){$conf['iterations'] = 1;}
494 if (!$conf['fontSize']){$conf['fontSize']=12;}
495 if ($conf['spacing'] || $conf['wordSpacing']) { // If any kind of spacing applys, we cannot use angles!!
496 $conf['angle']=0;
497 }
498 if (!isset($conf['antiAlias'])){$conf['antiAlias']=1;}
499 $cObj =t3lib_div::makeInstance('tslib_cObj');
500 $cObj->start($this->data);
501
502 $conf['fontColor'] = trim($cObj->stdWrap($conf['fontColor'], $conf['fontColor.']));
503 $conf['text']=$cObj->stdWrap($conf['text'],$conf['text.']);
504 // Strip HTML
505 if (!$conf['doNotStripHTML']) {
506 $conf['text'] = strip_tags($conf['text']);
507 }
508 $this->combinedTextStrings[] = strip_tags($conf['text']);
509
510 // Max length = 100 if automatic line braks are not defined:
511 if (!isset($conf['breakWidth']) || !$conf['breakWidth']) {
512 $tlen = (intval($conf['textMaxLength']) ? intval($conf['textMaxLength']) : 100);
513 $conf['text'] = substr($conf['text'], 0, $tlen);
514 }
515 if ((string)$conf['text']!='') {
516
517 // Char range map thingie:
518 $fontBaseName = basename($conf['fontFile']);
519 if (is_array($this->charRangeMap[$fontBaseName])) {
520
521 // Initialize splitRendering array:
522 if (!is_array($conf['splitRendering.'])) {
523 $conf['splitRendering.'] = array();
524 }
525
526 $cfgK = $this->charRangeMap[$fontBaseName]['cfgKey'];
527 if (!isset($conf['splitRendering.'][$cfgK])) { // Do not impose settings if a splitRendering object already exists:
528 // Set configuration:
529 $conf['splitRendering.'][$cfgK] = 'charRange';
530 $conf['splitRendering.'][$cfgK.'.'] = $this->charRangeMap[$fontBaseName]['charMapConfig'];
531
532 // multiplicator of fontsize:
533 if ($this->charRangeMap[$fontBaseName]['multiplicator']) {
534 $conf['splitRendering.'][$cfgK.'.']['fontSize'] = round($conf['fontSize'] * $this->charRangeMap[$fontBaseName]['multiplicator']);
535 }
536 // multiplicator of pixelSpace:
537 if ($this->charRangeMap[$fontBaseName]['pixelSpace']) {
538 $travKeys = array('xSpaceBefore','xSpaceAfter','ySpaceBefore','ySpaceAfter');
539 foreach($travKeys as $pxKey) {
540 if (isset($conf['splitRendering.'][$cfgK.'.'][$pxKey])) {
541 $conf['splitRendering.'][$cfgK.'.'][$pxKey] = round($conf['splitRendering.'][$cfgK.'.'][$pxKey] * ($conf['fontSize'] / $this->charRangeMap[$fontBaseName]['pixelSpace']));
542 }
543 }
544 }
545 }
546 }
547 if (is_array($conf['splitRendering.'])) {
548 foreach($conf['splitRendering.'] as $key => $value) {
549 if (is_array($conf['splitRendering.'][$key])) {
550 if (isset($conf['splitRendering.'][$key]['fontFile'])) {
551 $conf['splitRendering.'][$key]['fontFile'] = $this->checkFile($conf['splitRendering.'][$key]['fontFile']);
552 }
553 }
554 }
555 }
556
557 return $conf;
558 }
559 }
560
561 /**
562 * Calculation of offset using "splitCalc" and insertion of dimensions from other GIFBUILDER objects.
563 *
564 * Example:
565 * Input: 2+2, 2*3, 123, [10.w]
566 * Output: 4,6,123,45 (provided that the width of object in position 10 was 45 pixels wide)
567 *
568 * @param string The string to resolve/calculate the result of. The string is divided by a comma first and each resulting part is calculated into an integer.
569 * @return string The resolved string with each part (separated by comma) returned separated by comma
570 * @access private
571 */
572 function calcOffset($string) {
573 $value = array();
574 $numbers = t3lib_div::trimExplode(',', $this->calculateFunctions($string));
575
576 foreach ($numbers as $key => $val) {
577 if ((string)$val == (string)intval($val)) {
578 $value[$key] = intval($val);
579 } else {
580 $value[$key] = $this->calculateValue($val);
581 }
582 }
583
584 $string = implode(',', $value);
585 return $string;
586 }
587
588 /**
589 * Returns an "imgResource" creating an instance of the tslib_cObj class and calling tslib_cObj::getImgResource
590 *
591 * @param string Filename value OR the string "GIFBUILDER", see documentation in TSref for the "datatype" called "imgResource"
592 * @param array TypoScript properties passed to the function. Either GIFBUILDER properties or imgResource properties, depending on the value of $file (whether that is "GIFBUILDER" or a file reference)
593 * @return array Returns an array with file information if an image was returned. Otherwise false.
594 * @access private
595 * @see tslib_cObj::getImgResource()
596 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=315&cHash=63b593a934
597 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=282&cHash=831a95115d
598 */
599 function getResource($file,$fileArray) {
600 if (!t3lib_div::inList($this->imageFileExt, $fileArray['ext'])) {
601 $fileArray['ext'] = $this->gifExtension;
602 }
603 $cObj =t3lib_div::makeInstance('tslib_cObj');
604 $cObj->start($this->data);
605 return $cObj->getImgResource($file,$fileArray);
606 }
607
608 /**
609 * Returns the reference to a "resource" in TypoScript.
610 *
611 * @param string The resource value.
612 * @return string Returns the relative filepath
613 * @access private
614 * @see t3lib_TStemplate::getFileName()
615 */
616 function checkFile($file) {
617 return $GLOBALS['TSFE']->tmpl->getFileName($file);
618 }
619
620 /**
621 * Calculates the GIFBUILDER output filename/path based on a serialized, hashed value of this->setup
622 *
623 * @param string Filename prefix, eg. "GB_"
624 * @return string The relative filepath (relative to PATH_site)
625 * @access private
626 */
627 function fileName($pre) {
628
629 $meaningfulPrefix = '';
630
631 if ($GLOBALS['TSFE']->config['config']['meaningfulTempFilePrefix']) {
632 $meaningfulPrefix = implode('_', array_merge($this->combinedTextStrings, $this->combinedFileNames));
633 // strip everything non-ascii
634 $meaningfulPrefix = preg_replace('/[^A-Za-z0-9_-]/', '', trim($meaningfulPrefix));
635 $meaningfulPrefix = substr($meaningfulPrefix, 0, intval($GLOBALS['TSFE']->config['config']['meaningfulTempFilePrefix'])) . '_';
636 }
637
638 // WARNING: In PHP5 I discovered that rendering with freetype of Japanese letters was totally corrupt. Not only the wrong glyphs are printed but also some memory stack overflow resulted in strange additional chars - and finally the reason for this investigation: The Bounding box data was changing all the time resulting in new images being generated all the time. With PHP4 it works fine.
639 return $this->tempPath.
640 $pre.
641 $meaningfulPrefix .
642 t3lib_div::shortMD5(serialize($this->setup)).
643 '.'.$this->extension();
644 }
645
646 /**
647 * Returns the file extension used in the filename
648 *
649 * @return string Extension; "jpg" or "gif"/"png"
650 * @access private
651 */
652 function extension() {
653 switch(strtolower($this->setup['format'])) {
654 case 'jpg':
655 case 'jpeg':
656 return 'jpg';
657 break;
658 case 'png':
659 return 'png';
660 break;
661 case 'gif':
662 return 'gif';
663 break;
664 default:
665 return $this->gifExtension;
666 break;
667 }
668 }
669
670 /**
671 * Calculates the value concerning the dimensions of objects.
672 *
673 * @param string $string: The string to be calculated (e.g. "[20.h]+13")
674 * @return integer The calculated value (e.g. "23")
675 * @see calcOffset()
676 */
677 protected function calculateValue($string) {
678 $calculatedValue = 0;
679 $parts = t3lib_div::splitCalc($string, '+-*/%');
680
681 foreach ($parts as $part) {
682 $theVal = $part[1];
683 $sign = $part[0];
684
685 if ((string)intval($theVal) == (string)$theVal) {
686 $theVal = intval($theVal);
687 } elseif ('[' . substr($theVal, 1, -1) . ']' == $theVal) {
688 $objParts = explode('.', substr($theVal, 1, -1));
689 $theVal = 0;
690 if (isset($this->objBB[$objParts[0]])) {
691 if ($objParts[1] == 'w') {
692 $theVal = $this->objBB[$objParts[0]][0];
693 } elseif ($objParts[1] == 'h') {
694 $theVal = $this->objBB[$objParts[0]][1];
695 } elseif ($objParts[1] == 'lineHeight') {
696 $theVal = $this->objBB[$objParts[0]][2]['lineHeight'];
697 }
698 $theVal = intval($theVal);
699 }
700 } elseif (floatval($theVal)) {
701 $theVal = floatval($theVal);
702 } else {
703 $theVal = 0;
704 }
705
706 if ($sign == '-') {
707 $calculatedValue-= $theVal;
708 } elseif ($sign == '+') {
709 $calculatedValue+= $theVal;
710 } elseif ($sign == '/' && $theVal) {
711 $calculatedValue = $calculatedValue / $theVal;
712 } elseif ($sign == '*') {
713 $calculatedValue = $calculatedValue * $theVal;
714 } elseif ($sign == '%' && $theVal) {
715 $calculatedValue%= $theVal;
716 }
717 }
718
719 return round($calculatedValue);
720 }
721
722 /**
723 * Calculates special functions:
724 * + max([10.h], [20.h]) -> gets the maximum of the given values
725 *
726 * @param string $string: The raw string with functions to be calculated
727 * @return string The calculated values
728 */
729 protected function calculateFunctions($string) {
730 if (preg_match_all('#max\(([^)]+)\)#', $string, $matches)) {
731 foreach ($matches[1] as $index => $maxExpression) {
732 $string = str_replace(
733 $matches[0][$index],
734 $this->calculateMaximum(
735 $maxExpression
736 ),
737 $string
738 );
739 }
740 }
741
742 return $string;
743 }
744
745 /**
746 * Calculates the maximum of a set of values defined like "[10.h],[20.h],1000"
747 *
748 * @param string $string: The string to be used to calculate the maximum (e.g. "[10.h],[20.h],1000")
749 * @return integer The maxium value of the given comma separated and calculated values
750 */
751 protected function calculateMaximum($string) {
752 $parts = t3lib_div::trimExplode(',', $this->calcOffset($string), true);
753 $maximum = (count($parts) ? max($parts) : 0);
754 return $maximum;
755 }
756 }
757
758
759 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_gifbuilder.php']) {
760 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_gifbuilder.php']);
761 }
762
763 ?>