/t3lib/class.t3lib_stdgraphic.php
PHP | 2999 lines | 1831 code | 235 blank | 933 comment | 413 complexity | 9bd815c852e86bc7b4d3a0640283f880 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, BSD-2-Clause, AGPL-1.0, LGPL-3.0, GPL-3.0, MIT, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
- /***************************************************************
- * Copyright notice
- *
- * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
- * All rights reserved
- *
- * This script is part of the TYPO3 project. The TYPO3 project is
- * free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * The GNU General Public License can be found at
- * http://www.gnu.org/copyleft/gpl.html.
- * A copy is found in the textfile GPL.txt and important notices to the license
- * from the author is found in LICENSE.txt distributed with these scripts.
- *
- *
- * This script is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * This copyright notice MUST APPEAR in all copies of the script!
- ***************************************************************/
- /**
- * Standard graphical functions
- *
- * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
- *
- * @author Kasper Skårhøj <kasperYYYY@typo3.com>
- */
- /**
- * Class contains a bunch of cool functions for manipulating graphics with GDlib/Freetype and ImageMagick
- * VERY OFTEN used with gifbuilder that extends this class and provides a TypoScript API to using these functions
- *
- * With TYPO3 4.4 GDlib 1.x support was dropped, also an option from $TYPO3_CONF_VARS
- * $TYPO3_CONF_VARS['GFX']['gdlib_2'] = 0, // String/Boolean. Set this if you are using the new GDlib 2.0.1+. If you don't set this flag and still use GDlib2, you might encounter strange behaviours like black images etc. This feature might take effect only if ImageMagick is installed and working as well! You can also use the value "no_imagecopyresized_fix" - in that case it will NOT try to fix a known issue where "imagecopyresized" does not work correctly.
- *
- * @author Kasper Skårhøj <kasperYYYY@typo3.com>
- * @package TYPO3
- * @subpackage t3lib
- * @see tslib_gifBuilder
- */
- class t3lib_stdGraphic {
- // Internal configuration, set in init()
- // The ImageMagick filename used for combining two images. This name changed during the versions.
- var $combineScript = 'combine';
- // If set, there is no frame pointer prepended to the filenames.
- var $noFramePrepended = 0;
- // If set, imagecopyresized will not be called directly. For GD2 (some PHP installs?)
- var $imagecopyresized_fix = 0;
- // This should be changed to 'png' if you want this class to read/make PNG-files instead!
- var $gifExtension = 'gif';
- // File formats supported by gdlib. This variable get's filled in "init" method
- var $gdlibExtensions = '';
- // Set to TRUE if generated png's should be truecolor by default
- var $png_truecolor = FALSE;
- // 16777216 Colors is the maximum value for PNG, JPEG truecolor images (24-bit, 8-bit / Channel)
- var $truecolorColors = 0xffffff;
- // If set, then all files in typo3temp will be logged in a database table. In addition to being a log of the files with original filenames, it also serves to secure that the same image is not rendered simultaneously by two different processes.
- var $enable_typo3temp_db_tracking = 0;
- // Commalist of file extensions perceived as images by TYPO3. List should be set to 'gif,png,jpeg,jpg' if IM is not available. Lowercase and no spaces between!
- var $imageFileExt = 'gif,jpg,jpeg,png,tif,bmp,tga,pcx,ai,pdf';
- // Commalist of web image extensions (can be shown by a webbrowser)
- var $webImageExt = 'gif,jpg,jpeg,png';
- // Will be ' -negate' if ImageMagick ver 5.2+. See init();
- var $maskNegate = '';
- var $NO_IM_EFFECTS = '';
- var $cmds = array(
- 'jpg' => '',
- 'jpeg' => '',
- 'gif' => '',
- 'png' => '-colors 64'
- );
- var $NO_IMAGE_MAGICK = '';
- var $V5_EFFECTS = 0;
- var $mayScaleUp = 1;
- // Variables for testing, alternative usage etc.
- // Filename prefix for images scaled in imageMagickConvert()
- var $filenamePrefix = '';
- // Forcing the output filename of imageMagickConvert() to this value. However after calling imageMagickConvert() it will be set blank again.
- var $imageMagickConvert_forceFileNameBody = '';
- // This flag should always be FALSE. If set TRUE, imageMagickConvert will always write a new file to the tempdir! Used for debugging.
- var $dontCheckForExistingTempFile = 0;
- // Prevents imageMagickConvert() from compressing the gif-files with t3lib_div::gif_compress()
- var $dontCompress = 0;
- // For debugging ONLY!
- var $dontUnlinkTempFiles = 0;
- // For debugging only. Filenames will not be based on mtime and only filename (not path) will be used. This key is also included in the hash of the filename...
- var $alternativeOutputKey = '';
- // Internal:
- // All ImageMagick commands executed is stored in this array for tracking. Used by the Install Tools Image section
- var $IM_commands = array();
- var $workArea = array();
- /**
- * Preserve the alpha transparency layer of read PNG images
- *
- * @var boolean
- */
- protected $saveAlphaLayer = FALSE;
- // Constants:
- // 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.
- var $tempPath = 'typo3temp/';
- // Prefix for relative paths. Used in "show_item.php" script. Is prefixed the output file name IN imageMagickConvert()
- var $absPrefix = '';
- // ImageMagick scaling command; "-geometry" eller "-sample". Used in makeText() and imageMagickConvert()
- var $scalecmd = '-geometry';
- // Used by v5_blur() to simulate 10 continuous steps of blurring
- var $im5fx_blurSteps = '1x2,2x2,3x2,4x3,5x3,5x4,6x4,7x5,8x5,9x5';
- // Used by v5_sharpen() to simulate 10 continuous steps of sharpening.
- var $im5fx_sharpenSteps = '1x2,2x2,3x2,2x3,3x3,4x3,3x4,4x4,4x5,5x5';
- // This is the limit for the number of pixels in an image before it will be rendered as JPG instead of GIF/PNG
- var $pixelLimitGif = 10000;
- // Array mapping HTML color names to RGB values.
- var $colMap = array(
- 'aqua' => array(0, 255, 255),
- 'black' => array(0, 0, 0),
- 'blue' => array(0, 0, 255),
- 'fuchsia' => array(255, 0, 255),
- 'gray' => array(128, 128, 128),
- 'green' => array(0, 128, 0),
- 'lime' => array(0, 255, 0),
- 'maroon' => array(128, 0, 0),
- 'navy' => array(0, 0, 128),
- 'olive' => array(128, 128, 0),
- 'purple' => array(128, 0, 128),
- 'red' => array(255, 0, 0),
- 'silver' => array(192, 192, 192),
- 'teal' => array(0, 128, 128),
- 'yellow' => array(255, 255, 0),
- 'white' => array(255, 255, 255)
- );
- /**
- * Charset conversion object:
- *
- * @var t3lib_cs
- */
- var $csConvObj;
- // Is set to the native character set of the input strings.
- var $nativeCharset = '';
- /**
- * Init function. Must always call this when using the class.
- * This function will read the configuration information from $GLOBALS['TYPO3_CONF_VARS']['GFX'] can set some values in internal variables.
- *
- * @return void
- */
- function init() {
- $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
- if (function_exists('imagecreatefromjpeg') && function_exists('imagejpeg')) {
- $this->gdlibExtensions .= ',jpg,jpeg';
- }
- if (function_exists('imagecreatefrompng') && function_exists('imagepng')) {
- $this->gdlibExtensions .= ',png';
- }
- if (function_exists('imagecreatefromgif') && function_exists('imagegif')) {
- $this->gdlibExtensions .= ',gif';
- }
- if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['png_truecolor']) {
- $this->png_truecolor = TRUE;
- }
- if (!$gfxConf['im']) {
- $this->NO_IMAGE_MAGICK = 1;
- }
- if (!$this->NO_IMAGE_MAGICK && (!$gfxConf['im_version_5'] || $gfxConf['im_version_5'] === 'im4' || $gfxConf['im_version_5'] === 'im5')) {
- 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
- );
- }
- // When GIFBUILDER gets used in truecolor mode
- // No colors parameter if we generate truecolor images.
- if ($this->png_truecolor) {
- $this->cmds['png'] = '';
- }
- // Setting default JPG parameters:
- $this->jpegQuality = t3lib_utility_Math::forceIntegerInRange($gfxConf['jpg_quality'], 10, 100, 75);
- $this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -sharpen 50 -quality ' . $this->jpegQuality;
- if ($gfxConf['im_combine_filename']) {
- $this->combineScript = $gfxConf['im_combine_filename'];
- }
- if ($gfxConf['im_noFramePrepended']) {
- $this->noFramePrepended = 1;
- }
- // Kept for backwards compatibility, can be turned on manually through localconf.php,
- // but not through the installer anymore
- $this->imagecopyresized_fix = ($gfxConf['gdlib_2'] === 'no_imagecopyresized_fix' ? 0 : 1);
- if ($gfxConf['gdlib_png']) {
- $this->gifExtension = 'png';
- }
- if ($gfxConf['enable_typo3temp_db_tracking']) {
- $this->enable_typo3temp_db_tracking = $gfxConf['enable_typo3temp_db_tracking'];
- }
- $this->imageFileExt = $gfxConf['imagefile_ext'];
- // This should be set if ImageMagick ver. 5+ is used.
- if ($gfxConf['im_negate_mask']) {
- // Boolean. Indicates if the mask images should be inverted first.
- // This depends of the ImageMagick version. Below ver. 5.1 this should be FALSE.
- // Above ImageMagick version 5.2+ it should be TRUE.
- // Just set the flag if the masks works opposite the intension!
- $this->maskNegate = ' -negate';
- }
- if ($gfxConf['im_no_effects']) {
- // Boolean. This is necessary if using ImageMagick 5+.
- // Effects in Imagemagick 5+ tends to render very slowly!!
- // - therefore must be disabled in order not to perform sharpen, blurring and such.
- $this->NO_IM_EFFECTS = 1;
- $this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -quality ' . $this->jpegQuality;
- }
- // ... but if 'im_v5effects' is set, don't care about 'im_no_effects'
- if ($gfxConf['im_v5effects']) {
- $this->NO_IM_EFFECTS = 0;
- $this->V5_EFFECTS = 1;
- if ($gfxConf['im_v5effects'] > 0) {
- $this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -quality ' . intval($gfxConf['jpg_quality']) . $this->v5_sharpen(10);
- }
- }
- // Secures that images are not scaled up.
- if ($gfxConf['im_noScaleUp']) {
- $this->mayScaleUp = 0;
- }
- if (TYPO3_MODE == 'FE') {
- $this->csConvObj = $GLOBALS['TSFE']->csConvObj;
- } elseif (is_object($GLOBALS['LANG'])) { // BE assumed:
- $this->csConvObj = $GLOBALS['LANG']->csConvObj;
- } else { // The object may not exist yet, so we need to create it now. Happens in the Install Tool for example.
- $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
- }
- $this->nativeCharset = 'utf-8';
- }
- /*************************************************
- *
- * Layering images / "IMAGE" GIFBUILDER object
- *
- *************************************************/
- /**
- * Implements the "IMAGE" GIFBUILDER object, when the "mask" property is TRUE.
- * 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
- * The operation involves ImageMagick for combining.
- *
- * @param pointer $im GDlib image pointer
- * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
- * @param array $workArea The current working area coordinates.
- * @return void
- * @see tslib_gifBuilder::make()
- */
- function maskImageOntoImage(&$im, $conf, $workArea) {
- if ($conf['file'] && $conf['mask']) {
- $imgInf = pathinfo($conf['file']);
- $imgExt = strtolower($imgInf['extension']);
- if (!t3lib_div::inList($this->gdlibExtensions, $imgExt)) {
- $BBimage = $this->imageMagickConvert($conf['file'], $this->gifExtension, '', '', '', '', '');
- } else {
- $BBimage = $this->getImageDimensions($conf['file']);
- }
- $maskInf = pathinfo($conf['mask']);
- $maskExt = strtolower($maskInf['extension']);
- if (!t3lib_div::inList($this->gdlibExtensions, $maskExt)) {
- $BBmask = $this->imageMagickConvert($conf['mask'], $this->gifExtension, '', '', '', '', '');
- } else {
- $BBmask = $this->getImageDimensions($conf['mask']);
- }
- if ($BBimage && $BBmask) {
- $w = imagesx($im);
- $h = imagesy($im);
- $tmpStr = $this->randomName();
- $theImage = $tmpStr . '_img.' . $this->gifExtension;
- $theDest = $tmpStr . '_dest.' . $this->gifExtension;
- $theMask = $tmpStr . '_mask.' . $this->gifExtension;
- // Prepare overlay image
- $cpImg = $this->imageCreateFromFile($BBimage[3]);
- $destImg = imagecreatetruecolor($w, $h);
- // Preserve alpha transparency
- if ($this->saveAlphaLayer) {
- imagealphablending($destImg, FALSE);
- imagesavealpha($destImg, TRUE);
- $Bcolor = imagecolorallocatealpha($destImg, 0, 0, 0, 127);
- imagefill($destImg, 0, 0, $Bcolor);
- } else {
- $Bcolor = ImageColorAllocate($destImg, 0, 0, 0);
- ImageFilledRectangle($destImg, 0, 0, $w, $h, $Bcolor);
- }
- $this->copyGifOntoGif($destImg, $cpImg, $conf, $workArea);
- $this->ImageWrite($destImg, $theImage);
- imageDestroy($cpImg);
- imageDestroy($destImg);
- // Prepare mask image
- $cpImg = $this->imageCreateFromFile($BBmask[3]);
- $destImg = imagecreatetruecolor($w, $h);
- if ($this->saveAlphaLayer) {
- imagealphablending($destImg, FALSE);
- imagesavealpha($destImg, TRUE);
- $Bcolor = imagecolorallocatealpha($destImg, 0, 0, 0, 127);
- imagefill($destImg, 0, 0, $Bcolor);
- } else {
- $Bcolor = ImageColorAllocate($destImg, 0, 0, 0);
- ImageFilledRectangle($destImg, 0, 0, $w, $h, $Bcolor);
- }
- $this->copyGifOntoGif($destImg, $cpImg, $conf, $workArea);
- $this->ImageWrite($destImg, $theMask);
- imageDestroy($cpImg);
- imageDestroy($destImg);
- // Mask the images
- $this->ImageWrite($im, $theDest);
- // Let combineExec handle maskNegation
- $this->combineExec($theDest, $theImage, $theMask, $theDest, TRUE);
- // The main image is loaded again...
- $backIm = $this->imageCreateFromFile($theDest);
- // ... and if nothing went wrong we load it onto the old one.
- if ($backIm) {
- if (!$this->saveAlphaLayer) {
- ImageColorTransparent($backIm, -1);
- }
- $im = $backIm;
- }
- // Unlink files from process
- if (!$this->dontUnlinkTempFiles) {
- unlink($theDest);
- unlink($theImage);
- unlink($theMask);
- }
- }
- }
- }
- /**
- * Implements the "IMAGE" GIFBUILDER object, when the "mask" property is FALSE (using only $conf['file'])
- *
- * @param pointer $im GDlib image pointer
- * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
- * @param array $workArea The current working area coordinates.
- * @return void
- * @see tslib_gifBuilder::make(), maskImageOntoImage()
- */
- function copyImageOntoImage(&$im, $conf, $workArea) {
- if ($conf['file']) {
- if (!t3lib_div::inList($this->gdlibExtensions, $conf['BBOX'][2])) {
- $conf['BBOX'] = $this->imageMagickConvert($conf['BBOX'][3], $this->gifExtension, '', '', '', '', '');
- $conf['file'] = $conf['BBOX'][3];
- }
- $cpImg = $this->imageCreateFromFile($conf['file']);
- $this->copyGifOntoGif($im, $cpImg, $conf, $workArea);
- imageDestroy($cpImg);
- }
- }
- /**
- * Copies two GDlib image pointers onto each other, using TypoScript configuration from $conf and the input $workArea definition.
- *
- * @param pointer $im GDlib image pointer, destination (bottom image)
- * @param pointer $cpImg GDlib image pointer, source (top image)
- * @param array $conf TypoScript array with the properties for the IMAGE GIFBUILDER object. Only used for the "tile" property value.
- * @param array $workArea Work area
- * @return void Works on the $im image pointer
- * @access private
- */
- function copyGifOntoGif(&$im, $cpImg, $conf, $workArea) {
- $cpW = imagesx($cpImg);
- $cpH = imagesy($cpImg);
- $tile = t3lib_div::intExplode(',', $conf['tile']);
- $tile[0] = t3lib_utility_Math::forceIntegerInRange($tile[0], 1, 20);
- $tile[1] = t3lib_utility_Math::forceIntegerInRange($tile[1], 1, 20);
- $cpOff = $this->objPosition($conf, $workArea, array($cpW * $tile[0], $cpH * $tile[1]));
- for ($xt = 0; $xt < $tile[0]; $xt++) {
- $Xstart = $cpOff[0] + $cpW * $xt;
- // If this image is inside of the workArea, then go on
- if ($Xstart + $cpW > $workArea[0]) {
- // X:
- if ($Xstart < $workArea[0]) {
- $cpImgCutX = $workArea[0] - $Xstart;
- $Xstart = $workArea[0];
- } else {
- $cpImgCutX = 0;
- }
- $w = $cpW - $cpImgCutX;
- if ($Xstart > $workArea[0] + $workArea[2] - $w) {
- $w = $workArea[0] + $workArea[2] - $Xstart;
- }
- // If this image is inside of the workArea, then go on
- if ($Xstart < $workArea[0] + $workArea[2]) {
- // Y:
- for ($yt = 0; $yt < $tile[1]; $yt++) {
- $Ystart = $cpOff[1] + $cpH * $yt;
- // If this image is inside of the workArea, then go on
- if ($Ystart + $cpH > $workArea[1]) {
- if ($Ystart < $workArea[1]) {
- $cpImgCutY = $workArea[1] - $Ystart;
- $Ystart = $workArea[1];
- } else {
- $cpImgCutY = 0;
- }
- $h = $cpH - $cpImgCutY;
- if ($Ystart > $workArea[1] + $workArea[3] - $h) {
- $h = $workArea[1] + $workArea[3] - $Ystart;
- }
- // If this image is inside of the workArea, then go on
- if ($Ystart < $workArea[1] + $workArea[3]) {
- $this->imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
- }
- }
- } // Y:
- }
- }
- }
- }
- /**
- * Alternative function for using the similar PHP function imagecopyresized(). Used for GD2 only.
- *
- * OK, the reason for this stupid fix is the following story:
- * GD1.x was capable of copying two images together and combining their palettes! GD2 is apparently not.
- * With GD2 only the palette of the dest-image is used which mostly results in totally black images when trying to
- * copy a color-ful image onto the destination.
- * The GD2-fix is to
- * 1) Create a blank TRUE-COLOR image
- * 2) Copy the destination image onto that one
- * 3) Then do the actual operation; Copying the source (top image) onto that
- * 4) ... and return the result pointer.
- * 5) Reduce colors (if we do not, the result may become strange!)
- * It works, but the resulting images is now a true-color PNG which may be very large.
- * So, why not use 'imagetruecolortopalette ($im, TRUE, 256)' - well because it does NOT WORK! So simple is that.
- *
- * @param resource $dstImg Destination image
- * @param resource $srcImg Source image
- * @param integer $dstX Destination x-coordinate
- * @param integer $dstY Destination y-coordinate
- * @param integer $srcX Source x-coordinate
- * @param integer $srcY Source y-coordinate
- * @param integer $dstWidth Destination width
- * @param integer $dstHeight Destination height
- * @param integer $srcWidth Source width
- * @param integer $srcHeight Source height
- * @return void
- * @access private
- * @see t3lib_iconWorks::imagecopyresized()
- */
- function imagecopyresized(&$dstImg, $srcImg, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight) {
- if ($this->imagecopyresized_fix) {
- // Make true color image
- $tmpImg = imagecreatetruecolor(imagesx($dstImg), imagesy($dstImg));
- // Copy the source image onto that
- imagecopyresized($tmpImg, $dstImg, 0, 0, 0, 0, imagesx($dstImg), imagesy($dstImg), imagesx($dstImg), imagesy($dstImg));
- // Then copy the source image onto that (the actual operation!)
- imagecopyresized($tmpImg, $srcImg, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
- // Set the destination image
- $dstImg = $tmpImg;
- } else {
- imagecopyresized($dstImg, $srcImg, $dstX, $dstY, $srcX, $srcY, $dstWidth, $dstHeight, $srcWidth, $srcHeight);
- }
- }
- /********************************
- *
- * Text / "TEXT" GIFBUILDER object
- *
- ********************************/
- /**
- * Implements the "TEXT" GIFBUILDER object
- *
- * @param pointer $im GDlib image pointer
- * @param array $conf TypoScript array with configuration for the GIFBUILDER object.
- * @param array $workArea The current working area coordinates.
- * @return void
- * @see tslib_gifBuilder::make()
- */
- function makeText(&$im, $conf, $workArea) {
- // Spacing
- list($spacing, $wordSpacing) = $this->calcWordSpacing($conf);
- // Position
- $txtPos = $this->txtPosition($conf, $workArea, $conf['BBOX']);
- $theText = $this->recodeString($conf['text']);
- if ($conf['imgMap'] && is_array($conf['imgMap.'])) {
- $this->addToMap($this->calcTextCordsForMap($conf['BBOX'][2], $txtPos, $conf['imgMap.']), $conf['imgMap.']);
- }
- if (!$conf['hideButCreateMap']) {
- // Font Color:
- $cols = $this->convertColor($conf['fontColor']);
- // NiceText is calculated
- if (!$conf['niceText']) {
- $Fcolor = ImageColorAllocate($im, $cols[0], $cols[1], $cols[2]);
- // antiAliasing is setup:
- $Fcolor = ($conf['antiAlias']) ? $Fcolor : -$Fcolor;
- for ($a = 0; $a < $conf['iterations']; $a++) {
- // If any kind of spacing applys, we use this function:
- if ($spacing || $wordSpacing) {
- $this->SpacedImageTTFText($im, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, self::prependAbsolutePath($conf['fontFile']), $theText, $spacing, $wordSpacing, $conf['splitRendering.']);
- } else {
- $this->renderTTFText($im, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, $conf['fontFile'], $theText, $conf['splitRendering.'], $conf);
- }
- }
- } else { // NICETEXT::
- // options anti_aliased and iterations is NOT available when doing this!!
- $w = imagesx($im);
- $h = imagesy($im);
- $tmpStr = $this->randomName();
- $fileMenu = $tmpStr . '_menuNT.' . $this->gifExtension;
- $fileColor = $tmpStr . '_colorNT.' . $this->gifExtension;
- $fileMask = $tmpStr . '_maskNT.' . $this->gifExtension;
- // Scalefactor
- $sF = t3lib_utility_Math::forceIntegerInRange($conf['niceText.']['scaleFactor'], 2, 5);
- $newW = ceil($sF * imagesx($im));
- $newH = ceil($sF * imagesy($im));
- // Make mask
- $maskImg = imagecreatetruecolor($newW, $newH);
- $Bcolor = ImageColorAllocate($maskImg, 255, 255, 255);
- ImageFilledRectangle($maskImg, 0, 0, $newW, $newH, $Bcolor);
- $Fcolor = ImageColorAllocate($maskImg, 0, 0, 0);
- // If any kind of spacing applys, we use this function:
- if ($spacing || $wordSpacing) {
- $this->SpacedImageTTFText($maskImg, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, self::prependAbsolutePath($conf['fontFile']), $theText, $spacing, $wordSpacing, $conf['splitRendering.'], $sF);
- } else {
- $this->renderTTFText($maskImg, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, $conf['fontFile'], $theText, $conf['splitRendering.'], $conf, $sF);
- }
- $this->ImageWrite($maskImg, $fileMask);
- ImageDestroy($maskImg);
- // Downscales the mask
- if ($this->NO_IM_EFFECTS) {
- if ($this->maskNegate) {
- // Negate 2 times makes no negate...
- $command = trim($this->scalecmd . ' ' . $w . 'x' . $h . '!');
- } else {
- $command = trim($this->scalecmd . ' ' . $w . 'x' . $h . '! -negate');
- }
- } else {
- if ($this->maskNegate) {
- $command = trim($conf['niceText.']['before'] . ' ' . $this->scalecmd . ' ' . $w . 'x' . $h . '! ' . $conf['niceText.']['after']);
- } else {
- $command = trim($conf['niceText.']['before'] . ' ' . $this->scalecmd . ' ' . $w . 'x' . $h . '! ' . $conf['niceText.']['after'] . ' -negate');
- }
- if ($conf['niceText.']['sharpen']) {
- if ($this->V5_EFFECTS) {
- $command .= $this->v5_sharpen($conf['niceText.']['sharpen']);
- } else {
- $command .= ' -sharpen ' . t3lib_utility_Math::forceIntegerInRange($conf['niceText.']['sharpen'], 1, 99);
- }
- }
- }
- $this->imageMagickExec($fileMask, $fileMask, $command);
- // Make the color-file
- $colorImg = imagecreatetruecolor($w, $h);
- $Ccolor = ImageColorAllocate($colorImg, $cols[0], $cols[1], $cols[2]);
- ImageFilledRectangle($colorImg, 0, 0, $w, $h, $Ccolor);
- $this->ImageWrite($colorImg, $fileColor);
- ImageDestroy($colorImg);
- // The mask is applied
- // The main pictures is saved temporarily
- $this->ImageWrite($im, $fileMenu);
- $this->combineExec($fileMenu, $fileColor, $fileMask, $fileMenu);
- // The main image is loaded again...
- $backIm = $this->imageCreateFromFile($fileMenu);
- // ... and if nothing went wrong we load it onto the old one.
- if ($backIm) {
- if (!$this->saveAlphaLayer) {
- ImageColorTransparent($backIm, -1);
- }
- $im = $backIm;
- }
- // Deleting temporary files;
- if (!$this->dontUnlinkTempFiles) {
- unlink($fileMenu);
- unlink($fileColor);
- unlink($fileMask);
- }
- }
- }
- }
- /**
- * Calculates text position for printing the text onto the image based on configuration like alignment and workarea.
- *
- * @param array $conf TypoScript array for the TEXT GIFBUILDER object
- * @param array $workArea Workarea definition
- * @param array $BB Bounding box information, was set in tslib_gifBuilder::start()
- * @return array [0]=x, [1]=y, [2]=w, [3]=h
- * @access private
- * @see makeText()
- */
- function txtPosition($conf, $workArea, $BB) {
- $bbox = $BB[2];
- $angle = intval($conf['angle']) / 180 * pi();
- $conf['angle'] = 0;
- $straightBB = $this->calcBBox($conf);
- // offset, align, valign, workarea
- // [0]=x, [1]=y, [2]=w, [3]=h
- $result = array();
- $result[2] = $BB[0];
- $result[3] = $BB[1];
- $w = $workArea[2];
- $h = $workArea[3];
- switch ($conf['align']) {
- case 'right':
- case 'center':
- $factor = abs(cos($angle));
- $sign = (cos($angle) < 0) ? -1 : 1;
- $len1 = $sign * $factor * $straightBB[0];
- $len2 = $sign * $BB[0];
- $result[0] = $w - ceil($len2 * $factor + (1 - $factor) * $len1);
- $factor = abs(sin($angle));
- $sign = (sin($angle) < 0) ? -1 : 1;
- $len1 = $sign * $factor * $straightBB[0];
- $len2 = $sign * $BB[1];
- $result[1] = ceil($len2 * $factor + (1 - $factor) * $len1);
- break;
- }
- switch ($conf['align']) {
- case 'right':
- break;
- case 'center':
- $result[0] = round(($result[0]) / 2);
- $result[1] = round(($result[1]) / 2);
- break;
- default:
- $result[0] = 0;
- $result[1] = 0;
- break;
- }
- $result = $this->applyOffset($result, t3lib_div::intExplode(',', $conf['offset']));
- $result = $this->applyOffset($result, $workArea);
- return $result;
- }
- /**
- * Calculates bounding box information for the TEXT GIFBUILDER object.
- *
- * @param array $conf TypoScript array for the TEXT GIFBUILDER object
- * @return array Array with three keys [0]/[1] being x/y and [2] being the bounding box array
- * @access private
- * @see txtPosition(), tslib_gifBuilder::start()
- */
- function calcBBox($conf) {
- $sF = $this->getTextScalFactor($conf);
- list($spacing, $wordSpacing) = $this->calcWordSpacing($conf, $sF);
- $theText = $this->recodeString($conf['text']);
- $charInf = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $theText, $conf['splitRendering.'], $sF);
- $theBBoxInfo = $charInf;
- if ($conf['angle']) {
- $xArr = array($charInf[0], $charInf[2], $charInf[4], $charInf[6]);
- $yArr = array($charInf[1], $charInf[3], $charInf[5], $charInf[7]);
- $x = max($xArr) - min($xArr);
- $y = max($yArr) - min($yArr);
- } else {
- $x = ($charInf[2] - $charInf[0]);
- $y = ($charInf[1] - $charInf[7]);
- }
- // Set original lineHeight (used by line breaks):
- $theBBoxInfo['lineHeight'] = $y;
- // If any kind of spacing applys, we use this function:
- if ($spacing || $wordSpacing) {
- $x = 0;
- if (!$spacing && $wordSpacing) {
- $bits = explode(' ', $theText);
- foreach ($bits as $word) {
- $word .= ' ';
- $wordInf = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $word, $conf['splitRendering.'], $sF);
- $wordW = ($wordInf[2] - $wordInf[0]);
- $x += $wordW + $wordSpacing;
- }
- } else {
- $utf8Chars = $this->singleChars($theText);
- // For each UTF-8 char, do:
- foreach ($utf8Chars as $char) {
- $charInf = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $char, $conf['splitRendering.'], $sF);
- $charW = ($charInf[2] - $charInf[0]);
- $x += $charW + (($char == ' ') ? $wordSpacing : $spacing);
- }
- }
- } elseif (isset($conf['breakWidth']) && $conf['breakWidth'] && $this->getRenderedTextWidth($conf['text'], $conf) > $conf['breakWidth']) {
- $maxWidth = 0;
- $currentWidth = 0;
- $breakWidth = $conf['breakWidth'];
- $breakSpace = $this->getBreakSpace($conf, $theBBoxInfo);
- $wordPairs = $this->getWordPairsForLineBreak($conf['text']);
- // Iterate through all word pairs:
- foreach ($wordPairs as $index => $wordPair) {
- $wordWidth = $this->getRenderedTextWidth($wordPair, $conf);
- if ($index == 0 || $currentWidth + $wordWidth <= $breakWidth) {
- $currentWidth += $wordWidth;
- } else {
- $maxWidth = max($maxWidth, $currentWidth);
- $y += $breakSpace;
- // Restart:
- $currentWidth = $wordWidth;
- }
- }
- $x = max($maxWidth, $currentWidth) * $sF;
- }
- if ($sF > 1) {
- $x = ceil($x / $sF);
- $y = ceil($y / $sF);
- if (is_array($theBBoxInfo)) {
- foreach ($theBBoxInfo as &$value) {
- $value = ceil($value / $sF);
- }
- unset($value);
- }
- }
- return array($x, $y, $theBBoxInfo);
- }
- /**
- * Adds an <area> tag to the internal variable $this->map which is used to accumulate the content for an ImageMap
- *
- * @param array $cords Coordinates for a polygon image map as created by ->calcTextCordsForMap()
- * @param array $conf Configuration for "imgMap." property of a TEXT GIFBUILDER object.
- * @return void
- * @access private
- * @see makeText(), calcTextCordsForMap()
- */
- function addToMap($cords, $conf) {
- $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']) . '" />';
- }
- /**
- * Calculating the coordinates for a TEXT string on an image map. Used in an <area> tag
- *
- * @param array $cords Coordinates (from BBOX array)
- * @param array $offset Offset array
- * @param array $conf Configuration for "imgMap." property of a TEXT GIFBUILDER object.
- * @return array
- * @access private
- * @see makeText(), calcTextCordsForMap()
- */
- function calcTextCordsForMap($cords, $offset, $conf) {
- $pars = t3lib_div::intExplode(',', $conf['explode'] . ',');
- $newCords[0] = $cords[0] + $offset[0] - $pars[0];
- $newCords[1] = $cords[1] + $offset[1] + $pars[1];
- $newCords[2] = $cords[2] + $offset[0] + $pars[0];
- $newCords[3] = $cords[3] + $offset[1] + $pars[1];
- $newCords[4] = $cords[4] + $offset[0] + $pars[0];
- $newCords[5] = $cords[5] + $offset[1] - $pars[1];
- $newCords[6] = $cords[6] + $offset[0] - $pars[0];
- $newCords[7] = $cords[7] + $offset[1] - $pars[1];
- return $newCords;
- }
- /**
- * Printing text onto an image like the PHP function imageTTFText does but in addition it offers options for spacing of letters and words.
- * Spacing is done by printing one char at a time and this means that the spacing is rather uneven and probably not very nice.
- * See
- *
- * @param pointer $im (See argument for PHP function imageTTFtext())
- * @param integer $fontSize (See argument for PHP function imageTTFtext())
- * @param integer $angle (See argument for PHP function imageTTFtext())
- * @param integer $x (See argument for PHP function imageTTFtext())
- * @param integer $y (See argument for PHP function imageTTFtext())
- * @param integer $Fcolor (See argument for PHP function imageTTFtext())
- * @param string $fontFile (See argument for PHP function imageTTFtext())
- * @param string $text (See argument for PHP function imageTTFtext()). UTF-8 string, possibly with entities in.
- * @param integer $spacing The spacing of letters in pixels
- * @param integer $wordSpacing The spacing of words in pixels
- * @param array $splitRenderingConf Array
- * @param integer $sF Scale factor
- * @return void
- * @access private
- */
- function SpacedImageTTFText(&$im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $text, $spacing, $wordSpacing, $splitRenderingConf, $sF = 1) {
- $spacing *= $sF;
- $wordSpacing *= $sF;
- if (!$spacing && $wordSpacing) {
- $bits = explode(' ', $text);
- foreach ($bits as $word) {
- $word .= ' ';
- $wordInf = $this->ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $word, $splitRenderingConf, $sF);
- $wordW = ($wordInf[2] - $wordInf[0]);
- $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $word, $splitRenderingConf, $sF);
- $x += $wordW + $wordSpacing;
- }
- } else {
- $utf8Chars = $this->singleChars($text);
- // For each UTF-8 char, do:
- foreach ($utf8Chars as $char) {
- $charInf = $this->ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $char, $splitRenderingConf, $sF);
- $charW = ($charInf[2] - $charInf[0]);
- $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $char, $splitRenderingConf, $sF);
- $x += $charW + (($char == ' ') ? $wordSpacing : $spacing);
- }
- }
- }
- /**
- * Function that finds the right fontsize that will render the textstring within a certain width
- *
- * @param array $conf The TypoScript properties of the TEXT GIFBUILDER object
- * @return integer The new fontSize
- * @access private
- * @see tslib_gifBuilder::start()
- */
- function fontResize($conf) {
- // 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!!!!
- $maxWidth = intval($conf['maxWidth']);
- list($spacing, $wordSpacing) = $this->calcWordSpacing($conf);
- if ($maxWidth) {
- // If any kind of spacing applys, we use this function:
- if ($spacing || $wordSpacing) {
- return $conf['fontSize'];
- // ################ no calc for spacing yet !!!!!!
- } else {
- do {
- // Determine bounding box.
- $bounds = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $this->recodeString($conf['text']), $conf['splitRendering.']);
- if ($conf['angle'] < 0) {
- $pixelWidth = abs($bounds[4] - $bounds[0]);
- } elseif ($conf['angle'] > 0) {
- $pixelWidth = abs($bounds[2] - $bounds[6]);
- } else {
- $pixelWidth = abs($bounds[4] - $bounds[6]);
- }
- // Size is fine, exit:
- if ($pixelWidth <= $maxWidth) {
- break;
- } else {
- $conf['fontSize']--;
- }
- } while ($conf['fontSize'] > 1);
- }
- // If spacing
- }
- return $conf['fontSize'];
- }
- /**
- * Wrapper for ImageTTFBBox
- *
- * @param integer $fontSize (See argument for PHP function ImageTTFBBox())
- * @param integer $angle (See argument for PHP function ImageTTFBBox())
- * @param string $fontFile (See argument for PHP function ImageTTFBBox())
- * @param string $string (See argument for PHP function ImageTTFBBox())
- * @param array $splitRendering Split-rendering configuration
- * @param integer $sF Scale factor
- * @return array Information array.
- */
- function ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $string, $splitRendering, $sF = 1) {
- // Initialize:
- $offsetInfo = array();
- $stringParts = $this->splitString($string, $splitRendering, $fontSize, $fontFile);
- // Traverse string parts:
- foreach ($stringParts as $strCfg) {
- $fontFile = self::prependAbsolutePath($strCfg['fontFile']);
- if (is_readable($fontFile)) {
- /**
- * Calculate Bounding Box for part.
- * Due to a PHP bug, we must retry if $calc[2] is negative.
- * @see https://bugs.php.net/bug.php?id=51315
- * @see https://bugs.php.net/bug.php?id=22513
- */
- $try = 0;
- do {
- $calc = ImageTTFBBox(
- t3lib_div::freetypeDpiComp($sF * $strCfg['fontSize']),
- $angle,
- $fontFile,
- $strCfg['str']
- );
- } while ($calc[2] < 0 && $try++ < 10);
- // Calculate offsets:
- if (!count($offsetInfo)) {
- // First run, just copy over.
- $offsetInfo = $calc;
- } else {
- $offsetInfo[2] += $calc[2] - $calc[0] + intval($splitRendering['compX']) + intval($strCfg['xSpaceBefore']) + intval($strCfg['xSpaceAfter']);
- $offsetInfo[3] += $calc[3] - $calc[1] - intval($splitRendering['compY']) - intval($strCfg['ySpaceBefore']) - intval($strCfg['ySpaceAfter']);
- $offsetInfo[4] += $calc[4] - $calc[6] + intval($splitRendering['compX']) + intval($strCfg['xSpaceBefore']) + intval($strCfg['xSpaceAfter']);
- $offsetInfo[5] += $calc[5] - $calc[7] - intval($splitRendering['compY']) - intval($strCfg['ySpaceBefore']) - intval($strCfg['ySpaceAfter']);
- }
- } else {
- debug('cannot read file: ' . $fontFile, 't3lib_stdGraphic::ImageTTFBBoxWrapper()');
- }
- }
- return $offsetInfo;
- }
- /**
- * Wrapper for ImageTTFText
- *
- * @param pointer $im (See argument for PHP function imageTTFtext())
- * @param integer $fontSize (See argument for PHP function imageTTFtext())
- * @param integer $angle (See argument for PHP function imageTTFtext())
- * @param integer $x (See argument for PHP function imageTTFtext())
- * @param integer $y (See argument for PHP function imageTTFtext())
- * @param integer $color (See argument for PHP function imageTTFtext())
- * @param string $fontFile (See argument for PHP function imageTTFtext())
- * @param string $string (See argument for PHP function imageTTFtext()). UTF-8 string, possibly with entities in.
- * @param array $splitRendering Split-rendering configuration
- * @param integer $sF Scale factor
- * @return void
- */
- function ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $string, $splitRendering, $sF = 1) {
- // Initialize:
- $stringParts = $this->splitString($string, $splitRendering, $fontSize, $fontFile);
- $x = ceil($sF * $x);
- $y = ceil($sF * $y);
- // Traverse string parts:
- foreach ($stringParts as $i => $strCfg) {
- // Initialize:
- $colorIndex = $color;
- // Set custom color if any (only when niceText is off):
- if ($strCfg['color'] && $sF == 1) {
- $cols = $this->convertColor($strCfg['color']);
- $colorIndex = ImageColorAllocate($im, $cols[0], $cols[1], $cols[2]);
- $colorIndex = $color >= 0 ? $colorIndex : -$colorIndex;
- }
- // Setting xSpaceBefore
- if ($i) {
- $x += intval($strCfg['xSpaceBefore']);
- $y -= intval($strCfg['ySpaceBefore']);
- }
- $fontFile = self::prependAbsolutePath($strCfg['fontFile']);
- if (is_readable($fontFile)) {
- // Render part:
- ImageTTFText($im, t3lib_div::freetypeDpiComp($sF * $strCfg['fontSize']), $angle, $x, $y, $colorIndex, $fontFile, $strCfg['str']);
- // Calculate offset to apply:
- $wordInf = ImageTTFBBox(t3lib_div::freetypeDpiComp($sF * $strCfg['fontSize']), $angle, self::prependAbsolutePath($strCfg['fontFile']), $strCfg['str']);
- $x += $wordInf[2] - $wordInf[0] + intval($splitRendering['compX']) + intval($strCfg['xSpaceAfter']);
- $y += $wordInf[5] - $wordInf[7] - intval($splitRendering['compY']) - intval($strCfg['ySpaceAfter']);
- } else {
- debug('cannot read file: ' . $fontFile, 't3lib_stdGraphic::ImageTTFTextWrapper()');
- }
- }
- }
- /**
- * Splitting a string for ImageTTFBBox up into an array where each part has its own configuration options.
- *
- * @param string $string UTF-8 string
- * @param array $splitRendering Split-rendering configuration from GIFBUILDER TEXT object.
- * @param integer $fontSize Current fontsize
- * @param string $fontFile Current font file
- * @return array Array with input string splitted according to configuration
- */
- function splitString($string, $splitRendering, $fontSize, $fontFile) {
- // Initialize by setting the whole string and default configuration as the first entry.
- $result = array();
- $result[] = array(
- 'str' => $string,
- 'fontSize' => $fontSize,
- 'fontFile' => $fontFile
- );
- // Traverse the split-rendering configuration:
- // Splitting will create more entries in $result with individual configurations.
- if (is_array($splitRendering)) {
- $sKeyArray = t3lib_TStemplate::sortedKeyList($splitRendering);
- // Traverse configured options:
- foreach ($sKeyArray as $key) {
- $cfg = $splitRendering[$key . '.'];
- // Process each type of split rendering keyword:
- switch ((string) $splitRendering[$key]) {
- case 'highlightWord':
- if (strlen($cfg['value'])) {
- $newResult = array();
- // Traverse the current parts of the result array:
- foreach ($result as $part) {
- // Explode the string value by the word value to highlight:
- $explodedParts = explode($cfg['value'], $part['str']);
- foreach ($explodedParts as $c => $expValue) {
- if (strlen($expValue)) {
- $newResult[] = array_merge($part, array('str' => $expValue));
- }
- if ($c + 1 < count($explodedParts)) {
- $newResult[] = array(
- 'str' => $cfg['value'],
- 'fontSize' => $cfg['fontSize'] ? $cfg['fontSize'] : $part['fontSize'],
- 'fontFile' => $cfg['fontFile'] ? $cfg['fontFile'] : $part['fontFile'],
- 'color' => $cfg['color'],
- 'xSpaceBefore' => $cfg['xSpaceBefore'],
- 'xSpaceAfter' => $cfg['xSpaceAfter'],
- 'ySpaceBefore' => $cfg['ySpaceBefore'],
- 'ySpaceAfter' => $cfg['ySpaceAfter'],
- );
- }
- }
- }
- // Set the new result as result array:
- if (count($newResult)) {
- $result = $newResult;
- }
- }
- break;
- case 'charRange':
- if (strlen($cfg['value'])) {
- // Initialize range:
- $ranges = t3lib_div::trimExplode(',', $cfg['value'], 1);
- foreach ($ranges as $i => $rangeDef) {
- $ranges[$i] = t3lib_div::intExplode('-', $ranges[$i]);
- if (!isset($ranges[$i][1])) {
- $ranges[$i][1] = $ranges[$i][0];
- }
- }
- $newResult = array();
- // Traverse the current parts of the result array:
- foreach ($result as $part) {
- // Initialize:
- $currentState = -1;
- $bankAccum = '';
- // Explode the string value by the word value to highlight:
- $utf8Chars = $this->singleChars($part['str']);
- foreach ($utf8Chars as $utfChar) {
- // Find number and evaluate position:
- $uNumber = $this->csConvObj->utf8CharToUnumber($utfChar);
- $inRange = 0;
- foreach ($ranges as $rangeDef) {
- if ($uNumber >= $rangeDef[0] && (!$rangeDef[1] || $uNumber <= $rangeDef[1])) {
- $inRange = 1;
- break;
- }
- }
- if ($currentState == -1) {
- $currentState = $inRange;
- } // Initialize first char
- // Switch bank:
- if ($inRange != $currentState && !t3lib_div::inList('32,10,13,9', $uNumber)) {
- // Set result:
- if (strlen($bankAccum)) {
- $newResult[] = array(
- 'str' => $bankAccum,
- 'fontSize' => $currentState && $cfg['fontSize'] ? $cfg['fontSize'] : $part['fontSize'],
- 'fontFile' => $currentState && $cfg['fontFile'] ? $cfg['fontFile'] : $part['fontFile'],
- 'color' => $currentState ? $cfg['color'] : '',
- 'xSpaceBefore' => $currentState ? $cfg['xSpaceBefore'] : '',
- 'xSpaceAfter' => $currentState ? $cfg['xSpaceAfter'] : '',
- 'ySpaceBefore' => $currentState ? $cfg['ySpaceBefore'] : '',
- 'ySpaceAfter' => $currentState ? $cfg['ySpaceAfter'] : '',
- );
- }
- // Initialize new settings:
- $currentState = $inRange;
- $bankAccum = '';
- }
- // Add char to bank:
- $bankAccum .= $utfChar;
- }
- // Set result for FINAL part:
- if (strlen($bankAccum)) {
- $newResult[] = array(
- 'str' => $bankAccum,
- 'fontSize' => $currentState && $cfg['fontSize'] ? $cfg['fontSize'] : $part['fontSize'],
- 'fontFile' => $currentState && $cfg['fontFile'] ? $cfg['fontFile'] : $part['fontFile'],
- 'color' => $currentState ? $cfg['color'] : '',
- 'xSpaceBefore' => $currentState ? $cfg['xSpaceBefore'] : '',
- 'xSpaceAfter' => $currentState ? $cfg['xSpaceAfter'] : '',
- 'ySpaceBefore' => $currentState ? $cfg['ySpaceBefore'] : '',
- 'ySpaceAfter' => $currentState ? $cfg['ySpaceAfter'] : '',
- );
- }
- }
- // Set the new result as result array:
- if (count($newResult)) {
- $result = $newResult;
- }
- }
- break;
- }
- }
- }
- return $result;
- }
- /**
- * Calculates the spacing and wordSpacing values
- *
- * @param array $conf TypoScript array for the TEXT GIFBUILDER object
- * @param integer $scaleFactor TypoScript value from eg $conf['niceText.']['scaleFactor']
- * @return array Array with two keys [0]/[1] being array($spacing,$wordSpacing)
- * @access private
- * @see calcBBox()
- */
- function calcWordSpacing($conf, $scaleFactor = 1) {
- $spacing = intval($conf['spacing']);
- $wordSpacing = intval($conf['wordSpacing']);
- $wordSpacing = $wordSpacing ? $wordSpacing : $spacing * 2;
- $spacing *= $scaleFactor;
- $wordSpacing *= $scaleFactor;
- return array($spacing, $wordSpacing);
- }
- /**
- * Calculates and returns the niceText.scaleFactor
- *
- * @param array $conf TypoScript array for the TEXT GIFBUILDER object
- * @return integer TypoScript value from eg $conf['niceText.']['scaleFactor']
- * @access private
- */
- function getTextScalFactor($conf) {
- if (!$conf['niceText']) {
- $sF = 1;
- } else { // NICETEXT::
- $sF = t3lib_utility_Math::forceIntegerInRange($conf['niceText.']['scaleFactor'], 2, 5);
- }
- return $sF;
- }
- /**
- * Renders a regular text and takes care of a possible line break automatically.
- *
- * @param pointer $im (See argument for PHP function imageTTFtext())
- * @param integer $fontSize (See argument for PHP function imageTTFtext())
- * @param integer $angle (See argument for PHP function imageTTFtext())
- * @param integer $x (See argument for PHP function imageTTFtext())
- * @param integer $y (See argument for PHP function imageTTFtext())
- * @param integer $color (See argument for PHP function imageTTFtext())
- * @param string $fontFile (See argument for PHP function imageTTFtext())
- * @param string $string (See argument for PHP function imageTTFtext()). UTF-8 string, possibly with entities in.
- * @param array $splitRendering Split-rendering configuration
- * @param array $conf The configuration
- * @param integer $sF Scale factor
- * @return void
- */
- protected function renderTTFText(&$im, $fontSize, $angle, $x, $y, $color, $fontFile, $string, $splitRendering, $conf, $sF = 1) {
- if (isset($conf['breakWidth']) && $conf['breakWidth'] && $this->getRenderedTextWidth($string, $conf) > $conf['breakWidth']) {
- $phrase = '';
- $currentWidth = 0;
- $breakWidth = $conf['breakWidth'];
- $breakSpace = $this->getBreakSpace($conf);
- $wordPairs = $this->getWordPairsForLineBreak($string);
- // Iterate through all word pairs:
- foreach ($wordPairs as $index => $wordPair) {
- $wordWidth = $this->getRenderedTextWidth($wordPair, $conf);
- if ($index == 0 || $currentWidth + $wordWidth <= $breakWidth) {
- $currentWidth += $wordWidth;
- $phrase .= $wordPair;
- } else {
- // Render the current phrase that is below breakWidth:
- $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $phrase, $splitRendering, $sF);
- // Calculate the news height offset:
- $y += $breakSpace;
- // Restart the phrase:
- $currentWidth = $wordWidth;
- $phrase = $wordPair;
- }
- }
- // Render the remaining phrase:
- if ($currentWidth) {
- $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $phrase, $splitRendering, $sF);
- }
- } else {
- $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $string, $splitRendering, $sF);
- }
- }
- /**
- * Gets the word pairs used for automatic line breaks.
- *
- * @param string $string
- * @return array
- */
- protected function getWordPairsForLineBreak($string) {
- $wordPairs = array();
- $wordsArray = preg_split('#([- .,!:]+)#', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
- $wordsCount = count($wordsArray);
- for ($index = 0; $index < $wordsCount; $index += 2) {
- $wordPairs[] = $wordsArray[$index] . $wordsArray[$index + 1];
- }
- return $wordPairs;
- }
- /**
- * Gets the rendered text width.
- *
- * @param string $text
- * @param array $conf
- * @param integer
- */
- protected function getRenderedTextWidth($text, $conf) {
- $bounds = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $this->recodeString($text), $conf['splitRendering.']);
- if ($conf['angle'] < 0) {
- $pixelWidth = abs($bounds[4] - $bounds[0]);
- } elseif ($conf['angle'] > 0) {
- $pixelWidth = abs($bounds[2] - $bounds[6]);
- } else {
- $pixelWidth = abs($bounds[4] - $bounds[6]);
- }
- return $pixelWidth;
- }
- /**
- * Gets the break space for each new line.
- *
- * @param array $conf TypoScript configuration for the currently rendered object
- * @param array $boundingBox The bounding box the the currently rendered object
- * @return integer The break space
- */
- protected function getBreakSpace($conf, array $boundingBox = NULL) {
- if (!isset($boundingBox)) {
- $boundingBox = $this->calcBBox($conf);
- $boundingBox = $boundingBox[2];
- }
- if (isset($conf['breakSpace']) && $conf['breakSpace']) {
- $breakSpace = $boundingBox['lineHeight'] * $conf['breakSpace'];
- } else {
- $breakSpace = $boundingBox['lineHeight'];
- }
- return $breakSpace;
- }
- /***************************…
Large files files are truncated, but you can click here to view the full file