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