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