[BUGFIX] Illegal string offsets in t3lib_stdgraphic
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / GraphicalFunctions.php
1 <?php
2 namespace TYPO3\CMS\Core\Imaging;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 1999-2011 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 * Standard graphical functions
31 *
32 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
33 *
34 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
35 */
36 /**
37 * Class contains a bunch of cool functions for manipulating graphics with GDlib/Freetype and ImageMagick
38 * VERY OFTEN used with gifbuilder that extends this class and provides a TypoScript API to using these functions
39 *
40 * With TYPO3 4.4 GDlib 1.x support was dropped, also an option from $TYPO3_CONF_VARS
41 * $TYPO3_CONF_VARS['GFX']['gdlib_2'] = 0, // String/Boolean. Set this if you are using the new GDlib 2.0.1+. If you don't set this flag and still use GDlib2, you might encounter strange behaviours like black images etc. This feature might take effect only if ImageMagick is installed and working as well! You can also use the value "no_imagecopyresized_fix" - in that case it will NOT try to fix a known issue where "imagecopyresized" does not work correctly.
42 *
43 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
44 * @package TYPO3
45 * @subpackage t3lib
46 * @see tslib_gifBuilder
47 */
48 class GraphicalFunctions {
49
50 // Internal configuration, set in init()
51 // The ImageMagick filename used for combining two images. This name changed during the versions.
52 /**
53 * @todo Define visibility
54 */
55 public $combineScript = 'combine';
56
57 // If set, there is no frame pointer prepended to the filenames.
58 /**
59 * @todo Define visibility
60 */
61 public $noFramePrepended = 0;
62
63 // If set, imagecopyresized will not be called directly. For GD2 (some PHP installs?)
64 /**
65 * @todo Define visibility
66 */
67 public $imagecopyresized_fix = 0;
68
69 // This should be changed to 'png' if you want this class to read/make PNG-files instead!
70 /**
71 * @todo Define visibility
72 */
73 public $gifExtension = 'gif';
74
75 // File formats supported by gdlib. This variable get's filled in "init" method
76 /**
77 * @todo Define visibility
78 */
79 public $gdlibExtensions = '';
80
81 // Set to TRUE if generated png's should be truecolor by default
82 /**
83 * @todo Define visibility
84 */
85 public $png_truecolor = FALSE;
86
87 // 16777216 Colors is the maximum value for PNG, JPEG truecolor images (24-bit, 8-bit / Channel)
88 /**
89 * @todo Define visibility
90 */
91 public $truecolorColors = 16777215;
92
93 // If set, then all files in typo3temp will be logged in a database table. In addition to being a log of the files with original filenames, it also serves to secure that the same image is not rendered simultaneously by two different processes.
94 /**
95 * @todo Define visibility
96 */
97 public $enable_typo3temp_db_tracking = 0;
98
99 // Commalist of file extensions perceived as images by TYPO3. List should be set to 'gif,png,jpeg,jpg' if IM is not available. Lowercase and no spaces between!
100 /**
101 * @todo Define visibility
102 */
103 public $imageFileExt = 'gif,jpg,jpeg,png,tif,bmp,tga,pcx,ai,pdf';
104
105 // Commalist of web image extensions (can be shown by a webbrowser)
106 /**
107 * @todo Define visibility
108 */
109 public $webImageExt = 'gif,jpg,jpeg,png';
110
111 // Will be ' -negate' if ImageMagick ver 5.2+. See init();
112 /**
113 * @todo Define visibility
114 */
115 public $maskNegate = '';
116
117 /**
118 * @todo Define visibility
119 */
120 public $NO_IM_EFFECTS = '';
121
122 /**
123 * @todo Define visibility
124 */
125 public $cmds = array(
126 'jpg' => '',
127 'jpeg' => '',
128 'gif' => '',
129 'png' => '-colors 64'
130 );
131
132 /**
133 * @todo Define visibility
134 */
135 public $NO_IMAGE_MAGICK = '';
136
137 /**
138 * @todo Define visibility
139 */
140 public $V5_EFFECTS = 0;
141
142 /**
143 * @todo Define visibility
144 */
145 public $mayScaleUp = 1;
146
147 // Variables for testing, alternative usage etc.
148 // Filename prefix for images scaled in imageMagickConvert()
149 /**
150 * @todo Define visibility
151 */
152 public $filenamePrefix = '';
153
154 // Forcing the output filename of imageMagickConvert() to this value. However after calling imageMagickConvert() it will be set blank again.
155 /**
156 * @todo Define visibility
157 */
158 public $imageMagickConvert_forceFileNameBody = '';
159
160 // This flag should always be FALSE. If set TRUE, imageMagickConvert will always write a new file to the tempdir! Used for debugging.
161 /**
162 * @todo Define visibility
163 */
164 public $dontCheckForExistingTempFile = 0;
165
166 // Prevents imageMagickConvert() from compressing the gif-files with t3lib_div::gif_compress()
167 /**
168 * @todo Define visibility
169 */
170 public $dontCompress = 0;
171
172 // For debugging ONLY!
173 /**
174 * @todo Define visibility
175 */
176 public $dontUnlinkTempFiles = 0;
177
178 // For debugging only. Filenames will not be based on mtime and only filename (not path) will be used. This key is also included in the hash of the filename...
179 /**
180 * @todo Define visibility
181 */
182 public $alternativeOutputKey = '';
183
184 // Internal:
185 // All ImageMagick commands executed is stored in this array for tracking. Used by the Install Tools Image section
186 /**
187 * @todo Define visibility
188 */
189 public $IM_commands = array();
190
191 /**
192 * @todo Define visibility
193 */
194 public $workArea = array();
195
196 /**
197 * Preserve the alpha transparency layer of read PNG images
198 *
199 * @var boolean
200 */
201 protected $saveAlphaLayer = FALSE;
202
203 // Constants:
204 // The temp-directory where to store the files. Normally relative to PATH_site but is allowed to be the absolute path AS LONG AS it is a subdir to PATH_site.
205 /**
206 * @todo Define visibility
207 */
208 public $tempPath = 'typo3temp/';
209
210 // Prefix for relative paths. Used in "show_item.php" script. Is prefixed the output file name IN imageMagickConvert()
211 /**
212 * @todo Define visibility
213 */
214 public $absPrefix = '';
215
216 // ImageMagick scaling command; "-geometry" eller "-sample". Used in makeText() and imageMagickConvert()
217 /**
218 * @todo Define visibility
219 */
220 public $scalecmd = '-geometry';
221
222 // Used by v5_blur() to simulate 10 continuous steps of blurring
223 /**
224 * @todo Define visibility
225 */
226 public $im5fx_blurSteps = '1x2,2x2,3x2,4x3,5x3,5x4,6x4,7x5,8x5,9x5';
227
228 // Used by v5_sharpen() to simulate 10 continuous steps of sharpening.
229 /**
230 * @todo Define visibility
231 */
232 public $im5fx_sharpenSteps = '1x2,2x2,3x2,2x3,3x3,4x3,3x4,4x4,4x5,5x5';
233
234 // This is the limit for the number of pixels in an image before it will be rendered as JPG instead of GIF/PNG
235 /**
236 * @todo Define visibility
237 */
238 public $pixelLimitGif = 10000;
239
240 // Array mapping HTML color names to RGB values.
241 /**
242 * @todo Define visibility
243 */
244 public $colMap = array(
245 'aqua' => array(0, 255, 255),
246 'black' => array(0, 0, 0),
247 'blue' => array(0, 0, 255),
248 'fuchsia' => array(255, 0, 255),
249 'gray' => array(128, 128, 128),
250 'green' => array(0, 128, 0),
251 'lime' => array(0, 255, 0),
252 'maroon' => array(128, 0, 0),
253 'navy' => array(0, 0, 128),
254 'olive' => array(128, 128, 0),
255 'purple' => array(128, 0, 128),
256 'red' => array(255, 0, 0),
257 'silver' => array(192, 192, 192),
258 'teal' => array(0, 128, 128),
259 'yellow' => array(255, 255, 0),
260 'white' => array(255, 255, 255)
261 );
262
263 /**
264 * Charset conversion object:
265 *
266 * @var \TYPO3\CMS\Core\Charset\CharsetConverter
267 * @todo Define visibility
268 */
269 public $csConvObj;
270
271 // Is set to the native character set of the input strings.
272 /**
273 * @todo Define visibility
274 */
275 public $nativeCharset = '';
276
277 /**
278 * Init function. Must always call this when using the class.
279 * This function will read the configuration information from $GLOBALS['TYPO3_CONF_VARS']['GFX'] can set some values in internal variables.
280 *
281 * @return void
282 * @todo Define visibility
283 */
284 public function init() {
285 $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
286 if (function_exists('imagecreatefromjpeg') && function_exists('imagejpeg')) {
287 $this->gdlibExtensions .= ',jpg,jpeg';
288 }
289 if (function_exists('imagecreatefrompng') && function_exists('imagepng')) {
290 $this->gdlibExtensions .= ',png';
291 }
292 if (function_exists('imagecreatefromgif') && function_exists('imagegif')) {
293 $this->gdlibExtensions .= ',gif';
294 }
295 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['png_truecolor']) {
296 $this->png_truecolor = TRUE;
297 }
298 if (!$gfxConf['im']) {
299 $this->NO_IMAGE_MAGICK = 1;
300 }
301 if (!$this->NO_IMAGE_MAGICK && (!$gfxConf['im_version_5'] || $gfxConf['im_version_5'] === 'im4' || $gfxConf['im_version_5'] === 'im5')) {
302 throw new \RuntimeException('Your TYPO3 installation is configured to use an old version of ImageMagick, which is not supported anymore. ' . 'Please upgrade to ImageMagick version 6 or GraphicksMagick and set $TYPO3_CONF_VARS[\'GFX\'][\'im_version_5\'] appropriately.', 1305059666);
303 }
304 // When GIFBUILDER gets used in truecolor mode
305 // No colors parameter if we generate truecolor images.
306 if ($this->png_truecolor) {
307 $this->cmds['png'] = '';
308 }
309 // Setting default JPG parameters:
310 $this->jpegQuality = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($gfxConf['jpg_quality'], 10, 100, 75);
311 $this->cmds['jpg'] = ($this->cmds['jpeg'] = '-colorspace RGB -sharpen 50 -quality ' . $this->jpegQuality);
312 if ($gfxConf['im_combine_filename']) {
313 $this->combineScript = $gfxConf['im_combine_filename'];
314 }
315 if ($gfxConf['im_noFramePrepended']) {
316 $this->noFramePrepended = 1;
317 }
318 // Kept for backwards compatibility, can be turned on manually through localconf.php,
319 // but not through the installer anymore
320 $this->imagecopyresized_fix = $gfxConf['gdlib_2'] === 'no_imagecopyresized_fix' ? 0 : 1;
321 if ($gfxConf['gdlib_png']) {
322 $this->gifExtension = 'png';
323 }
324 if ($gfxConf['enable_typo3temp_db_tracking']) {
325 $this->enable_typo3temp_db_tracking = $gfxConf['enable_typo3temp_db_tracking'];
326 }
327 $this->imageFileExt = $gfxConf['imagefile_ext'];
328 // This should be set if ImageMagick ver. 5+ is used.
329 if ($gfxConf['im_negate_mask']) {
330 // Boolean. Indicates if the mask images should be inverted first.
331 // This depends of the ImageMagick version. Below ver. 5.1 this should be FALSE.
332 // Above ImageMagick version 5.2+ it should be TRUE.
333 // Just set the flag if the masks works opposite the intension!
334 $this->maskNegate = ' -negate';
335 }
336 if ($gfxConf['im_no_effects']) {
337 // Boolean. This is necessary if using ImageMagick 5+.
338 // Effects in Imagemagick 5+ tends to render very slowly!!
339 // - therefore must be disabled in order not to perform sharpen, blurring and such.
340 $this->NO_IM_EFFECTS = 1;
341 $this->cmds['jpg'] = ($this->cmds['jpeg'] = '-colorspace RGB -quality ' . $this->jpegQuality);
342 }
343 // ... but if 'im_v5effects' is set, don't care about 'im_no_effects'
344 if ($gfxConf['im_v5effects']) {
345 $this->NO_IM_EFFECTS = 0;
346 $this->V5_EFFECTS = 1;
347 if ($gfxConf['im_v5effects'] > 0) {
348 $this->cmds['jpg'] = ($this->cmds['jpeg'] = '-colorspace RGB -quality ' . intval($gfxConf['jpg_quality']) . $this->v5_sharpen(10));
349 }
350 }
351 // Secures that images are not scaled up.
352 if ($gfxConf['im_noScaleUp']) {
353 $this->mayScaleUp = 0;
354 }
355 if (TYPO3_MODE == 'FE') {
356 $this->csConvObj = $GLOBALS['TSFE']->csConvObj;
357 } elseif (is_object($GLOBALS['LANG'])) {
358 // BE assumed:
359 $this->csConvObj = $GLOBALS['LANG']->csConvObj;
360 } else {
361 // The object may not exist yet, so we need to create it now. Happens in the Install Tool for example.
362 $this->csConvObj = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Charset\\CharsetConverter');
363 }
364 $this->nativeCharset = 'utf-8';
365 }
366
367 /*************************************************
368 *
369 * Layering images / "IMAGE" GIFBUILDER object
370 *
371 *************************************************/
372 /**
373 * Implements the "IMAGE" GIFBUILDER object, when the "mask" property is TRUE.
374 * It reads the two images defined by $conf['file'] and $conf['mask'] and copies the $conf['file'] onto the input image pointer image using the $conf['mask'] as a grayscale mask
375 * The operation involves ImageMagick for combining.
376 *
377 * @param pointer $im GDlib image pointer
378 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
379 * @param array $workArea The current working area coordinates.
380 * @return void
381 * @see tslib_gifBuilder::make()
382 * @todo Define visibility
383 */
384 public function maskImageOntoImage(&$im, $conf, $workArea) {
385 if ($conf['file'] && $conf['mask']) {
386 $imgInf = pathinfo($conf['file']);
387 $imgExt = strtolower($imgInf['extension']);
388 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::inList($this->gdlibExtensions, $imgExt)) {
389 $BBimage = $this->imageMagickConvert($conf['file'], $this->gifExtension);
390 } else {
391 $BBimage = $this->getImageDimensions($conf['file']);
392 }
393 $maskInf = pathinfo($conf['mask']);
394 $maskExt = strtolower($maskInf['extension']);
395 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::inList($this->gdlibExtensions, $maskExt)) {
396 $BBmask = $this->imageMagickConvert($conf['mask'], $this->gifExtension);
397 } else {
398 $BBmask = $this->getImageDimensions($conf['mask']);
399 }
400 if ($BBimage && $BBmask) {
401 $w = imagesx($im);
402 $h = imagesy($im);
403 $tmpStr = $this->randomName();
404 $theImage = $tmpStr . '_img.' . $this->gifExtension;
405 $theDest = $tmpStr . '_dest.' . $this->gifExtension;
406 $theMask = $tmpStr . '_mask.' . $this->gifExtension;
407 // Prepare overlay image
408 $cpImg = $this->imageCreateFromFile($BBimage[3]);
409 $destImg = imagecreatetruecolor($w, $h);
410 // Preserve alpha transparency
411 if ($this->saveAlphaLayer) {
412 imagesavealpha($destImg, TRUE);
413 $Bcolor = imagecolorallocatealpha($destImg, 0, 0, 0, 127);
414 imagefill($destImg, 0, 0, $Bcolor);
415 } else {
416 $Bcolor = ImageColorAllocate($destImg, 0, 0, 0);
417 ImageFilledRectangle($destImg, 0, 0, $w, $h, $Bcolor);
418 }
419 $this->copyGifOntoGif($destImg, $cpImg, $conf, $workArea);
420 $this->ImageWrite($destImg, $theImage);
421 imageDestroy($cpImg);
422 imageDestroy($destImg);
423 // Prepare mask image
424 $cpImg = $this->imageCreateFromFile($BBmask[3]);
425 $destImg = imagecreatetruecolor($w, $h);
426 if ($this->saveAlphaLayer) {
427 imagesavealpha($destImg, TRUE);
428 $Bcolor = imagecolorallocatealpha($destImg, 0, 0, 0, 127);
429 imagefill($destImg, 0, 0, $Bcolor);
430 } else {
431 $Bcolor = ImageColorAllocate($destImg, 0, 0, 0);
432 ImageFilledRectangle($destImg, 0, 0, $w, $h, $Bcolor);
433 }
434 $this->copyGifOntoGif($destImg, $cpImg, $conf, $workArea);
435 $this->ImageWrite($destImg, $theMask);
436 imageDestroy($cpImg);
437 imageDestroy($destImg);
438 // Mask the images
439 $this->ImageWrite($im, $theDest);
440 // Let combineExec handle maskNegation
441 $this->combineExec($theDest, $theImage, $theMask, $theDest, TRUE);
442 // The main image is loaded again...
443 $backIm = $this->imageCreateFromFile($theDest);
444 // ... and if nothing went wrong we load it onto the old one.
445 if ($backIm) {
446 if (!$this->saveAlphaLayer) {
447 ImageColorTransparent($backIm, -1);
448 }
449 $im = $backIm;
450 }
451 // Unlink files from process
452 if (!$this->dontUnlinkTempFiles) {
453 unlink($theDest);
454 unlink($theImage);
455 unlink($theMask);
456 }
457 }
458 }
459 }
460
461 /**
462 * Implements the "IMAGE" GIFBUILDER object, when the "mask" property is FALSE (using only $conf['file'])
463 *
464 * @param pointer $im GDlib image pointer
465 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
466 * @param array $workArea The current working area coordinates.
467 * @return void
468 * @see tslib_gifBuilder::make(), maskImageOntoImage()
469 * @todo Define visibility
470 */
471 public function copyImageOntoImage(&$im, $conf, $workArea) {
472 if ($conf['file']) {
473 if (!\TYPO3\CMS\Core\Utility\GeneralUtility::inList($this->gdlibExtensions, $conf['BBOX'][2])) {
474 $conf['BBOX'] = $this->imageMagickConvert($conf['BBOX'][3], $this->gifExtension);
475 $conf['file'] = $conf['BBOX'][3];
476 }
477 $cpImg = $this->imageCreateFromFile($conf['file']);
478 $this->copyGifOntoGif($im, $cpImg, $conf, $workArea);
479 imageDestroy($cpImg);
480 }
481 }
482
483 /**
484 * Copies two GDlib image pointers onto each other, using TypoScript configuration from $conf and the input $workArea definition.
485 *
486 * @param pointer $im GDlib image pointer, destination (bottom image)
487 * @param pointer $cpImg GDlib image pointer, source (top image)
488 * @param array $conf TypoScript array with the properties for the IMAGE GIFBUILDER object. Only used for the "tile" property value.
489 * @param array $workArea Work area
490 * @return void Works on the $im image pointer
491 * @access private
492 * @todo Define visibility
493 */
494 public function copyGifOntoGif(&$im, $cpImg, $conf, $workArea) {
495 $cpW = imagesx($cpImg);
496 $cpH = imagesy($cpImg);
497 $tile = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $conf['tile']);
498 $tile[0] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($tile[0], 1, 20);
499 $tile[1] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($tile[1], 1, 20);
500 $cpOff = $this->objPosition($conf, $workArea, array($cpW * $tile[0], $cpH * $tile[1]));
501 for ($xt = 0; $xt < $tile[0]; $xt++) {
502 $Xstart = $cpOff[0] + $cpW * $xt;
503 // If this image is inside of the workArea, then go on
504 if ($Xstart + $cpW > $workArea[0]) {
505 // X:
506 if ($Xstart < $workArea[0]) {
507 $cpImgCutX = $workArea[0] - $Xstart;
508 $Xstart = $workArea[0];
509 } else {
510 $cpImgCutX = 0;
511 }
512 $w = $cpW - $cpImgCutX;
513 if ($Xstart > $workArea[0] + $workArea[2] - $w) {
514 $w = $workArea[0] + $workArea[2] - $Xstart;
515 }
516 // If this image is inside of the workArea, then go on
517 if ($Xstart < $workArea[0] + $workArea[2]) {
518 // Y:
519 for ($yt = 0; $yt < $tile[1]; $yt++) {
520 $Ystart = $cpOff[1] + $cpH * $yt;
521 // If this image is inside of the workArea, then go on
522 if ($Ystart + $cpH > $workArea[1]) {
523 if ($Ystart < $workArea[1]) {
524 $cpImgCutY = $workArea[1] - $Ystart;
525 $Ystart = $workArea[1];
526 } else {
527 $cpImgCutY = 0;
528 }
529 $h = $cpH - $cpImgCutY;
530 if ($Ystart > $workArea[1] + $workArea[3] - $h) {
531 $h = $workArea[1] + $workArea[3] - $Ystart;
532 }
533 // If this image is inside of the workArea, then go on
534 if ($Ystart < $workArea[1] + $workArea[3]) {
535 $this->imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
536 }
537 }
538 }
539 }
540 }
541 }
542 }
543
544 /**
545 * Alternative function for using the similar PHP function imagecopyresized(). Used for GD2 only.
546 *
547 * OK, the reason for this stupid fix is the following story:
548 * GD1.x was capable of copying two images together and combining their palettes! GD2 is apparently not.
549 * With GD2 only the palette of the dest-image is used which mostly results in totally black images when trying to
550 * copy a color-ful image onto the destination.
551 * The GD2-fix is to
552 * 1) Create a blank TRUE-COLOR image
553 * 2) Copy the destination image onto that one
554 * 3) Then do the actual operation; Copying the source (top image) onto that
555 * 4) ... and return the result pointer.
556 * 5) Reduce colors (if we do not, the result may become strange!)
557 * It works, but the resulting images is now a true-color PNG which may be very large.
558 * So, why not use 'imagetruecolortopalette ($im, TRUE, 256)' - well because it does NOT WORK! So simple is that.
559 *
560 * @param resource $dstImg Destination image
561 * @param resource $srcImg Source image
562 * @param integer $dstX Destination x-coordinate
563 * @param integer $dstY Destination y-coordinate
564 * @param integer $srcX Source x-coordinate
565 * @param integer $srcY Source y-coordinate
566 * @param integer $dstWidth Destination width
567 * @param integer $dstHeight Destination height
568 * @param integer $srcWidth Source width
569 * @param integer $srcHeight Source height
570 * @return void
571 * @access private
572 * @see t3lib_iconWorks::imagecopyresized()
573 * @todo Define visibility
574 */
575 public function imagecopyresized(&$dstImg, $srcImg, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight) {
576 if ($this->imagecopyresized_fix && !$this->saveAlphaLayer) {
577 // Make true color image
578 $tmpImg = imagecreatetruecolor(imagesx($dstImg), imagesy($dstImg));
579 // Copy the source image onto that
580 imagecopyresized($tmpImg, $dstImg, 0, 0, 0, 0, imagesx($dstImg), imagesy($dstImg), imagesx($dstImg), imagesy($dstImg));
581 // Then copy the source image onto that (the actual operation!)
582 imagecopyresized($tmpImg, $srcImg, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
583 // Set the destination image
584 $dstImg = $tmpImg;
585 } else {
586 imagecopyresized($dstImg, $srcImg, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
587 }
588 }
589
590 /********************************
591 *
592 * Text / "TEXT" GIFBUILDER object
593 *
594 ********************************/
595 /**
596 * Implements the "TEXT" GIFBUILDER object
597 *
598 * @param pointer $im GDlib image pointer
599 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
600 * @param array $workArea The current working area coordinates.
601 * @return void
602 * @see tslib_gifBuilder::make()
603 * @todo Define visibility
604 */
605 public function makeText(&$im, $conf, $workArea) {
606 // Spacing
607 list($spacing, $wordSpacing) = $this->calcWordSpacing($conf);
608 // Position
609 $txtPos = $this->txtPosition($conf, $workArea, $conf['BBOX']);
610 $theText = $this->recodeString($conf['text']);
611 if ($conf['imgMap'] && is_array($conf['imgMap.'])) {
612 $this->addToMap($this->calcTextCordsForMap($conf['BBOX'][2], $txtPos, $conf['imgMap.']), $conf['imgMap.']);
613 }
614 if (!$conf['hideButCreateMap']) {
615 // Font Color:
616 $cols = $this->convertColor($conf['fontColor']);
617 // NiceText is calculated
618 if (!$conf['niceText']) {
619 $Fcolor = ImageColorAllocate($im, $cols[0], $cols[1], $cols[2]);
620 // antiAliasing is setup:
621 $Fcolor = $conf['antiAlias'] ? $Fcolor : -$Fcolor;
622 for ($a = 0; $a < $conf['iterations']; $a++) {
623 // If any kind of spacing applys, we use this function:
624 if ($spacing || $wordSpacing) {
625 $this->SpacedImageTTFText($im, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, self::prependAbsolutePath($conf['fontFile']), $theText, $spacing, $wordSpacing, $conf['splitRendering.']);
626 } else {
627 $this->renderTTFText($im, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, $conf['fontFile'], $theText, $conf['splitRendering.'], $conf);
628 }
629 }
630 } else {
631 // NICETEXT::
632 // options anti_aliased and iterations is NOT available when doing this!!
633 $w = imagesx($im);
634 $h = imagesy($im);
635 $tmpStr = $this->randomName();
636 $fileMenu = $tmpStr . '_menuNT.' . $this->gifExtension;
637 $fileColor = $tmpStr . '_colorNT.' . $this->gifExtension;
638 $fileMask = $tmpStr . '_maskNT.' . $this->gifExtension;
639 // Scalefactor
640 $sF = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($conf['niceText.']['scaleFactor'], 2, 5);
641 $newW = ceil($sF * imagesx($im));
642 $newH = ceil($sF * imagesy($im));
643 // Make mask
644 $maskImg = imagecreatetruecolor($newW, $newH);
645 $Bcolor = ImageColorAllocate($maskImg, 255, 255, 255);
646 ImageFilledRectangle($maskImg, 0, 0, $newW, $newH, $Bcolor);
647 $Fcolor = ImageColorAllocate($maskImg, 0, 0, 0);
648 // If any kind of spacing applies, we use this function:
649 if ($spacing || $wordSpacing) {
650 $this->SpacedImageTTFText($maskImg, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, self::prependAbsolutePath($conf['fontFile']), $theText, $spacing, $wordSpacing, $conf['splitRendering.'], $sF);
651 } else {
652 $this->renderTTFText($maskImg, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, $conf['fontFile'], $theText, $conf['splitRendering.'], $conf, $sF);
653 }
654 $this->ImageWrite($maskImg, $fileMask);
655 ImageDestroy($maskImg);
656 // Downscales the mask
657 if ($this->NO_IM_EFFECTS) {
658 if ($this->maskNegate) {
659 // Negate 2 times makes no negate...
660 $command = trim($this->scalecmd . ' ' . $w . 'x' . $h . '!');
661 } else {
662 $command = trim($this->scalecmd . ' ' . $w . 'x' . $h . '! -negate');
663 }
664 } else {
665 if ($this->maskNegate) {
666 $command = trim($conf['niceText.']['before'] . ' ' . $this->scalecmd . ' ' . $w . 'x' . $h . '! ' . $conf['niceText.']['after']);
667 } else {
668 $command = trim($conf['niceText.']['before'] . ' ' . $this->scalecmd . ' ' . $w . 'x' . $h . '! ' . $conf['niceText.']['after'] . ' -negate');
669 }
670 if ($conf['niceText.']['sharpen']) {
671 if ($this->V5_EFFECTS) {
672 $command .= $this->v5_sharpen($conf['niceText.']['sharpen']);
673 } else {
674 $command .= ' -sharpen ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($conf['niceText.']['sharpen'], 1, 99);
675 }
676 }
677 }
678 $this->imageMagickExec($fileMask, $fileMask, $command);
679 // Make the color-file
680 $colorImg = imagecreatetruecolor($w, $h);
681 $Ccolor = ImageColorAllocate($colorImg, $cols[0], $cols[1], $cols[2]);
682 ImageFilledRectangle($colorImg, 0, 0, $w, $h, $Ccolor);
683 $this->ImageWrite($colorImg, $fileColor);
684 ImageDestroy($colorImg);
685 // The mask is applied
686 // The main pictures is saved temporarily
687 $this->ImageWrite($im, $fileMenu);
688 $this->combineExec($fileMenu, $fileColor, $fileMask, $fileMenu);
689 // The main image is loaded again...
690 $backIm = $this->imageCreateFromFile($fileMenu);
691 // ... and if nothing went wrong we load it onto the old one.
692 if ($backIm) {
693 if (!$this->saveAlphaLayer) {
694 ImageColorTransparent($backIm, -1);
695 }
696 $im = $backIm;
697 }
698 // Deleting temporary files;
699 if (!$this->dontUnlinkTempFiles) {
700 unlink($fileMenu);
701 unlink($fileColor);
702 unlink($fileMask);
703 }
704 }
705 }
706 }
707
708 /**
709 * Calculates text position for printing the text onto the image based on configuration like alignment and workarea.
710 *
711 * @param array $conf TypoScript array for the TEXT GIFBUILDER object
712 * @param array $workArea Workarea definition
713 * @param array $BB Bounding box information, was set in tslib_gifBuilder::start()
714 * @return array [0]=x, [1]=y, [2]=w, [3]=h
715 * @access private
716 * @see makeText()
717 * @todo Define visibility
718 */
719 public function txtPosition($conf, $workArea, $BB) {
720 $bbox = $BB[2];
721 $angle = intval($conf['angle']) / 180 * pi();
722 $conf['angle'] = 0;
723 $straightBB = $this->calcBBox($conf);
724 // offset, align, valign, workarea
725 // [0]=x, [1]=y, [2]=w, [3]=h
726 $result = array();
727 $result[2] = $BB[0];
728 $result[3] = $BB[1];
729 $w = $workArea[2];
730 $h = $workArea[3];
731 switch ($conf['align']) {
732 case 'right':
733
734 case 'center':
735 $factor = abs(cos($angle));
736 $sign = cos($angle) < 0 ? -1 : 1;
737 $len1 = $sign * $factor * $straightBB[0];
738 $len2 = $sign * $BB[0];
739 $result[0] = $w - ceil(($len2 * $factor + (1 - $factor) * $len1));
740 $factor = abs(sin($angle));
741 $sign = sin($angle) < 0 ? -1 : 1;
742 $len1 = $sign * $factor * $straightBB[0];
743 $len2 = $sign * $BB[1];
744 $result[1] = ceil($len2 * $factor + (1 - $factor) * $len1);
745 break;
746 }
747 switch ($conf['align']) {
748 case 'right':
749 break;
750 case 'center':
751 $result[0] = round($result[0] / 2);
752 $result[1] = round($result[1] / 2);
753 break;
754 default:
755 $result[0] = 0;
756 $result[1] = 0;
757 break;
758 }
759 $result = $this->applyOffset($result, \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $conf['offset']));
760 $result = $this->applyOffset($result, $workArea);
761 return $result;
762 }
763
764 /**
765 * Calculates bounding box information for the TEXT GIFBUILDER object.
766 *
767 * @param array $conf TypoScript array for the TEXT GIFBUILDER object
768 * @return array Array with three keys [0]/[1] being x/y and [2] being the bounding box array
769 * @access private
770 * @see txtPosition(), tslib_gifBuilder::start()
771 * @todo Define visibility
772 */
773 public function calcBBox($conf) {
774 $sF = $this->getTextScalFactor($conf);
775 list($spacing, $wordSpacing) = $this->calcWordSpacing($conf, $sF);
776 $theText = $this->recodeString($conf['text']);
777 $charInf = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $theText, $conf['splitRendering.'], $sF);
778 $theBBoxInfo = $charInf;
779 if ($conf['angle']) {
780 $xArr = array($charInf[0], $charInf[2], $charInf[4], $charInf[6]);
781 $yArr = array($charInf[1], $charInf[3], $charInf[5], $charInf[7]);
782 $x = max($xArr) - min($xArr);
783 $y = max($yArr) - min($yArr);
784 } else {
785 $x = $charInf[2] - $charInf[0];
786 $y = $charInf[1] - $charInf[7];
787 }
788 // Set original lineHeight (used by line breaks):
789 $theBBoxInfo['lineHeight'] = $y;
790 // If any kind of spacing applys, we use this function:
791 if ($spacing || $wordSpacing) {
792 $x = 0;
793 if (!$spacing && $wordSpacing) {
794 $bits = explode(' ', $theText);
795 foreach ($bits as $word) {
796 $word .= ' ';
797 $wordInf = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $word, $conf['splitRendering.'], $sF);
798 $wordW = $wordInf[2] - $wordInf[0];
799 $x += $wordW + $wordSpacing;
800 }
801 } else {
802 $utf8Chars = $this->singleChars($theText);
803 // For each UTF-8 char, do:
804 foreach ($utf8Chars as $char) {
805 $charInf = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $char, $conf['splitRendering.'], $sF);
806 $charW = $charInf[2] - $charInf[0];
807 $x += $charW + ($char == ' ' ? $wordSpacing : $spacing);
808 }
809 }
810 } elseif (isset($conf['breakWidth']) && $conf['breakWidth'] && $this->getRenderedTextWidth($conf['text'], $conf) > $conf['breakWidth']) {
811 $maxWidth = 0;
812 $currentWidth = 0;
813 $breakWidth = $conf['breakWidth'];
814 $breakSpace = $this->getBreakSpace($conf, $theBBoxInfo);
815 $wordPairs = $this->getWordPairsForLineBreak($conf['text']);
816 // Iterate through all word pairs:
817 foreach ($wordPairs as $index => $wordPair) {
818 $wordWidth = $this->getRenderedTextWidth($wordPair, $conf);
819 if ($index == 0 || $currentWidth + $wordWidth <= $breakWidth) {
820 $currentWidth += $wordWidth;
821 } else {
822 $maxWidth = max($maxWidth, $currentWidth);
823 $y += $breakSpace;
824 // Restart:
825 $currentWidth = $wordWidth;
826 }
827 }
828 $x = max($maxWidth, $currentWidth) * $sF;
829 }
830 if ($sF > 1) {
831 $x = ceil($x / $sF);
832 $y = ceil($y / $sF);
833 if (is_array($theBBoxInfo)) {
834 foreach ($theBBoxInfo as &$value) {
835 $value = ceil($value / $sF);
836 }
837 unset($value);
838 }
839 }
840 return array($x, $y, $theBBoxInfo);
841 }
842
843 /**
844 * Adds an <area> tag to the internal variable $this->map which is used to accumulate the content for an ImageMap
845 *
846 * @param array $cords Coordinates for a polygon image map as created by ->calcTextCordsForMap()
847 * @param array $conf Configuration for "imgMap." property of a TEXT GIFBUILDER object.
848 * @return void
849 * @access private
850 * @see makeText(), calcTextCordsForMap()
851 * @todo Define visibility
852 */
853 public function addToMap($cords, $conf) {
854 $this->map .= '<area' . ' shape="poly"' . ' coords="' . implode(',', $cords) . '"' . ' href="' . htmlspecialchars($conf['url']) . '"' . ($conf['target'] ? ' target="' . htmlspecialchars($conf['target']) . '"' : '') . $JS . (strlen($conf['titleText']) ? ' title="' . htmlspecialchars($conf['titleText']) . '"' : '') . ' alt="' . htmlspecialchars($conf['altText']) . '" />';
855 }
856
857 /**
858 * Calculating the coordinates for a TEXT string on an image map. Used in an <area> tag
859 *
860 * @param array $cords Coordinates (from BBOX array)
861 * @param array $offset Offset array
862 * @param array $conf Configuration for "imgMap." property of a TEXT GIFBUILDER object.
863 * @return array
864 * @access private
865 * @see makeText(), calcTextCordsForMap()
866 * @todo Define visibility
867 */
868 public function calcTextCordsForMap($cords, $offset, $conf) {
869 $pars = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $conf['explode'] . ',');
870 $newCords[0] = $cords[0] + $offset[0] - $pars[0];
871 $newCords[1] = $cords[1] + $offset[1] + $pars[1];
872 $newCords[2] = $cords[2] + $offset[0] + $pars[0];
873 $newCords[3] = $cords[3] + $offset[1] + $pars[1];
874 $newCords[4] = $cords[4] + $offset[0] + $pars[0];
875 $newCords[5] = $cords[5] + $offset[1] - $pars[1];
876 $newCords[6] = $cords[6] + $offset[0] - $pars[0];
877 $newCords[7] = $cords[7] + $offset[1] - $pars[1];
878 return $newCords;
879 }
880
881 /**
882 * Printing text onto an image like the PHP function imageTTFText does but in addition it offers options for spacing of letters and words.
883 * Spacing is done by printing one char at a time and this means that the spacing is rather uneven and probably not very nice.
884 * See
885 *
886 * @param pointer $im (See argument for PHP function imageTTFtext())
887 * @param integer $fontSize (See argument for PHP function imageTTFtext())
888 * @param integer $angle (See argument for PHP function imageTTFtext())
889 * @param integer $x (See argument for PHP function imageTTFtext())
890 * @param integer $y (See argument for PHP function imageTTFtext())
891 * @param integer $Fcolor (See argument for PHP function imageTTFtext())
892 * @param string $fontFile (See argument for PHP function imageTTFtext())
893 * @param string $text (See argument for PHP function imageTTFtext()). UTF-8 string, possibly with entities in.
894 * @param integer $spacing The spacing of letters in pixels
895 * @param integer $wordSpacing The spacing of words in pixels
896 * @param array $splitRenderingConf Array
897 * @param integer $sF Scale factor
898 * @return void
899 * @access private
900 * @todo Define visibility
901 */
902 public function SpacedImageTTFText(&$im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $text, $spacing, $wordSpacing, $splitRenderingConf, $sF = 1) {
903 $spacing *= $sF;
904 $wordSpacing *= $sF;
905 if (!$spacing && $wordSpacing) {
906 $bits = explode(' ', $text);
907 foreach ($bits as $word) {
908 $word .= ' ';
909 $wordInf = $this->ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $word, $splitRenderingConf, $sF);
910 $wordW = $wordInf[2] - $wordInf[0];
911 $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $word, $splitRenderingConf, $sF);
912 $x += $wordW + $wordSpacing;
913 }
914 } else {
915 $utf8Chars = $this->singleChars($text);
916 // For each UTF-8 char, do:
917 foreach ($utf8Chars as $char) {
918 $charInf = $this->ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $char, $splitRenderingConf, $sF);
919 $charW = $charInf[2] - $charInf[0];
920 $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $char, $splitRenderingConf, $sF);
921 $x += $charW + ($char == ' ' ? $wordSpacing : $spacing);
922 }
923 }
924 }
925
926 /**
927 * Function that finds the right fontsize that will render the textstring within a certain width
928 *
929 * @param array $conf The TypoScript properties of the TEXT GIFBUILDER object
930 * @return integer The new fontSize
931 * @access private
932 * @see tslib_gifBuilder::start()
933 * @todo Define visibility
934 */
935 public function fontResize($conf) {
936 // You have to use +calc options like [10.h] in 'offset' to get the right position of your text-image, if you use +calc in XY height!!!!
937 $maxWidth = intval($conf['maxWidth']);
938 list($spacing, $wordSpacing) = $this->calcWordSpacing($conf);
939 if ($maxWidth) {
940 // If any kind of spacing applys, we use this function:
941 if ($spacing || $wordSpacing) {
942 return $conf['fontSize'];
943 } else {
944 do {
945 // Determine bounding box.
946 $bounds = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $this->recodeString($conf['text']), $conf['splitRendering.']);
947 if ($conf['angle'] < 0) {
948 $pixelWidth = abs($bounds[4] - $bounds[0]);
949 } elseif ($conf['angle'] > 0) {
950 $pixelWidth = abs($bounds[2] - $bounds[6]);
951 } else {
952 $pixelWidth = abs($bounds[4] - $bounds[6]);
953 }
954 // Size is fine, exit:
955 if ($pixelWidth <= $maxWidth) {
956 break;
957 } else {
958 $conf['fontSize']--;
959 }
960 } while ($conf['fontSize'] > 1);
961 }
962 }
963 return $conf['fontSize'];
964 }
965
966 /**
967 * Wrapper for ImageTTFBBox
968 *
969 * @param integer $fontSize (See argument for PHP function ImageTTFBBox())
970 * @param integer $angle (See argument for PHP function ImageTTFBBox())
971 * @param string $fontFile (See argument for PHP function ImageTTFBBox())
972 * @param string $string (See argument for PHP function ImageTTFBBox())
973 * @param array $splitRendering Split-rendering configuration
974 * @param integer $sF Scale factor
975 * @return array Information array.
976 * @todo Define visibility
977 */
978 public function ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $string, $splitRendering, $sF = 1) {
979 // Initialize:
980 $offsetInfo = array();
981 $stringParts = $this->splitString($string, $splitRendering, $fontSize, $fontFile);
982 // Traverse string parts:
983 foreach ($stringParts as $strCfg) {
984 $fontFile = self::prependAbsolutePath($strCfg['fontFile']);
985 if (is_readable($fontFile)) {
986 /**
987 * Calculate Bounding Box for part.
988 * Due to a PHP bug, we must retry if $calc[2] is negative.
989 *
990 * @see https://bugs.php.net/bug.php?id=51315
991 * @see https://bugs.php.net/bug.php?id=22513
992 */
993 $try = 0;
994 do {
995 $calc = ImageTTFBBox(\TYPO3\CMS\Core\Utility\GeneralUtility::freetypeDpiComp($sF * $strCfg['fontSize']), $angle, $fontFile, $strCfg['str']);
996 } while ($calc[2] < 0 && $try++ < 10);
997 // Calculate offsets:
998 if (!count($offsetInfo)) {
999 // First run, just copy over.
1000 $offsetInfo = $calc;
1001 } else {
1002 $offsetInfo[2] += $calc[2] - $calc[0] + intval($splitRendering['compX']) + intval($strCfg['xSpaceBefore']) + intval($strCfg['xSpaceAfter']);
1003 $offsetInfo[3] += $calc[3] - $calc[1] - intval($splitRendering['compY']) - intval($strCfg['ySpaceBefore']) - intval($strCfg['ySpaceAfter']);
1004 $offsetInfo[4] += $calc[4] - $calc[6] + intval($splitRendering['compX']) + intval($strCfg['xSpaceBefore']) + intval($strCfg['xSpaceAfter']);
1005 $offsetInfo[5] += $calc[5] - $calc[7] - intval($splitRendering['compY']) - intval($strCfg['ySpaceBefore']) - intval($strCfg['ySpaceAfter']);
1006 }
1007 } else {
1008 debug('cannot read file: ' . $fontFile, 'TYPO3\\CMS\\Core\\Imaging\\GraphicalFunctions::ImageTTFBBoxWrapper()');
1009 }
1010 }
1011 return $offsetInfo;
1012 }
1013
1014 /**
1015 * Wrapper for ImageTTFText
1016 *
1017 * @param pointer $im (See argument for PHP function imageTTFtext())
1018 * @param integer $fontSize (See argument for PHP function imageTTFtext())
1019 * @param integer $angle (See argument for PHP function imageTTFtext())
1020 * @param integer $x (See argument for PHP function imageTTFtext())
1021 * @param integer $y (See argument for PHP function imageTTFtext())
1022 * @param integer $color (See argument for PHP function imageTTFtext())
1023 * @param string $fontFile (See argument for PHP function imageTTFtext())
1024 * @param string $string (See argument for PHP function imageTTFtext()). UTF-8 string, possibly with entities in.
1025 * @param array $splitRendering Split-rendering configuration
1026 * @param integer $sF Scale factor
1027 * @return void
1028 * @todo Define visibility
1029 */
1030 public function ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $string, $splitRendering, $sF = 1) {
1031 // Initialize:
1032 $stringParts = $this->splitString($string, $splitRendering, $fontSize, $fontFile);
1033 $x = ceil($sF * $x);
1034 $y = ceil($sF * $y);
1035 // Traverse string parts:
1036 foreach ($stringParts as $i => $strCfg) {
1037 // Initialize:
1038 $colorIndex = $color;
1039 // Set custom color if any (only when niceText is off):
1040 if ($strCfg['color'] && $sF == 1) {
1041 $cols = $this->convertColor($strCfg['color']);
1042 $colorIndex = ImageColorAllocate($im, $cols[0], $cols[1], $cols[2]);
1043 $colorIndex = $color >= 0 ? $colorIndex : -$colorIndex;
1044 }
1045 // Setting xSpaceBefore
1046 if ($i) {
1047 $x += intval($strCfg['xSpaceBefore']);
1048 $y -= intval($strCfg['ySpaceBefore']);
1049 }
1050 $fontFile = self::prependAbsolutePath($strCfg['fontFile']);
1051 if (is_readable($fontFile)) {
1052 // Render part:
1053 ImageTTFText($im, \TYPO3\CMS\Core\Utility\GeneralUtility::freetypeDpiComp($sF * $strCfg['fontSize']), $angle, $x, $y, $colorIndex, $fontFile, $strCfg['str']);
1054 // Calculate offset to apply:
1055 $wordInf = ImageTTFBBox(\TYPO3\CMS\Core\Utility\GeneralUtility::freetypeDpiComp($sF * $strCfg['fontSize']), $angle, self::prependAbsolutePath($strCfg['fontFile']), $strCfg['str']);
1056 $x += $wordInf[2] - $wordInf[0] + intval($splitRendering['compX']) + intval($strCfg['xSpaceAfter']);
1057 $y += $wordInf[5] - $wordInf[7] - intval($splitRendering['compY']) - intval($strCfg['ySpaceAfter']);
1058 } else {
1059 debug('cannot read file: ' . $fontFile, 'TYPO3\\CMS\\Core\\Imaging\\GraphicalFunctions::ImageTTFTextWrapper()');
1060 }
1061 }
1062 }
1063
1064 /**
1065 * Splitting a string for ImageTTFBBox up into an array where each part has its own configuration options.
1066 *
1067 * @param string $string UTF-8 string
1068 * @param array $splitRendering Split-rendering configuration from GIFBUILDER TEXT object.
1069 * @param integer $fontSize Current fontsize
1070 * @param string $fontFile Current font file
1071 * @return array Array with input string splitted according to configuration
1072 * @todo Define visibility
1073 */
1074 public function splitString($string, $splitRendering, $fontSize, $fontFile) {
1075 // Initialize by setting the whole string and default configuration as the first entry.
1076 $result = array();
1077 $result[] = array(
1078 'str' => $string,
1079 'fontSize' => $fontSize,
1080 'fontFile' => $fontFile
1081 );
1082 // Traverse the split-rendering configuration:
1083 // Splitting will create more entries in $result with individual configurations.
1084 if (is_array($splitRendering)) {
1085 $sKeyArray = \TYPO3\CMS\Core\TypoScript\TemplateService::sortedKeyList($splitRendering);
1086 // Traverse configured options:
1087 foreach ($sKeyArray as $key) {
1088 $cfg = $splitRendering[$key . '.'];
1089 // Process each type of split rendering keyword:
1090 switch ((string) $splitRendering[$key]) {
1091 case 'highlightWord':
1092 if (strlen($cfg['value'])) {
1093 $newResult = array();
1094 // Traverse the current parts of the result array:
1095 foreach ($result as $part) {
1096 // Explode the string value by the word value to highlight:
1097 $explodedParts = explode($cfg['value'], $part['str']);
1098 foreach ($explodedParts as $c => $expValue) {
1099 if (strlen($expValue)) {
1100 $newResult[] = array_merge($part, array('str' => $expValue));
1101 }
1102 if ($c + 1 < count($explodedParts)) {
1103 $newResult[] = array(
1104 'str' => $cfg['value'],
1105 'fontSize' => $cfg['fontSize'] ? $cfg['fontSize'] : $part['fontSize'],
1106 'fontFile' => $cfg['fontFile'] ? $cfg['fontFile'] : $part['fontFile'],
1107 'color' => $cfg['color'],
1108 'xSpaceBefore' => $cfg['xSpaceBefore'],
1109 'xSpaceAfter' => $cfg['xSpaceAfter'],
1110 'ySpaceBefore' => $cfg['ySpaceBefore'],
1111 'ySpaceAfter' => $cfg['ySpaceAfter']
1112 );
1113 }
1114 }
1115 }
1116 // Set the new result as result array:
1117 if (count($newResult)) {
1118 $result = $newResult;
1119 }
1120 }
1121 break;
1122 case 'charRange':
1123 if (strlen($cfg['value'])) {
1124 // Initialize range:
1125 $ranges = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $cfg['value'], 1);
1126 foreach ($ranges as $i => $rangeDef) {
1127 $ranges[$i] = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode('-', $ranges[$i]);
1128 if (!isset($ranges[$i][1])) {
1129 $ranges[$i][1] = $ranges[$i][0];
1130 }
1131 }
1132 $newResult = array();
1133 // Traverse the current parts of the result array:
1134 foreach ($result as $part) {
1135 // Initialize:
1136 $currentState = -1;
1137 $bankAccum = '';
1138 // Explode the string value by the word value to highlight:
1139 $utf8Chars = $this->singleChars($part['str']);
1140 foreach ($utf8Chars as $utfChar) {
1141 // Find number and evaluate position:
1142 $uNumber = $this->csConvObj->utf8CharToUnumber($utfChar);
1143 $inRange = 0;
1144 foreach ($ranges as $rangeDef) {
1145 if ($uNumber >= $rangeDef[0] && (!$rangeDef[1] || $uNumber <= $rangeDef[1])) {
1146 $inRange = 1;
1147 break;
1148 }
1149 }
1150 if ($currentState == -1) {
1151 $currentState = $inRange;
1152 }
1153 // Initialize first char
1154 // Switch bank:
1155 if ($inRange != $currentState && !\TYPO3\CMS\Core\Utility\GeneralUtility::inList('32,10,13,9', $uNumber)) {
1156 // Set result:
1157 if (strlen($bankAccum)) {
1158 $newResult[] = array(
1159 'str' => $bankAccum,
1160 'fontSize' => $currentState && $cfg['fontSize'] ? $cfg['fontSize'] : $part['fontSize'],
1161 'fontFile' => $currentState && $cfg['fontFile'] ? $cfg['fontFile'] : $part['fontFile'],
1162 'color' => $currentState ? $cfg['color'] : '',
1163 'xSpaceBefore' => $currentState ? $cfg['xSpaceBefore'] : '',
1164 'xSpaceAfter' => $currentState ? $cfg['xSpaceAfter'] : '',
1165 'ySpaceBefore' => $currentState ? $cfg['ySpaceBefore'] : '',
1166 'ySpaceAfter' => $currentState ? $cfg['ySpaceAfter'] : ''
1167 );
1168 }
1169 // Initialize new settings:
1170 $currentState = $inRange;
1171 $bankAccum = '';
1172 }
1173 // Add char to bank:
1174 $bankAccum .= $utfChar;
1175 }
1176 // Set result for FINAL part:
1177 if (strlen($bankAccum)) {
1178 $newResult[] = array(
1179 'str' => $bankAccum,
1180 'fontSize' => $currentState && $cfg['fontSize'] ? $cfg['fontSize'] : $part['fontSize'],
1181 'fontFile' => $currentState && $cfg['fontFile'] ? $cfg['fontFile'] : $part['fontFile'],
1182 'color' => $currentState ? $cfg['color'] : '',
1183 'xSpaceBefore' => $currentState ? $cfg['xSpaceBefore'] : '',
1184 'xSpaceAfter' => $currentState ? $cfg['xSpaceAfter'] : '',
1185 'ySpaceBefore' => $currentState ? $cfg['ySpaceBefore'] : '',
1186 'ySpaceAfter' => $currentState ? $cfg['ySpaceAfter'] : ''
1187 );
1188 }
1189 }
1190 // Set the new result as result array:
1191 if (count($newResult)) {
1192 $result = $newResult;
1193 }
1194 }
1195 break;
1196 }
1197 }
1198 }
1199 return $result;
1200 }
1201
1202 /**
1203 * Calculates the spacing and wordSpacing values
1204 *
1205 * @param array $conf TypoScript array for the TEXT GIFBUILDER object
1206 * @param integer $scaleFactor TypoScript value from eg $conf['niceText.']['scaleFactor']
1207 * @return array Array with two keys [0]/[1] being array($spacing,$wordSpacing)
1208 * @access private
1209 * @see calcBBox()
1210 * @todo Define visibility
1211 */
1212 public function calcWordSpacing($conf, $scaleFactor = 1) {
1213 $spacing = intval($conf['spacing']);
1214 $wordSpacing = intval($conf['wordSpacing']);
1215 $wordSpacing = $wordSpacing ? $wordSpacing : $spacing * 2;
1216 $spacing *= $scaleFactor;
1217 $wordSpacing *= $scaleFactor;
1218 return array($spacing, $wordSpacing);
1219 }
1220
1221 /**
1222 * Calculates and returns the niceText.scaleFactor
1223 *
1224 * @param array $conf TypoScript array for the TEXT GIFBUILDER object
1225 * @return integer TypoScript value from eg $conf['niceText.']['scaleFactor']
1226 * @access private
1227 * @todo Define visibility
1228 */
1229 public function getTextScalFactor($conf) {
1230 if (!$conf['niceText']) {
1231 $sF = 1;
1232 } else {
1233 // NICETEXT::
1234 $sF = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($conf['niceText.']['scaleFactor'], 2, 5);
1235 }
1236 return $sF;
1237 }
1238
1239 /**
1240 * Renders a regular text and takes care of a possible line break automatically.
1241 *
1242 * @param pointer $im (See argument for PHP function imageTTFtext())
1243 * @param integer $fontSize (See argument for PHP function imageTTFtext())
1244 * @param integer $angle (See argument for PHP function imageTTFtext())
1245 * @param integer $x (See argument for PHP function imageTTFtext())
1246 * @param integer $y (See argument for PHP function imageTTFtext())
1247 * @param integer $color (See argument for PHP function imageTTFtext())
1248 * @param string $fontFile (See argument for PHP function imageTTFtext())
1249 * @param string $string (See argument for PHP function imageTTFtext()). UTF-8 string, possibly with entities in.
1250 * @param array $splitRendering Split-rendering configuration
1251 * @param array $conf The configuration
1252 * @param integer $sF Scale factor
1253 * @return void
1254 */
1255 protected function renderTTFText(&$im, $fontSize, $angle, $x, $y, $color, $fontFile, $string, $splitRendering, $conf, $sF = 1) {
1256 if (isset($conf['breakWidth']) && $conf['breakWidth'] && $this->getRenderedTextWidth($string, $conf) > $conf['breakWidth']) {
1257 $phrase = '';
1258 $currentWidth = 0;
1259 $breakWidth = $conf['breakWidth'];
1260 $breakSpace = $this->getBreakSpace($conf);
1261 $wordPairs = $this->getWordPairsForLineBreak($string);
1262 // Iterate through all word pairs:
1263 foreach ($wordPairs as $index => $wordPair) {
1264 $wordWidth = $this->getRenderedTextWidth($wordPair, $conf);
1265 if ($index == 0 || $currentWidth + $wordWidth <= $breakWidth) {
1266 $currentWidth += $wordWidth;
1267 $phrase .= $wordPair;
1268 } else {
1269 // Render the current phrase that is below breakWidth:
1270 $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $phrase, $splitRendering, $sF);
1271 // Calculate the news height offset:
1272 $y += $breakSpace;
1273 // Restart the phrase:
1274 $currentWidth = $wordWidth;
1275 $phrase = $wordPair;
1276 }
1277 }
1278 // Render the remaining phrase:
1279 if ($currentWidth) {
1280 $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $phrase, $splitRendering, $sF);
1281 }
1282 } else {
1283 $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $string, $splitRendering, $sF);
1284 }
1285 }
1286
1287 /**
1288 * Gets the word pairs used for automatic line breaks.
1289 *
1290 * @param string $string
1291 * @return array
1292 */
1293 protected function getWordPairsForLineBreak($string) {
1294 $wordPairs = array();
1295 $wordsArray = preg_split('#([- .,!:]+)#', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
1296 $wordsCount = count($wordsArray);
1297 for ($index = 0; $index < $wordsCount; $index += 2) {
1298 $wordPairs[] = $wordsArray[$index] . $wordsArray[($index + 1)];
1299 }
1300 return $wordPairs;
1301 }
1302
1303 /**
1304 * Gets the rendered text width.
1305 *
1306 * @param string $text
1307 * @param array $conf
1308 * @param integer
1309 */
1310 protected function getRenderedTextWidth($text, $conf) {
1311 $bounds = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $this->recodeString($text), $conf['splitRendering.']);
1312 if ($conf['angle'] < 0) {
1313 $pixelWidth = abs($bounds[4] - $bounds[0]);
1314 } elseif ($conf['angle'] > 0) {
1315 $pixelWidth = abs($bounds[2] - $bounds[6]);
1316 } else {
1317 $pixelWidth = abs($bounds[4] - $bounds[6]);
1318 }
1319 return $pixelWidth;
1320 }
1321
1322 /**
1323 * Gets the break space for each new line.
1324 *
1325 * @param array $conf TypoScript configuration for the currently rendered object
1326 * @param array $boundingBox The bounding box the the currently rendered object
1327 * @return integer The break space
1328 */
1329 protected function getBreakSpace($conf, array $boundingBox = NULL) {
1330 if (!isset($boundingBox)) {
1331 $boundingBox = $this->calcBBox($conf);
1332 $boundingBox = $boundingBox[2];
1333 }
1334 if (isset($conf['breakSpace']) && $conf['breakSpace']) {
1335 $breakSpace = $boundingBox['lineHeight'] * $conf['breakSpace'];
1336 } else {
1337 $breakSpace = $boundingBox['lineHeight'];
1338 }
1339 return $breakSpace;
1340 }
1341
1342 /*********************************************
1343 *
1344 * Other GIFBUILDER objects related to TEXT
1345 *
1346 *********************************************/
1347 /**
1348 * Implements the "OUTLINE" GIFBUILDER object / property for the TEXT object
1349 *
1350 * @param pointer $im GDlib image pointer
1351 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
1352 * @param array $workArea The current working area coordinates.
1353 * @param array $txtConf TypoScript array with configuration for the associated TEXT GIFBUILDER object.
1354 * @return void
1355 * @see tslib_gifBuilder::make(), makeText()
1356 * @todo Define visibility
1357 */
1358 public function makeOutline(&$im, $conf, $workArea, $txtConf) {
1359 $thickness = intval($conf['thickness']);
1360 if ($thickness) {
1361 $txtConf['fontColor'] = $conf['color'];
1362 $outLineDist = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($thickness, 1, 2);
1363 for ($b = 1; $b <= $outLineDist; $b++) {
1364 if ($b == 1) {
1365 $it = 8;
1366 } else {
1367 $it = 16;
1368 }
1369 $outL = $this->circleOffset($b, $it);
1370 for ($a = 0; $a < $it; $a++) {
1371 $this->makeText($im, $txtConf, $this->applyOffset($workArea, $outL[$a]));
1372 }
1373 }
1374 }
1375 }
1376
1377 /**
1378 * Creates some offset values in an array used to simulate a circularly applied outline around TEXT
1379 *
1380 * access private
1381 *
1382 * @param integer $distance Distance
1383 * @param integer $iterations Iterations.
1384 * @return array
1385 * @see makeOutline()
1386 * @todo Define visibility
1387 */
1388 public function circleOffset($distance, $iterations) {
1389 $res = array();
1390 if ($distance && $iterations) {
1391 for ($a = 0; $a < $iterations; $a++) {
1392 $yOff = round(sin((2 * pi() / $iterations * ($a + 1))) * 100 * $distance);
1393 if ($yOff) {
1394 $yOff = intval(ceil(abs(($yOff / 100))) * ($yOff / abs($yOff)));
1395 }
1396 $xOff = round(cos((2 * pi() / $iterations * ($a + 1))) * 100 * $distance);
1397 if ($xOff) {
1398 $xOff = intval(ceil(abs(($xOff / 100))) * ($xOff / abs($xOff)));
1399 }
1400 $res[$a] = array($xOff, $yOff);
1401 }
1402 }
1403 return $res;
1404 }
1405
1406 /**
1407 * Implements the "EMBOSS" GIFBUILDER object / property for the TEXT object
1408 *
1409 * @param pointer $im GDlib image pointer
1410 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
1411 * @param array $workArea The current working area coordinates.
1412 * @param array $txtConf TypoScript array with configuration for the associated TEXT GIFBUILDER object.
1413 * @return void
1414 * @see tslib_gifBuilder::make(), makeShadow()
1415 * @todo Define visibility
1416 */
1417 public function makeEmboss(&$im, $conf, $workArea, $txtConf) {
1418 $conf['color'] = $conf['highColor'];
1419 $this->makeShadow($im, $conf, $workArea, $txtConf);
1420 $newOffset = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $conf['offset']);
1421 $newOffset[0] *= -1;
1422 $newOffset[1] *= -1;
1423 $conf['offset'] = implode(',', $newOffset);
1424 $conf['color'] = $conf['lowColor'];
1425 $this->makeShadow($im, $conf, $workArea, $txtConf);
1426 }
1427
1428 /**
1429 * Implements the "SHADOW" GIFBUILDER object / property for the TEXT object
1430 * The operation involves ImageMagick for combining.
1431 *
1432 * @param pointer $im GDlib image pointer
1433 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
1434 * @param array $workArea The current working area coordinates.
1435 * @param array $txtConf TypoScript array with configuration for the associated TEXT GIFBUILDER object.
1436 * @retur void
1437 * @see tslib_gifBuilder::make(), makeText(), makeEmboss()
1438 * @todo Define visibility
1439 */
1440 public function makeShadow(&$im, $conf, $workArea, $txtConf) {
1441 $workArea = $this->applyOffset($workArea, \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $conf['offset']));
1442 $blurRate = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(intval($conf['blur']), 0, 99);
1443 // No effects if ImageMagick ver. 5+
1444 if (!$blurRate || $this->NO_IM_EFFECTS) {
1445 $txtConf['fontColor'] = $conf['color'];
1446 $this->makeText($im, $txtConf, $workArea);
1447 } else {
1448 $w = imagesx($im);
1449 $h = imagesy($im);
1450 // Area around the blur used for cropping something
1451 $blurBorder = 3;
1452 $tmpStr = $this->randomName();
1453 $fileMenu = $tmpStr . '_menu.' . $this->gifExtension;
1454 $fileColor = $tmpStr . '_color.' . $this->gifExtension;
1455 $fileMask = $tmpStr . '_mask.' . $this->gifExtension;
1456 // BlurColor Image laves
1457 $blurColImg = imagecreatetruecolor($w, $h);
1458 $bcols = $this->convertColor($conf['color']);
1459 $Bcolor = ImageColorAllocate($blurColImg, $bcols[0], $bcols[1], $bcols[2]);
1460 ImageFilledRectangle($blurColImg, 0, 0, $w, $h, $Bcolor);
1461 $this->ImageWrite($blurColImg, $fileColor);
1462 ImageDestroy($blurColImg);
1463 // The mask is made: BlurTextImage
1464 $blurTextImg = imagecreatetruecolor($w + $blurBorder * 2, $h + $blurBorder * 2);
1465 // Black background
1466 $Bcolor = ImageColorAllocate($blurTextImg, 0, 0, 0);
1467 ImageFilledRectangle($blurTextImg, 0, 0, $w + $blurBorder * 2, $h + $blurBorder * 2, $Bcolor);
1468 $txtConf['fontColor'] = 'white';
1469 $blurBordArr = array($blurBorder, $blurBorder);
1470 $this->makeText($blurTextImg, $txtConf, $this->applyOffset($workArea, $blurBordArr));
1471 // Dump to temporary file
1472 $this->ImageWrite($blurTextImg, $fileMask);
1473 // Destroy
1474 ImageDestroy($blurTextImg);
1475 $command = '';
1476 $command .= $this->maskNegate;
1477 if ($this->V5_EFFECTS) {
1478 $command .= $this->v5_blur($blurRate + 1);
1479 } else {
1480 // Blurring of the mask
1481 // How many blur-commands that is executed. Min = 1;
1482 $times = ceil($blurRate / 10);
1483 // Here I boost the blur-rate so that it is 100 already at 25. The rest is done by up to 99 iterations of the blur-command.
1484 $newBlurRate = $blurRate * 4;
1485 $newBlurRate = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($newBlurRate, 1, 99);
1486 // Building blur-command
1487 for ($a = 0; $a < $times; $a++) {
1488 $command .= ' -blur ' . $blurRate;
1489 }
1490 }
1491 $this->imageMagickExec($fileMask, $fileMask, $command . ' +matte');
1492 // The mask is loaded again
1493 $blurTextImg_tmp = $this->imageCreateFromFile($fileMask);
1494 // If nothing went wrong we continue with the blurred mask
1495 if ($blurTextImg_tmp) {
1496 // Cropping the border from the mask
1497 $blurTextImg = imagecreatetruecolor($w, $h);
1498 $this->imagecopyresized($blurTextImg, $blurTextImg_tmp, 0, 0, $blurBorder, $blurBorder, $w, $h, $w, $h);
1499 // Destroy the temporary mask
1500 ImageDestroy($blurTextImg_tmp);
1501 // Adjust the mask
1502 $intensity = 40;
1503 if ($conf['intensity']) {
1504 $intensity = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($conf['intensity'], 0, 100);
1505 }
1506 $intensity = ceil(255 - $intensity / 100 * 255);
1507 $this->inputLevels($blurTextImg, 0, $intensity, $this->maskNegate);
1508 $opacity = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(intval($conf['opacity']), 0, 100);
1509 if ($opacity && $opacity < 100) {
1510 $high = ceil(255 * $opacity / 100);
1511 // Reducing levels as the opacity demands
1512 $this->outputLevels($blurTextImg, 0, $high, $this->maskNegate);
1513 }
1514 // Dump the mask again
1515 $this->ImageWrite($blurTextImg, $fileMask);
1516 // Destroy the mask
1517 ImageDestroy($blurTextImg);
1518 // The pictures are combined
1519 // The main pictures is saved temporarily
1520 $this->ImageWrite($im, $fileMenu);
1521 $this->combineExec($fileMenu, $fileColor, $fileMask, $fileMenu);
1522 // The main image is loaded again...
1523 $backIm = $this->imageCreateFromFile($fileMenu);
1524 // ... and if nothing went wrong we load it onto the old one.
1525 if ($backIm) {
1526 if (!$this->saveAlphaLayer) {
1527 ImageColorTransparent($backIm, -1);
1528 }
1529 $im = $backIm;
1530 }
1531 }
1532 // Deleting temporary files;
1533 if (!$this->dontUnlinkTempFiles) {
1534 unlink($fileMenu);
1535 unlink($fileColor);
1536 unlink($fileMask);
1537 }
1538 }
1539 }
1540
1541 /****************************
1542 *
1543 * Other GIFBUILDER objects
1544 *
1545 ****************************/
1546 /**
1547 * Implements the "BOX" GIFBUILDER object
1548 *
1549 * @param pointer $im GDlib image pointer
1550 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
1551 * @param array $workArea The current working area coordinates.
1552 * @return void
1553 * @see tslib_gifBuilder::make()
1554 * @todo Define visibility
1555 */
1556 public function makeBox(&$im, $conf, $workArea) {
1557 $cords = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $conf['dimensions'] . ',,,');
1558 $conf['offset'] = $cords[0] . ',' . $cords[1];
1559 $cords = $this->objPosition($conf, $workArea, array($cords[2], $cords[3]));
1560 $cols = $this->convertColor($conf['color']);
1561 $opacity = 0;
1562 if (isset($conf['opacity'])) {
1563 // conversion:
1564 // PHP 0 = opaque, 127 = transparent
1565 // TYPO3 100 = opaque, 0 = transparent
1566 $opacity = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(intval($conf['opacity']), 1, 100, 1);
1567 $opacity = abs($opacity - 100);
1568 $opacity = round(127 * $opacity / 100);
1569 }
1570 $tmpColor = ImageColorAllocateAlpha($im, $cols[0], $cols[1], $cols[2], $opacity);
1571 imagefilledrectangle($im, $cords[0], $cords[1], $cords[0] + $cords[2] - 1, $cords[1] + $cords[3] - 1, $tmpColor);
1572 }
1573
1574 /**
1575 * Implements the "Ellipse" GIFBUILDER object
1576 * Example Typoscript:
1577 * file = GIFBUILDER
1578 * file {
1579 * XY = 200,200
1580 * format = jpg
1581 * quality = 100
1582 * 10 = ELLIPSE
1583 * 10.dimensions = 100,100,50,50
1584 * 10.color = red
1585 *
1586 * $workArea = X,Y
1587 * $conf['dimensions'] = offset x, offset y, width of ellipse, height of ellipse
1588 *
1589 * @param pointer $im GDlib image pointer
1590 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
1591 * @param array $workArea The current working area coordinates.
1592 * @return void
1593 * @see tslib_gifBuilder::make()
1594 */
1595 public function makeEllipse(&$im, array $conf, array $workArea) {
1596 $ellipseConfiguration = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $conf['dimensions'] . ',,,');
1597 // Ellipse offset inside workArea (x/y)
1598 $conf['offset'] = $ellipseConfiguration[0] . ',' . $ellipseConfiguration[1];
1599 // @see objPosition
1600 $imageCoordinates = $this->objPosition($conf, $workArea, array($ellipseConfiguration[2], $ellipseConfiguration[3]));
1601 $color = $this->convertColor($conf['color']);
1602 $fillingColor = imagecolorallocate($im, $color[0], $color[1], $color[2]);
1603 imagefilledellipse($im, $imageCoordinates[0], $imageCoordinates[1], $imageCoordinates[2], $imageCoordinates[3], $fillingColor);
1604 }
1605
1606 /**
1607 * Implements the "EFFECT" GIFBUILDER object
1608 * The operation involves ImageMagick for applying effects
1609 *
1610 * @param pointer $im GDlib image pointer
1611 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
1612 * @return void
1613 * @see tslib_gifBuilder::make(), applyImageMagickToPHPGif()
1614 * @todo Define visibility
1615 */
1616 public function makeEffect(&$im, $conf) {
1617 $commands = $this->IMparams($conf['value']);
1618 if ($commands) {
1619 $this->applyImageMagickToPHPGif($im, $commands);
1620 }
1621 }
1622
1623 /**
1624 * Creating ImageMagick paramters from TypoScript property
1625 *
1626 * @param string $setup A string with effect keywords=value pairs separated by "|
1627 * @return string ImageMagick prepared parameters.
1628 * @access private
1629 * @see makeEffect()
1630 * @todo Define visibility
1631 */
1632 public function IMparams($setup) {
1633 if (!trim($setup)) {
1634 return '';
1635 }
1636 $effects = explode('|', $setup);
1637 $commands = '';
1638 foreach ($effects as $val) {
1639 $pairs = explode('=', $val, 2);
1640 $value = trim($pairs[1]);
1641 $effect = strtolower(trim($pairs[0]));
1642 switch ($effect) {
1643 case 'gamma':
1644 $commands .= ' -gamma ' . doubleval($value);
1645 break;
1646 case 'blur':
1647 if (!$this->NO_IM_EFFECTS) {
1648 if ($this->V5_EFFECTS) {
1649 $commands .= $this->v5_blur($value);
1650 } else {
1651 $commands .= ' -blur ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 1, 99);
1652 }
1653 }
1654 break;
1655 case 'sharpen':
1656 if (!$this->NO_IM_EFFECTS) {
1657 if ($this->V5_EFFECTS) {
1658 $commands .= $this->v5_sharpen($value);
1659 } else {
1660 $commands .= ' -sharpen ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 1, 99);
1661 }
1662 }
1663 break;
1664 case 'rotate':
1665 $commands .= ' -rotate ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0, 360);
1666 break;
1667 case 'solarize':
1668 $commands .= ' -solarize ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0, 99);
1669 break;
1670 case 'swirl':
1671 $commands .= ' -swirl ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0, 1000);
1672 break;
1673 case 'wave':
1674 $params = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $value);
1675 $commands .= ' -wave ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($params[0], 0, 99) . 'x' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($params[1], 0, 99);
1676 break;
1677 case 'charcoal':
1678 $commands .= ' -charcoal ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0, 100);
1679 break;
1680 case 'gray':
1681 $commands .= ' -colorspace GRAY';
1682 break;
1683 case 'edge':
1684 $commands .= ' -edge ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 0, 99);
1685 break;
1686 case 'emboss':
1687 $commands .= ' -emboss';
1688 break;
1689 case 'flip':
1690 $commands .= ' -flip';
1691 break;
1692 case 'flop':
1693 $commands .= ' -flop';
1694 break;
1695 case 'colors':
1696 $commands .= ' -colors ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, 2, 255);
1697 break;
1698 case 'shear':
1699 $commands .= ' -shear ' . \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($value, -90, 90);
1700 break;
1701 case 'invert':
1702 $commands .= ' -negate';
1703 break;
1704 }
1705 }
1706 return $commands;
1707 }
1708
1709 /**
1710 * Implements the "ADJUST" GIFBUILDER object
1711 *
1712 * @param pointer $im GDlib image pointer
1713 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
1714 * @return void
1715 * @see tslib_gifBuilder::make(), autoLevels(), outputLevels(), inputLevels()
1716 * @todo Define visibility
1717 */
1718 public function adjust(&$im, $conf) {
1719 $setup = $conf['value'];
1720 if (!trim($setup)) {
1721 return '';
1722 }
1723 $effects = explode('|', $setup);
1724 foreach ($effects as $val) {
1725 $pairs = explode('=', $val, 2);
1726 $value = trim($pairs[1]);
1727 $effect = strtolower(trim($pairs[0]));
1728 switch ($effect) {
1729 case 'inputlevels':
1730 // low,high
1731 $params = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $value);
1732 $this->inputLevels($im, $params[0], $params[1]);
1733 break;
1734 case 'outputlevels':
1735 $params = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $value);
1736 $this->outputLevels($im, $params[0], $params[1]);
1737 break;
1738 case 'autolevels':
1739 $this->autoLevels($im);
1740 break;
1741 }
1742 }
1743 }
1744
1745 /**
1746 * Implements the "CROP" GIFBUILDER object
1747 *
1748 * @param pointer $im GDlib image pointer
1749 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
1750 * @return void
1751 * @see tslib_gifBuilder::make()
1752 * @todo Define visibility
1753 */
1754 public function crop(&$im, $conf) {
1755 // Clears workArea to total image
1756 $this->setWorkArea('');
1757 $cords = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $conf['crop'] . ',,,');
1758 $conf['offset'] = $cords[0] . ',' . $cords[1];
1759 $cords = $this->objPosition($conf, $this->workArea, array($cords[2], $cords[3]));
1760 $newIm = imagecreatetruecolor($cords[2], $cords[3]);
1761 $cols = $this->convertColor($conf['backColor'] ? $conf['backColor'] : $this->setup['backColor']);
1762 $Bcolor = ImageColorAllocate($newIm, $cols[0], $cols[1], $cols[2]);
1763 ImageFilledRectangle($newIm, 0, 0, $cords[2], $cords[3], $Bcolor);
1764 $newConf = array();
1765 $workArea = array(0, 0, $cords[2], $cords[3]);
1766 if ($cords[0] < 0) {
1767 $workArea[0] = abs($cords[0]);
1768 } else {
1769 $newConf['offset'] = -$cords[0];
1770 }
1771 if ($cords[1] < 0) {
1772 $workArea[1] = abs($cords[1]);
1773 } else {
1774 $newConf['offset'] .= ',' . -$cords[1];
1775 }
1776 $this->copyGifOntoGif($newIm, $im, $newConf, $workArea);
1777 $im = $newIm;
1778 $this->w = imagesx($im);
1779 $this->h = imagesy($im);
1780 // Clears workArea to total image
1781 $this->setWorkArea('');
1782 }
1783
1784 /**
1785 * Implements the "SCALE" GIFBUILDER object
1786 *
1787 * @param pointer $im GDlib image pointer
1788 * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
1789 * @return void
1790 * @see tslib_gifBuilder::make()
1791 * @todo Define visibility
1792 */
1793 public function scale(&$im, $conf) {
1794 if ($conf['width'] || $conf['height'] || $conf['params']) {
1795 $tmpStr = $this->randomName();
1796 $theFile = $tmpStr . '.' . $this->gifExtension;
1797 $this->ImageWrite($im, $theFile);
1798 $theNewFile = $this->imageMagickConvert($theFile, $this->gifExtension, $conf['width'], $conf['height'], $conf['params']);
1799 $tmpImg = $this->imageCreateFromFile($theNewFile[3]);
1800 if ($tmpImg) {
1801 ImageDestroy($im);
1802 $im = $tmpImg;
1803 $this->w = imagesx($im);
1804 $this->h = imagesy($im);
1805 // Clears workArea to total image
1806 $this->setWorkArea('');
1807 }
1808 if (!$this->dontUnlinkTempFiles) {
1809 unlink($theFile);
1810 if ($theNewFile[3] && $theNewFile[3] != $theFile) {
1811 unlink($theNewFile[3]);
1812 }
1813 }
1814 }
1815 }
1816
1817 /**
1818 * Implements the "WORKAREA" GIFBUILDER object when setting it
1819 * Setting internal working area boundaries (->workArea)
1820 *
1821 * @param string $workArea Working area dimensions, comma separated
1822 * @return void
1823 * @access private
1824 * @see tslib_gifBuilder::make()
1825 * @todo Define visibility
1826 */
1827 public function setWorkArea($workArea) {
1828 $this->workArea = \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $workArea);
1829 $this->workArea = $this->applyOffset($this->workArea, $this->OFFSET);
1830 if (!$this->workArea[2]) {
1831 $this->workArea[2] = $this->w;
1832 }
1833 if (!$this->workArea[3]) {
1834 $this->workArea[3] = $this->h;
1835 }
1836 }
1837
1838 /*************************
1839 *
1840 * Adjustment functions
1841 *
1842 ************************/
1843 /**
1844 * Apply auto-levels to input image pointer
1845 *
1846 * @param integer $im GDlib Image Pointer
1847 * @return void
1848 * @todo Define visibility
1849 */
1850 public function autolevels(&$im) {
1851 $totalCols = ImageColorsTotal($im);
1852 $min = 255;
1853 $max = 0;
1854 for ($c = 0; $c < $totalCols; $c++) {
1855 $cols = ImageColorsForIndex($im, $c);
1856 $grayArr[] = round(($cols['red'] + $cols['green'] + $cols['blue']) / 3);
1857 }
1858 $min = min($grayArr);
1859 $max = max($grayArr);
1860 $delta = $max - $min;
1861 if ($delta) {
1862 for ($c = 0; $c < $totalCols; $c++) {
1863 $cols = ImageColorsForIndex($im, $c);
1864 $cols['red'] = floor(($cols['red'] - $min) / $delta * 255);
1865 $cols['green'] = floor(($cols['green'] - $min) / $delta * 255);
1866 $cols['blue'] = floor(($cols['blue'] - $min) / $delta * 255);
1867 ImageColorSet($im, $c, $cols['red'], $cols['green'], $cols['blue']);
1868 }
1869 }
1870 }
1871
1872 /**
1873 * Apply output levels to input image pointer (decreasing contrast)
1874 *
1875 * @param integer $im GDlib Image Pointer
1876 * @param integer $low The "low" value (close to 0)
1877 * @param integer $high The "high" value (close to 255)
1878 * @param boolean $swap If swap, then low and high are swapped. (Useful for negated masks...)
1879 * @return void
1880 * @todo Define visibility
1881 */
1882 public function outputLevels(&$im, $low, $high, $swap = '') {
1883 if ($low < $high) {
1884 $low = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($low, 0, 255);
1885 $high = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($high, 0, 255);
1886 if ($swap) {
1887 $temp = $low;
1888 $low = 255 - $high;
1889 $high = 255 - $temp;
1890 }
1891 $delta = $high - $low;
1892 $totalCols = ImageColorsTotal($im);
1893 for ($c = 0; $c < $totalCols; $c++) {
1894 $cols = ImageColorsForIndex($im, $c);
1895 $cols['red'] = $low + floor($cols['red'] / 255 * $delta);
1896 $cols['green'] = $low + floor($cols['green'] / 255 * $delta);
1897 $cols['blue'] = $low + floor($cols['blue'] / 255 * $delta);
1898 ImageColorSet($im, $c, $cols['red'], $cols['green'], $cols['blue']);
1899 }
1900 }
1901 }
1902
1903 /**
1904 * Apply input levels to input image pointer (increasing contrast)
1905 *
1906 * @param integer $im GDlib Image Pointer
1907 * @param integer $low The "low" value (close to 0)
1908 * @param integer $high The "high" value (close to 255)
1909 * @param boolean $swap If swap, then low and high are swapped. (Useful for negated masks...)
1910 * @return void
1911 * @todo Define visibility
1912 */
1913 public function inputLevels(&$im, $low, $high, $swap = '') {
1914 if ($low < $high) {
1915 $low = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($low, 0, 255);
1916 $high = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($high, 0, 255);
1917 if ($swap) {
1918 $temp = $low;
1919 $low = 255 - $high;
1920 $high = 255 - $temp;
1921 }
1922 $delta = $high - $low;
1923 $totalCols = ImageColorsTotal($im);
1924 for ($c = 0; $c < $totalCols; $c++) {
1925 $cols = ImageColorsForIndex($im, $c);
1926 $cols['red'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(($cols['red'] - $low) / $delta * 255, 0, 255);
1927 $cols['green'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(($cols['green'] - $low) / $delta * 255, 0, 255);
1928 $cols['blue'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(($cols['blue'] - $low) / $delta * 255, 0, 255);
1929 ImageColorSet($im, $c, $cols['red'], $cols['green'], $cols['blue']);
1930 }
1931 }
1932 }
1933
1934 /**
1935 * Reduce colors in image using IM and create a palette based image if possible (<=256 colors)
1936 *
1937 * @param string $file Image file to reduce
1938 * @param integer $cols Number of colors to reduce the image to.
1939 * @return string Reduced file
1940 * @todo Define visibility
1941 */
1942 public function IMreduceColors($file, $cols) {
1943 $fI = \TYPO3\CMS\Core\Utility\GeneralUtility::split_fileref($file);
1944 $ext = strtolower($fI['fileext']);
1945 $result = $this->randomName() . '.' . $ext;
1946 if (($reduce = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($cols, 0, $ext == 'gif' ? 256 : $this->truecolorColors, 0)) > 0) {
1947 $params = ' -colors ' . $reduce;
1948 if ($reduce <= 256) {
1949 $params .= ' -type Palette';
1950 }
1951 if ($ext == 'png' && $reduce <= 256) {
1952 $prefix = 'png8:';
1953 }
1954 $this->imageMagickExec($file, $prefix . $result, $params);
1955 if ($result) {
1956 return $result;
1957 }
1958 }
1959 return '';
1960 }
1961
1962 /*********************************
1963 *
1964 * GIFBUILDER Helper functions
1965 *
1966 *********************************/
1967 /**
1968 * Checks if the $fontFile is already at an absolute path and if not, prepends the correct path.
1969 * Use PATH_site unless we are in the backend.
1970 * Call it by t3lib_stdGraphic::prependAbsolutePath()
1971 *
1972 * @param string $fontFile The font file
1973 * @return string The font file with absolute path.
1974 * @todo Define visibility
1975 */
1976 public function prependAbsolutePath($fontFile) {
1977 $absPath = defined('PATH_typo3') ? dirname(PATH_thisScript) . '/' : PATH_site;
1978 $fontFile = \TYPO3\CMS\Core\Utility\GeneralUtility::isAbsPath($fontFile) ? $fontFile : \TYPO3\CMS\Core\Utility\GeneralUtility::resolveBackPath($absPath . $fontFile);
1979 return $fontFile;
1980 }
1981
1982 /**
1983 * Returns the IM command for sharpening with ImageMagick 5 (when $this->V5_EFFECTS is set).
1984 * Uses $this->im5fx_sharpenSteps for translation of the factor to an actual command.
1985 *
1986 * @param integer $factor The sharpening factor, 0-100 (effectively in 10 steps)
1987 * @return string The sharpening command, eg. " -sharpen 3x4
1988 * @see makeText(), IMparams(), v5_blur()
1989 * @todo Define visibility
1990 */
1991 public function v5_sharpen($factor) {
1992 $factor = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(ceil($factor / 10), 0, 10);
1993 $sharpenArr = explode(',', ',' . $this->im5fx_sharpenSteps);
1994 $sharpenF = trim($sharpenArr[$factor]);
1995 if ($sharpenF) {
1996 $cmd = ' -sharpen ' . $sharpenF;
1997 return $cmd;
1998 }
1999 }
2000
2001 /**
2002 * Returns the IM command for blurring with ImageMagick 5 (when $this->V5_EFFECTS is set).
2003 * Uses $this->im5fx_blurSteps for translation of the factor to an actual command.
2004 *
2005 * @param integer $factor The blurring factor, 0-100 (effectively in 10 steps)
2006 * @return string The blurring command, eg. " -blur 3x4
2007 * @see makeText(), IMparams(), v5_sharpen()
2008 * @todo Define visibility
2009 */
2010 public function v5_blur($factor) {
2011 $factor = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(ceil($factor / 10), 0, 10);
2012 $blurArr = explode(',', ',' . $this->im5fx_blurSteps);
2013 $blurF = trim($blurArr[$factor]);
2014 if ($blurF) {
2015 $cmd = ' -blur ' . $blurF;
2016 return $cmd;
2017 }
2018 }
2019
2020 /**
2021 * Returns a random filename prefixed with "temp_" and then 32 char md5 hash (without extension) from $this->tempPath.
2022 * Used by functions in this class to create truely temporary files for the on-the-fly processing. These files will most likely be deleted right away.
2023 *
2024 * @return string
2025 * @todo Define visibility
2026 */
2027 public function randomName() {
2028 $this->createTempSubDir('temp/');
2029 return $this->tempPath . 'temp/' . md5(uniqid(''));
2030 }
2031
2032 /**
2033 * Applies offset value to coordinated in $cords.
2034 * Basically the value of key 0/1 of $OFFSET is added to keys 0/1 of $cords
2035 *
2036 * @param array $cords Integer coordinates in key 0/1
2037 * @param array $OFFSET Offset values in key 0/1
2038 * @return array Modified $cords array
2039 * @todo Define visibility
2040 */
2041 public function applyOffset($cords, $OFFSET) {
2042 $cords[0] = intval($cords[0]) + intval($OFFSET[0]);
2043 $cords[1] = intval($cords[1]) + intval($OFFSET[1]);
2044 return $cords;
2045 }
2046
2047 /**
2048 * Converts a "HTML-color" TypoScript datatype to RGB-values.
2049 * Default is 0,0,0
2050 *
2051 * @param string $string "HTML-color" data type string, eg. 'red', '#ffeedd' or '255,0,255'. You can also add a modifying operator afterwards. There are two options: "255,0,255 : 20" - will add 20 to values, result is "255,20,255". Or "255,0,255 : *1.23" which will multiply all RGB values with 1.23
2052 * @return array RGB values in key 0/1/2 of the array
2053 * @todo Define visibility
2054 */
2055 public function convertColor($string) {
2056 $col = array();
2057 $cParts = explode(':', $string, 2);
2058 // Finding the RGB definitions of the color:
2059 $string = $cParts[0];
2060 if (strstr($string, '#')) {
2061 $string = preg_replace('/[^A-Fa-f0-9]*/', '', $string);
2062 $col[] = HexDec(substr($string, 0, 2));
2063 $col[] = HexDec(substr($string, 2, 2));
2064 $col[] = HexDec(substr($string, 4, 2));
2065 } elseif (strstr($string, ',')) {
2066 $string = preg_replace('/[^,0-9]*/', '', $string);
2067 $strArr = explode(',', $string);
2068 $col[] = intval($strArr[0]);
2069 $col[] = intval($strArr[1]);
2070 $col[] = intval($strArr[2]);
2071 } else {
2072 $string = strtolower(trim($string));
2073 if ($this->colMap[$string]) {
2074 $col = $this->colMap[$string];
2075 } else {
2076 $col = array(0, 0, 0);
2077 }
2078 }
2079 // ... and possibly recalculating the value
2080 if (trim($cParts[1])) {
2081 $cParts[1] = trim($cParts[1]);
2082 if (substr($cParts[1], 0, 1) == '*') {
2083 $val = doubleval(substr($cParts[1], 1));
2084 $col[0] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($col[0] * $val, 0, 255);
2085 $col[1] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($col[1] * $val, 0, 255);
2086 $col[2] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($col[2] * $val, 0, 255);
2087 } else {
2088 $val = intval($cParts[1]);
2089 $col[0] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($col[0] + $val, 0, 255);
2090 $col[1] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($col[1] + $val, 0, 255);
2091 $col[2] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($col[2] + $val, 0, 255);
2092 }
2093 }
2094 return $col;
2095 }
2096
2097 /**
2098 * Recode string
2099 * Used with text strings for fonts when languages has other character sets.
2100 *
2101 * @param string The text to recode
2102 * @return string The recoded string. Should be UTF-8 output. MAY contain entities (eg. &#123; or &#quot; which should render as real chars).
2103 * @todo Define visibility
2104 */
2105 public function recodeString($string) {
2106 // Recode string to UTF-8 from $this->nativeCharset:
2107 if ($this->nativeCharset && $this->nativeCharset != 'utf-8') {
2108 // Convert to UTF-8
2109 $string = $this->csConvObj->utf8_encode($string, $this->nativeCharset);
2110 }
2111 return $string;
2112 }
2113
2114 /**
2115 * Split a string into an array of individual characters
2116 * The function will look at $this->nativeCharset and if that is set, the input string is expected to be UTF-8 encoded, possibly with entities in it. Otherwise the string is supposed to be a single-byte charset which is just splitted by a for-loop.
2117 *
2118 * @param string $theText The text string to split
2119 * @param boolean $returnUnicodeNumber Return Unicode numbers instead of chars.
2120 * @return array Numerical array with a char as each value.
2121 * @todo Define visibility
2122 */
2123 public function singleChars($theText, $returnUnicodeNumber = FALSE) {
2124 if ($this->nativeCharset) {
2125 // Get an array of separated UTF-8 chars
2126 return $this->csConvObj->utf8_to_numberarray($theText, 1, $returnUnicodeNumber ? 0 : 1);
2127 } else {
2128 $output = array();
2129 $c = strlen($theText);
2130 for ($a = 0; $a < $c; $a++) {
2131 $output[] = substr($theText, $a, 1);
2132 }
2133 return $output;
2134 }
2135 }
2136
2137 /**
2138 * Create an array with object position/boundaries based on input TypoScript configuration (such as the "align" property is used), the work area definition and $BB array
2139 *
2140 * @param array $conf TypoScript configuration for a GIFBUILDER object
2141 * @param array makeBox Workarea definition
2142 * @param array $BB BB (Bounding box) array. Not just used for TEXT objects but also for others
2143 * @return array [0]=x, [1]=y, [2]=w, [3]=h
2144 * @access private
2145 * @see copyGifOntoGif(), makeBox(), crop()
2146 * @todo Define visibility
2147 */
2148 public function objPosition($conf, $workArea, $BB) {
2149 // offset, align, valign, workarea
2150 $result = array();
2151 $result[2] = $BB[0];
2152 $result[3] = $BB[1];
2153 $w = $workArea[2];
2154 $h = $workArea[3];
2155 $align = explode(',', $conf['align']);
2156 $align[0] = strtolower(substr(trim($align[0]), 0, 1));
2157 $align[1] = strtolower(substr(trim($align[1]), 0, 1));
2158 switch ($align[0]) {
2159 case 'r':
2160 $result[0] = $w - $result[2];
2161 break;
2162 case 'c':
2163 $result[0] = round(($w - $result[2]) / 2);
2164 break;
2165 default:
2166 $result[0] = 0;
2167 break;
2168 }
2169 switch ($align[1]) {
2170 case 'b':
2171 // y pos
2172 $result[1] = $h - $result[3];
2173 break;
2174 case 'c':
2175 $result[1] = round(($h - $result[3]) / 2);
2176 break;
2177 default:
2178 $result[1] = 0;
2179 break;
2180 }
2181 $result = $this->applyOffset($result, \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $conf['offset']));
2182 $result = $this->applyOffset($result, $workArea);
2183 return $result;
2184 }
2185
2186 /***********************************
2187 *
2188 * Scaling, Dimensions of images
2189 *
2190 ***********************************/
2191 /**
2192 * Converts $imagefile to another file in temp-dir of type $newExt (extension).
2193 *
2194 * @param string $imagefile The image filepath
2195 * @param string $newExt New extension, eg. "gif", "png", "jpg", "tif". If $newExt is NOT set, the new imagefile will be of the original format. If newExt = 'WEB' then one of the web-formats is applied.
2196 * @param string $w Width. $w / $h is optional. If only one is given the image is scaled proportionally. If an 'm' exists in the $w or $h and if both are present the $w and $h is regarded as the Maximum w/h and the proportions will be kept
2197 * @param string $h Height. See $w
2198 * @param string $params Additional ImageMagick parameters.
2199 * @param string $frame Refers to which frame-number to select in the image. '' or 0 will select the first frame, 1 will select the next and so on...
2200 * @param array $options An array with options passed to getImageScale (see this function).
2201 * @param boolean $mustCreate If set, then another image than the input imagefile MUST be returned. Otherwise you can risk that the input image is good enough regarding messures etc and is of course not rendered to a new, temporary file in typo3temp/. But this option will force it to.
2202 * @return array [0]/[1] is w/h, [2] is file extension and [3] is the filename.
2203 * @see getImageScale(), typo3/show_item.php, fileList_ext::renderImage(), tslib_cObj::getImgResource(), SC_tslib_showpic::show(), maskImageOntoImage(), copyImageOntoImage(), scale()
2204 * @todo Define visibility
2205 */
2206 public function imageMagickConvert($imagefile, $newExt = '', $w = '', $h = '', $params = '', $frame = '', $options = array(), $mustCreate = FALSE) {
2207 if ($this->NO_IMAGE_MAGICK) {
2208 // Returning file info right away
2209 return $this->getImageDimensions($imagefile);
2210 }
2211 if ($info = $this->getImageDimensions($imagefile)) {
2212 $newExt = strtolower(trim($newExt));
2213 // If no extension is given the original extension is used
2214 if (!$newExt) {
2215 $newExt = $info[2];
2216 }
2217 if ($newExt == 'web') {
2218 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($this->webImageExt, $info[2])) {
2219 $newExt = $info[2];
2220 } else {
2221 $newExt = $this->gif_or_jpg($info[2], $info[0], $info[1]);
2222 if (!$params) {
2223 $params = $this->cmds[$newExt];
2224 }
2225 }
2226 }
2227 if (\TYPO3\CMS\Core\Utility\GeneralUtility::inList($this->imageFileExt, $newExt)) {
2228 if (strstr($w . $h, 'm')) {
2229 $max = 1;
2230 } else {
2231 $max = 0;
2232 }
2233 $data = $this->getImageScale($info, $w, $h, $options);
2234 $w = $data['origW'];
2235 $h = $data['origH'];
2236 // If no conversion should be performed
2237 // this flag is TRUE if the width / height does NOT dictate
2238 // the image to be scaled!! (that is if no width / height is
2239 // given or if the destination w/h matches the original image
2240 // dimensions or if the option to not scale the image is set)
2241 $noScale = !$w && !$h || $data[0] == $info[0] && $data[1] == $info[1] || $options['noScale'];
2242 if ($noScale && !$data['crs'] && !$params && !$frame && $newExt == $info[2] && !$mustCreate) {
2243 // Set the new width and height before returning,
2244 // if the noScale option is set
2245 if (!empty($options['noScale'])) {
2246 $info[0] = $data[0];
2247 $info[1] = $data[1];
2248 }
2249 $info[3] = $imagefile;
2250 return $info;
2251 }
2252 $info[0] = $data[0];
2253 $info[1] = $data[1];
2254 $frame = $this->noFramePrepended ? '' : intval($frame);
2255 if (!$params) {
2256 $params = $this->cmds[$newExt];
2257 }
2258 // Cropscaling:
2259 if ($data['crs']) {
2260 if (!$data['origW']) {
2261 $data['origW'] = $data[0];
2262 }
2263 if (!$data['origH']) {
2264 $data['origH'] = $data[1];
2265 }
2266 $offsetX = intval(($data[0] - $data['origW']) * ($data['cropH'] + 100) / 200);
2267 $offsetY = intval(($data[1] - $data['origH']) * ($data['cropV'] + 100) / 200);
2268 $params .= ' -crop ' . $data['origW'] . 'x' . $data['origH'] . '+' . $offsetX . '+' . $offsetY . ' ';
2269 }
2270 $command = $this->scalecmd . ' ' . $info[0] . 'x' . $info[1] . '! ' . $params . ' ';
2271 $cropscale = $data['crs'] ? 'crs-V' . $data['cropV'] . 'H' . $data['cropH'] : '';
2272 if ($this->alternativeOutputKey) {
2273 $theOutputName = \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5($command . $cropscale . basename($imagefile) . $this->alternativeOutputKey . '[' . $frame . ']');
2274 } else {
2275 $theOutputName = \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5($command . $cropscale . $imagefile . filemtime($imagefile) . '[' . $frame . ']');
2276 }
2277 if ($this->imageMagickConvert_forceFileNameBody) {
2278 $theOutputName = $this->imageMagickConvert_forceFileNameBody;
2279 $this->imageMagickConvert_forceFileNameBody = '';
2280 }
2281 // Making the temporary filename:
2282 $this->createTempSubDir('pics/');
2283 $output = $this->absPrefix . $this->tempPath . 'pics/' . $this->filenamePrefix . $theOutputName . '.' . $newExt;
2284 // Register temporary filename:
2285 $GLOBALS['TEMP_IMAGES_ON_PAGE'][] = $output;
2286 if ($this->dontCheckForExistingTempFile || !$this->file_exists_typo3temp_file($output, $imagefile)) {
2287 $this->imageMagickExec($imagefile, $output, $command, $frame);
2288 }
2289 if (file_exists($output)) {
2290 $info[3] = $output;
2291 $info[2] = $newExt;
2292 // params could realisticly change some imagedata!
2293 if ($params) {
2294 $info = $this->getImageDimensions($info[3]);
2295 }
2296 if ($info[2] == $this->gifExtension && !$this->dontCompress) {
2297 // Compress with IM (lzw) or GD (rle) (Workaround for the absence of lzw-compression in GD)
2298 \TYPO3\CMS\Core\Utility\GeneralUtility::gif_compress($info[3], '');
2299 }
2300 return $info;
2301 }
2302 }
2303 }
2304 }
2305
2306 /**
2307 * Gets the input image dimensions.
2308 *
2309 * @param string $imageFile The image filepath
2310 * @return array Returns an array where [0]/[1] is w/h, [2] is extension and [3] is the filename.
2311 * @see imageMagickConvert(), tslib_cObj::getImgResource()
2312 * @todo Define visibility
2313 */
2314 public function getImageDimensions($imageFile) {
2315 preg_match('/([^\\.]*)$/', $imageFile, $reg);
2316 if (file_exists($imageFile) && \TYPO3\CMS\Core\Utility\GeneralUtility::inList($this->imageFileExt, strtolower($reg[0]))) {
2317 if ($returnArr = $this->getCachedImageDimensions($imageFile)) {
2318 return $returnArr;
2319 } else {
2320 if ($temp = @getImageSize($imageFile)) {
2321 $returnArr = array($temp[0], $temp[1], strtolower($reg[0]), $imageFile);
2322 } else {
2323 $returnArr = $this->imageMagickIdentify($imageFile);
2324 }
2325 if ($returnArr) {
2326 $this->cacheImageDimensions($returnArr);
2327 return $returnArr;
2328 }
2329 }
2330 }
2331 return FALSE;
2332 }
2333
2334 /**
2335 * Cache the result of the getImageDimensions function into the database. Does not check if the
2336 * file exists!
2337 *
2338 * @param array $identifyResult Result of the getImageDimensions function
2339 * @return boolean TRUE if operation was successful
2340 * @todo Define visibility
2341 */
2342 public function cacheImageDimensions($identifyResult) {
2343 // Create md5 hash of filemtime and filesize
2344 $md5Hash = md5(filemtime($identifyResult[3]) . filesize($identifyResult[3]));
2345 $result = FALSE;
2346 if ($md5Hash) {
2347 $fieldArray = array(
2348 'md5hash' => $md5Hash,
2349 'md5filename' => md5($identifyResult[3]),
2350 'tstamp' => $GLOBALS['EXEC_TIME'],
2351 'filename' => $identifyResult[3],
2352 'imagewidth' => $identifyResult[0],
2353 'imageheight' => $identifyResult[1]
2354 );
2355 $GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_imagesizes', $fieldArray);
2356 if (!($err = $GLOBALS['TYPO3_DB']->sql_error())) {
2357 $result = TRUE;
2358 }
2359 }
2360 return $result;
2361 }
2362
2363 /**
2364 * Fetch the cached imageDimensions from the MySQL database. Does not check if the image file exists!
2365 *
2366 * @param string $imageFile The image filepath
2367 * @return array Returns an array where [0]/[1] is w/h, [2] is extension and [3] is the filename.
2368 * @todo Define visibility
2369 */
2370 public function getCachedImageDimensions($imageFile) {
2371 // Create md5 hash of filemtime and filesize
2372 $md5Hash = md5(filemtime($imageFile) . filesize($imageFile));
2373 $cachedImageDimensions = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('md5hash, md5filename, imagewidth, imageheight', 'cache_imagesizes', 'md5filename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr(md5($imageFile), 'cache_imagesizes'));
2374 $result = FALSE;
2375 if (is_array($cachedImageDimensions)) {
2376 if ($cachedImageDimensions['md5hash'] != $md5Hash) {
2377 // File has changed, delete the row
2378 $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_imagesizes', 'md5filename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($cachedImageDimensions['md5filename'], 'cache_imagesizes'));
2379 } else {
2380 preg_match('/([^\\.]*)$/', $imageFile, $imageExtension);
2381 $result = array(
2382 (int) $cachedImageDimensions['imagewidth'],
2383 (int) $cachedImageDimensions['imageheight'],
2384 strtolower($imageExtension[0]),
2385 $imageFile
2386 );
2387 }
2388 }
2389 return $result;
2390 }
2391
2392 /**
2393 * Get numbers for scaling the image based on input
2394 *
2395 * @param array $info Current image information: Width, Height etc.
2396 * @param integer $w "required" width
2397 * @param integer $h "required" height
2398 * @param array $options Options: Keys are like "maxW", "maxH", "minW", "minH
2399 * @return array
2400 * @access private
2401 * @see imageMagickConvert()
2402 * @todo Define visibility
2403 */
2404 public function getImageScale($info, $w, $h, $options) {
2405 if (strstr($w . $h, 'm')) {
2406 $max = 1;
2407 } else {
2408 $max = 0;
2409 }
2410 if (strstr($w . $h, 'c')) {
2411 $out['cropH'] = intval(substr(strstr($w, 'c'), 1));
2412 $out['cropV'] = intval(substr(strstr($h, 'c'), 1));
2413 $crs = TRUE;
2414 } else {
2415 $crs = FALSE;
2416 }
2417 $out['crs'] = $crs;
2418 $w = intval($w);
2419 $h = intval($h);
2420 // If there are max-values...
2421 if (!empty($options['maxW'])) {
2422 // If width is given...
2423 if ($w) {
2424 if ($w > $options['maxW']) {
2425 $w = $options['maxW'];
2426 // Height should follow
2427 $max = 1;
2428 }
2429 } else {
2430 if ($info[0] > $options['maxW']) {
2431 $w = $options['maxW'];
2432 // Height should follow
2433 $max = 1;
2434 }
2435 }
2436 }
2437 if (!empty($options['maxH'])) {
2438 // If height is given...
2439 if ($h) {
2440 if ($h > $options['maxH']) {
2441 $h = $options['maxH'];
2442 // Height should follow
2443 $max = 1;
2444 }
2445 } else {
2446 // Changed [0] to [1] 290801
2447 if ($info[1] > $options['maxH']) {
2448 $h = $options['maxH'];
2449 // Height should follow
2450 $max = 1;
2451 }
2452 }
2453 }
2454 $out['origW'] = $w;
2455 $out['origH'] = $h;
2456 $out['max'] = $max;
2457 if (!$this->mayScaleUp) {
2458 if ($w > $info[0]) {
2459 $w = $info[0];
2460 }
2461 if ($h > $info[1]) {
2462 $h = $info[1];
2463 }
2464 }
2465 // If scaling should be performed
2466 if ($w || $h) {
2467 if ($w && !$h) {
2468 $info[1] = ceil($info[1] * ($w / $info[0]));
2469 $info[0] = $w;
2470 }
2471 if (!$w && $h) {
2472 $info[0] = ceil($info[0] * ($h / $info[1]));
2473 $info[1] = $h;
2474 }
2475 if ($w && $h) {
2476 if ($max) {
2477 $ratio = $info[0] / $info[1];
2478 if ($h * $ratio > $w) {
2479 $h = round($w / $ratio);
2480 } else {
2481 $w = round($h * $ratio);
2482 }
2483 }
2484 if ($crs) {
2485 $ratio = $info[0] / $info[1];
2486 if ($h * $ratio < $w) {
2487 $h = round($w / $ratio);
2488 } else {
2489 $w = round($h * $ratio);
2490 }
2491 }
2492 $info[0] = $w;
2493 $info[1] = $h;
2494 }
2495 }
2496 $out[0] = $info[0];
2497 $out[1] = $info[1];
2498 // Set minimum-measures!
2499 if (isset($options['minW']) && $out[0] < $options['minW']) {
2500 if (($max || $crs) && $out[0]) {
2501 $out[1] = round($out[1] * $options['minW'] / $out[0]);
2502 }
2503 $out[0] = $options['minW'];
2504 }
2505 if (isset($options['minH']) && $out[1] < $options['minH']) {
2506 if (($max || $crs) && $out[1]) {
2507 $out[0] = round($out[0] * $options['minH'] / $out[1]);
2508 }
2509 $out[1] = $options['minH'];
2510 }
2511 return $out;
2512 }
2513
2514 /**
2515 * Used to check if a certain process of scaling an image is already being carried out (can be logged in the SQL database)
2516 *
2517 * @param string $output Output imagefile
2518 * @param string $orig Original basis file
2519 * @return boolean Returns TRUE if the file is already being made; thus "TRUE" means "Don't render the image again
2520 * @access private
2521 * @todo Define visibility
2522 */
2523 public function file_exists_typo3temp_file($output, $orig = '') {
2524 if ($this->enable_typo3temp_db_tracking) {
2525 // If file exists, then we return immediately
2526 if (file_exists($output)) {
2527 return 1;
2528 } else {
2529 // If not, we look up in the cache_typo3temp_log table to see if there is a image being rendered right now.
2530 $md5Hash = md5($output);
2531 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('md5hash', 'cache_typo3temp_log', 'md5hash=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($md5Hash, 'cache_typo3temp_log') . ' AND tstamp>' . ($GLOBALS['EXEC_TIME'] - 30));
2532 // If there was a record, the image is being generated by another proces (we assume)
2533 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
2534 $GLOBALS['TYPO3_DB']->sql_free_result($res);
2535 if (is_object($GLOBALS['TSFE'])) {
2536 $GLOBALS['TSFE']->set_no_cache();
2537 }
2538 // ...so we set no_cache, because we dont want this page (which will NOT display an image...!) to be cached! (Only a page with the correct image on...)
2539 if (is_object($GLOBALS['TT'])) {
2540 $GLOBALS['TT']->setTSlogMessage('typo3temp_log: Assume this file is being rendered now: ' . $output);
2541 }
2542 // Return 'success - 2'
2543 return 2;
2544 } else {
2545 // If the current time is more than 30 seconds since this record was written, we clear the record, write a new and render the image.
2546 $insertFields = array(
2547 'md5hash' => $md5Hash,
2548 'tstamp' => $GLOBALS['EXEC_TIME'],
2549 'filename' => $output,
2550 'orig_filename' => $orig
2551 );
2552 $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_typo3temp_log', 'md5hash=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($md5Hash, 'cache_typo3temp_log'));
2553 $GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_typo3temp_log', $insertFields);
2554 if (is_object($GLOBALS['TT'])) {
2555 $GLOBALS['TT']->setTSlogMessage('typo3temp_log: The row did not exist, so a new is written and file is being processed: ' . $output);
2556 }
2557 return 0;
2558 }
2559 }
2560 } else {
2561 return file_exists($output);
2562 }
2563 }
2564
2565 /***********************************
2566 *
2567 * ImageMagick API functions
2568 *
2569 ***********************************/
2570 /**
2571 * Returns an array where [0]/[1] is w/h, [2] is extension and [3] is the filename.
2572 * Using ImageMagick
2573 *
2574 * @param string $imagefile The relative (to PATH_site) image filepath
2575 * @return array
2576 * @todo Define visibility
2577 */
2578 public function imageMagickIdentify($imagefile) {
2579 if (!$this->NO_IMAGE_MAGICK) {
2580 $frame = $this->noFramePrepended ? '' : '[0]';
2581 $cmd = \TYPO3\CMS\Core\Utility\GeneralUtility::imageMagickCommand('identify', $this->wrapFileName($imagefile) . $frame);
2582 $returnVal = array();
2583 \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd, $returnVal);
2584 $splitstring = $returnVal[0];
2585 $this->IM_commands[] = array('identify', $cmd, $returnVal[0]);
2586 if ($splitstring) {
2587 preg_match('/([^\\.]*)$/', $imagefile, $reg);
2588 $splitinfo = explode(' ', $splitstring);
2589 foreach ($splitinfo as $key => $val) {
2590 $temp = '';
2591 if ($val) {
2592 $temp = explode('x', $val);
2593 }
2594 if (intval($temp[0]) && intval($temp[1])) {
2595 $dim = $temp;
2596 break;
2597 }
2598 }
2599 if ($dim[0] && $dim[1]) {
2600 return array($dim[0], $dim[1], strtolower($reg[0]), $imagefile);
2601 }
2602 }
2603 }
2604 }
2605
2606 /**
2607 * Executes a ImageMagick "convert" on two filenames, $input and $output using $params before them.
2608 * Can be used for many things, mostly scaling and effects.
2609 *
2610 * @param string $input The relative (to PATH_site) image filepath, input file (read from)
2611 * @param string $output The relative (to PATH_site) image filepath, output filename (written to)
2612 * @param string $params ImageMagick parameters
2613 * @param integer $frame Optional, refers to which frame-number to select in the image. '' or 0
2614 * @return string The result of a call to PHP function "exec()
2615 * @todo Define visibility
2616 */
2617 public function imageMagickExec($input, $output, $params, $frame = 0) {
2618 if (!$this->NO_IMAGE_MAGICK) {
2619 // Unless noFramePrepended is set in the Install Tool, a frame number is added to
2620 // select a specific page of the image (by default this will be the first page)
2621 if (!$this->noFramePrepended) {
2622 $frame = '[' . intval($frame) . ']';
2623 } else {
2624 $frame = '';
2625 }
2626 $cmd = \TYPO3\CMS\Core\Utility\GeneralUtility::imageMagickCommand('convert', $params . ' ' . $this->wrapFileName($input) . $frame . ' ' . $this->wrapFileName($output));
2627 $this->IM_commands[] = array($output, $cmd);
2628 $ret = \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd);
2629 // Change the permissions of the file
2630 \TYPO3\CMS\Core\Utility\GeneralUtility::fixPermissions($output);
2631 return $ret;
2632 }
2633 }
2634
2635 /**
2636 * Executes a ImageMagick "combine" (or composite in newer times) on four filenames - $input, $overlay and $mask as input files and $output as the output filename (written to)
2637 * Can be used for many things, mostly scaling and effects.
2638 *
2639 * @param string $input The relative (to PATH_site) image filepath, bottom file
2640 * @param string $overlay The relative (to PATH_site) image filepath, overlay file (top)
2641 * @param string $mask The relative (to PATH_site) image filepath, the mask file (grayscale)
2642 * @param string $output The relative (to PATH_site) image filepath, output filename (written to)
2643 * @param boolean $handleNegation
2644 * @return void
2645 * @todo Define visibility
2646 */
2647 public function combineExec($input, $overlay, $mask, $output, $handleNegation = FALSE) {
2648 if (!$this->NO_IMAGE_MAGICK) {
2649 $params = '-colorspace GRAY +matte';
2650 if ($handleNegation) {
2651 if ($this->maskNegate) {
2652 $params .= ' ' . $this->maskNegate;
2653 }
2654 }
2655 $theMask = $this->randomName() . '.' . $this->gifExtension;
2656 $this->imageMagickExec($mask, $theMask, $params);
2657 $cmd = \TYPO3\CMS\Core\Utility\GeneralUtility::imageMagickCommand('combine', '-compose over +matte ' . $this->wrapFileName($input) . ' ' . $this->wrapFileName($overlay) . ' ' . $this->wrapFileName($theMask) . ' ' . $this->wrapFileName($output));
2658 // +matte = no alpha layer in output
2659 $this->IM_commands[] = array($output, $cmd);
2660 $ret = \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd);
2661 // Change the permissions of the file
2662 \TYPO3\CMS\Core\Utility\GeneralUtility::fixPermissions($output);
2663 if (is_file($theMask)) {
2664 @unlink($theMask);
2665 }
2666 return $ret;
2667 }
2668 }
2669
2670 /**
2671 * Escapes a file name so it can safely be used on the command line.
2672 *
2673 * @param string $inputName filename to safeguard, must not be empty
2674 * @return string $inputName escaped as needed
2675 */
2676 protected function wrapFileName($inputName) {
2677 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
2678 $currentLocale = setlocale(LC_CTYPE, 0);
2679 setlocale(LC_CTYPE, $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemLocale']);
2680 }
2681 $escapedInputName = escapeshellarg($inputName);
2682 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
2683 setlocale(LC_CTYPE, $currentLocale);
2684 }
2685 return $escapedInputName;
2686 }
2687
2688 /***********************************
2689 *
2690 * Various IO functions
2691 *