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