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