2 namespace TYPO3\CMS\Frontend\Imaging
;
4 /***************************************************************
7 * (c) 1999-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
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.
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.
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.
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
33 * Generating gif/png-files from TypoScript
34 * Used by the menu-objects and imgResource in TypoScript.
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.
41 * Here is an example of how to use this class (from tslib_content.php, function getImgResource):
43 * $gifCreator = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Imaging\\GifBuilder');
44 * $gifCreator->init();
46 * if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
47 * $gifCreator->start($fileArray, $this->data);
48 * $theImage = $gifCreator->gifBuild();
50 * return $gifCreator->getImageDimensions($theImage);
52 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
54 class GifBuilder
extends \TYPO3\CMS\Core\Imaging\GraphicalFunctions
{
59 * @todo Define visibility
65 * @todo Define visibility
71 * @todo Define visibility
77 * @todo Define visibility
82 * @todo Define visibility
86 // This holds the operational setup for gifbuilder. Basically this is a TypoScript array with properties.
88 * @todo Define visibility
90 public $setup = array();
92 // Contains all text strings used on this image
94 * @todo Define visibility
96 public $combinedTextStrings = array();
98 // Contains all filenames (basename without extension) used on this image
100 * @todo Define visibility
102 public $combinedFileNames = array();
104 // This is the array from which data->field: [key] is fetched. So this is the current record!
106 * @todo Define visibility
108 public $data = array();
111 * @todo Define visibility
113 public $objBB = array();
116 * @todo Define visibility
118 public $myClassName = 'gifbuilder';
121 * @todo Define visibility
123 public $charRangeMap = array();
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 \TYPO3\CMS\Frontend\ContentObject\Menu\GraphicalMenuContentObject 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.
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 \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer. Stored internally in the variable ->data
134 * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::getImgResource(), \TYPO3\CMS\Frontend\ContentObject\Menu\GraphicalMenuContentObject::makeGifs(), \TYPO3\CMS\Frontend\ContentObject\Menu\GraphicalMenuContentObject::findLargestDims()
135 * @todo Define visibility
137 public function start($conf, $data) {
138 if (is_array($conf)) {
139 $this->setup
= $conf;
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
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);
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)) {
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']);
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'];
176 if (!$this->setup
['backColor']) {
177 $this->setup
['backColor'] = 'white';
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']));
182 if (isset($this->setup
['transparentBackground.'])) {
183 $this->setup
['transparentBackground'] = $this->cObj
->stdWrap($this->setup
['transparentBackground'], $this->setup
['transparentBackground.']);
185 if (isset($this->setup
['reduceColors.'])) {
186 $this->setup
['reduceColors'] = $this->cObj
->stdWrap($this->setup
['reduceColors'], $this->setup
['reduceColors.']);
188 // Set default dimensions
189 if (isset($this->setup
['XY.'])) {
190 $this->setup
['XY'] = $this->cObj
->stdWrap($this->setup
['XY'], $this->setup
['XY.']);
192 if (!$this->setup
['XY']) {
193 $this->setup
['XY'] = '120,50';
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
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'];
207 $this->setup
[$theKey . '.']['fontSize'] = $this->fontResize($this->setup
[$theKey . '.']);
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;
217 $fileInfo = $this->getResource($conf['file'], $conf['file.']);
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;
224 $maskInfo = $this->getResource($conf['mask'], $conf['mask.']);
226 $this->setup
[$theKey . '.']['mask'] = $maskInfo[3];
228 $this->setup
[$theKey . '.']['mask'] = '';
232 unset($this->setup
[$theKey . '.']);
236 // Checks if disabled is set... (this is also done in menu.php / imgmenu!!)
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 . '.']);
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.']);
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.']);
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 . '.'])) {
264 if (isset($this->setup
[$theKey . '.']['offset.'])) {
265 $this->setup
[$theKey . '.']['offset'] = $this->cObj
->stdWrap($this->setup
[$theKey . '.']['offset'], $this->setup
[$theKey . '.']['offset.']);
267 if ($this->setup
[$theKey . '.']['offset']) {
268 $this->setup
[$theKey . '.']['offset'] = $this->calcOffset($this->setup
[$theKey . '.']['offset']);
274 if (isset($this->setup
[$theKey . '.']['dimensions.'])) {
275 $this->setup
[$theKey . '.']['dimensions'] = $this->cObj
->stdWrap($this->setup
[$theKey . '.']['dimensions'], $this->setup
[$theKey . '.']['dimensions.']);
277 if ($this->setup
[$theKey . '.']['dimensions']) {
278 $this->setup
[$theKey . '.']['dimensions'] = $this->calcOffset($this->setup
[$theKey . '.']['dimensions']);
282 if (isset($this->setup
[$theKey . '.']['set.'])) {
283 $this->setup
[$theKey . '.']['set'] = $this->cObj
->stdWrap($this->setup
[$theKey . '.']['set'], $this->setup
[$theKey . '.']['set.']);
285 if ($this->setup
[$theKey . '.']['set']) {
286 $this->setup
[$theKey . '.']['set'] = $this->calcOffset($this->setup
[$theKey . '.']['set']);
290 if (isset($this->setup
[$theKey . '.']['crop.'])) {
291 $this->setup
[$theKey . '.']['crop'] = $this->cObj
->stdWrap($this->setup
[$theKey . '.']['crop'], $this->setup
[$theKey . '.']['crop.']);
293 if ($this->setup
[$theKey . '.']['crop']) {
294 $this->setup
[$theKey . '.']['crop'] = $this->calcOffset($this->setup
[$theKey . '.']['crop']);
298 if (isset($this->setup
[$theKey . '.']['width.'])) {
299 $this->setup
[$theKey . '.']['width'] = $this->cObj
->stdWrap($this->setup
[$theKey . '.']['width'], $this->setup
[$theKey . '.']['width.']);
301 if ($this->setup
[$theKey . '.']['width']) {
302 $this->setup
[$theKey . '.']['width'] = $this->calcOffset($this->setup
[$theKey . '.']['width']);
304 if (isset($this->setup
[$theKey . '.']['height.'])) {
305 $this->setup
[$theKey . '.']['height'] = $this->cObj
->stdWrap($this->setup
[$theKey . '.']['height'], $this->setup
[$theKey . '.']['height.']);
307 if ($this->setup
[$theKey . '.']['height']) {
308 $this->setup
[$theKey . '.']['height'] = $this->calcOffset($this->setup
[$theKey . '.']['height']);
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);
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
;
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()
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
340 public function gifBuild() {
342 // Relative to PATH_site
343 $gifFileName = $this->fileName('GB/');
345 if (!file_exists($gifFileName)) {
346 // Create temporary directory if not done:
347 $this->createTempSubDir('GB/');
350 $this->output($gifFileName);
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()
366 * @todo Define visibility
368 public function make() {
371 // Reset internal properties
372 $this->saveAlphaLayer
= FALSE;
374 $this->im
= imagecreatetruecolor($XY[0], $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';
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);
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;
411 $this->maskImageOntoImage($this->im
, $conf, $this->workArea
);
413 $this->copyImageOntoImage($this->im
, $conf, $this->workArea
);
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;
427 $this->makeShadow($this->im
, $conf['shadow.'], $this->workArea
, $conf);
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;
438 $this->makeEmboss($this->im
, $conf['emboss.'], $this->workArea
, $conf);
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;
449 $this->makeOutline($this->im
, $conf['outline.'], $this->workArea
, $conf);
452 $this->makeText($this->im
, $conf, $this->workArea
);
456 if ($this->setup
[$conf['textObjNum']] == 'TEXT' && ($txtConf = $this->checkTextObj($this->setup
[$conf['textObjNum'] . '.']))) {
457 $this->makeOutline($this->im
, $conf, $this->workArea
, $txtConf);
461 if ($this->setup
[$conf['textObjNum']] == 'TEXT' && ($txtConf = $this->checkTextObj($this->setup
[$conf['textObjNum'] . '.']))) {
462 $this->makeEmboss($this->im
, $conf, $this->workArea
, $txtConf);
466 if ($this->setup
[$conf['textObjNum']] == 'TEXT' && ($txtConf = $this->checkTextObj($this->setup
[$conf['textObjNum'] . '.']))) {
467 $this->makeShadow($this->im
, $conf, $this->workArea
, $txtConf);
471 $this->makeBox($this->im
, $conf, $this->workArea
);
474 $this->makeEffect($this->im
, $conf);
477 $this->adjust($this->im
, $conf);
480 $this->crop($this->im
, $conf);
483 $this->scale($this->im
, $conf);
487 // this sets the workArea
488 $this->setWorkArea($conf['set']);
490 if (isset($conf['clear'])) {
491 // This sets the current to the default;
492 $this->workArea
= $this->defaultWorkArea
;
496 $this->makeEllipse($this->im
, $conf, $this->workArea
);
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']));
514 imagecolortransparent($this->im
, $Tcolor);
520 /*********************************************
522 * Various helper functions
524 ********************************************/
526 * Initializing/Cleaning of TypoScript properties for TEXT GIFBUILDER objects
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.
532 * @param array $conf GIFBUILDER object TypoScript properties
533 * @return array Modified $conf array IF the "text" property is not blank
535 * @todo Define visibility
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;
548 $conf['fontFile'] = $this->checkFile($conf['fontFile']);
549 if (!$conf['fontFile']) {
550 $conf['fontFile'] = 'typo3/sysext/core/Resources/Private/Font/nimbus.ttf';
552 if (!$conf['iterations']) {
553 $conf['iterations'] = 1;
555 if (!$conf['fontSize']) {
556 $conf['fontSize'] = 12;
558 // If any kind of spacing applys, we cannot use angles!!
559 if ($conf['spacing'] ||
$conf['wordSpacing']) {
562 if (!isset($conf['antiAlias'])) {
563 $conf['antiAlias'] = 1;
565 $conf['fontColor'] = trim($conf['fontColor']);
567 if (!$conf['doNotStripHTML']) {
568 $conf['text'] = strip_tags($conf['text']);
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);
577 $conf['text'] = substr($conf['text'], 0, $tlen);
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();
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']);
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']));
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']);
623 * Calculation of offset using "splitCalc" and insertion of dimensions from other GIFBUILDER objects.
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)
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
632 * @todo Define visibility
634 public function calcOffset($string) {
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);
641 $value[$key] = $this->calculateValue($val);
644 $string = implode(',', $value);
649 * Returns an "imgResource" creating an instance of the ContentObjectRenderer class and calling ContentObjectRenderer::getImgResource
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.
655 * @see \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::getImgResource()
656 * @todo Define visibility
658 public function getResource($file, $fileArray) {
659 if (!\TYPO3\CMS\Core\Utility\GeneralUtility
::inList($this->imageFileExt
, $fileArray['ext'])) {
660 $fileArray['ext'] = $this->gifExtension
;
662 $cObj = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
663 $cObj->start($this->data
);
664 return $cObj->getImgResource($file, $fileArray);
668 * Returns the reference to a "resource" in TypoScript.
670 * @param string $file The resource value.
671 * @return string Returns the relative filepath
673 * @see \TYPO3\CMS\Core\TypoScript\TemplateService::getFileName()
674 * @todo Define visibility
676 public function checkFile($file) {
677 return $GLOBALS['TSFE']->tmpl
->getFileName($file);
681 * Calculates the GIFBUILDER output filename/path based on a serialized, hashed value of this->setup
683 * @param string $pre Filename prefix, eg. "GB_
684 * @return string The relative filepath (relative to PATH_site)
686 * @todo Define visibility
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);
701 $meaningfulPrefix = substr($meaningfulPrefix, 0, $meaningfulPrefixLength);
703 $meaningfulPrefix .= '_';
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();
713 * Returns the file extension used in the filename
715 * @return string Extension; "jpg" or "gif"/"png
717 * @todo Define visibility
719 public function extension() {
720 switch (strtolower($this->setup
['format'])) {
733 return $this->gifExtension
;
739 * Calculates the value concerning the dimensions of objects.
741 * @param string $string The string to be calculated (e.g. "[20.h]+13")
742 * @return integer The calculated value (e.g. "23")
745 protected function calculateValue($string) {
746 $calculatedValue = 0;
747 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility
::splitCalc($string, '+-*/%');
748 foreach ($parts as $part) {
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));
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'];
764 $theVal = intval($theVal);
766 } elseif (floatval($theVal)) {
767 $theVal = floatval($theVal);
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;
783 return round($calculatedValue);
787 * Calculates special functions:
788 * + max([10.h], [20.h]) -> gets the maximum of the given values
790 * @param string $string The raw string with functions to be calculated
791 * @return string The calculated values
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);
803 * Calculates the maximum of a set of values defined like "[10.h],[20.h],1000"
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
808 protected function calculateMaximum($string) {
809 $parts = \TYPO3\CMS\Core\Utility\GeneralUtility
::trimExplode(',', $this->calcOffset($string), TRUE);
810 $maximum = count($parts) ?
max($parts) : 0;