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