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