PageRenderTime 56ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 1ms

/t3lib/class.t3lib_stdgraphic.php

https://bitbucket.org/linxpinx/mercurial
PHP | 3105 lines | 1744 code | 415 blank | 946 comment | 373 complexity | 1ce937f61531f425d226a191d233a3ab MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, Unlicense, LGPL-2.1, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 1999-2010 Kasper Skaarhoj (kasperYYYY@typo3.com)
  6. * All rights reserved
  7. *
  8. * This script is part of the TYPO3 project. The TYPO3 project is
  9. * free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * The GNU General Public License can be found at
  15. * http://www.gnu.org/copyleft/gpl.html.
  16. * A copy is found in the textfile GPL.txt and important notices to the license
  17. * from the author is found in LICENSE.txt distributed with these scripts.
  18. *
  19. *
  20. * This script is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * This copyright notice MUST APPEAR in all copies of the script!
  26. ***************************************************************/
  27. /**
  28. * Standard graphical functions
  29. *
  30. * $Id: class.t3lib_stdgraphic.php 7939 2010-06-16 19:33:38Z psychomieze $
  31. * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
  32. *
  33. * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
  34. */
  35. /**
  36. * [CLASS/FUNCTION INDEX of SCRIPT]
  37. *
  38. *
  39. *
  40. * 155: class t3lib_stdGraphic
  41. * 236: function init()
  42. *
  43. * SECTION: Layering images / "IMAGE" GIFBUILDER object
  44. * 366: function maskImageOntoImage(&$im,$conf,$workArea)
  45. * 436: function copyImageOntoImage(&$im,$conf,$workArea)
  46. * 458: function copyGifOntoGif(&$im,$cpImg,$conf,$workArea)
  47. * 537: function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h)
  48. *
  49. * SECTION: Text / "TEXT" GIFBUILDER object
  50. * 587: function makeText(&$im,$conf,$workArea)
  51. * 707: function txtPosition($conf,$workArea,$BB)
  52. * 761: function calcBBox($conf)
  53. * 820: function addToMap($cords,$conf)
  54. * 843: function calcTextCordsForMap($cords,$offset, $conf)
  55. * 878: function SpacedImageTTFText(&$im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $text, $spacing, $wordSpacing, $splitRenderingConf, $sF=1)
  56. * 915: function fontResize($conf)
  57. * 958: function ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $string, $splitRendering, $sF=1)
  58. * 1005: function ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $string, $splitRendering,$sF=1)
  59. * 1058: function splitString($string,$splitRendering,$fontSize,$fontFile)
  60. * 1208: function calcWordSpacing($conf, $scaleFactor=1)
  61. * 1227: function getTextScalFactor($conf)
  62. *
  63. * SECTION: Other GIFBUILDER objects related to TEXT
  64. * 1262: function makeOutline(&$im,$conf,$workArea,$txtConf)
  65. * 1291: function circleOffset($distance, $iterations)
  66. * 1315: function makeEmboss(&$im,$conf,$workArea,$txtConf)
  67. * 1337: function makeShadow(&$im,$conf,$workArea,$txtConf)
  68. *
  69. * SECTION: Other GIFBUILDER objects
  70. * 1469: function makeBox(&$im,$conf,$workArea)
  71. * 1491: function makeEffect(&$im, $conf)
  72. * 1506: function IMparams($setup)
  73. * 1589: function adjust(&$im, $conf)
  74. * 1621: function crop(&$im,$conf)
  75. * 1652: function scale(&$im,$conf)
  76. * 1684: function setWorkArea($workArea)
  77. *
  78. * SECTION: Adjustment functions
  79. * 1725: function autolevels(&$im)
  80. * 1756: function outputLevels(&$im,$low,$high,$swap='')
  81. * 1788: function inputLevels(&$im,$low,$high,$swap='')
  82. * 1819: function reduceColors(&$im,$limit, $cols)
  83. * 1832: function IMreduceColors($file, $cols)
  84. *
  85. * SECTION: GIFBUILDER Helper functions
  86. * 1875: function prependAbsolutePath($fontFile)
  87. * 1889: function v5_sharpen($factor)
  88. * 1908: function v5_blur($factor)
  89. * 1925: function randomName()
  90. * 1938: function applyOffset($cords,$OFFSET)
  91. * 1951: function convertColor($string)
  92. * 2001: function recodeString($string)
  93. * 2023: function singleChars($theText,$returnUnicodeNumber=FALSE)
  94. * 2046: function objPosition($conf,$workArea,$BB)
  95. *
  96. * SECTION: Scaling, Dimensions of images
  97. * 2125: function imageMagickConvert($imagefile,$newExt='',$w='',$h='',$params='',$frame='',$options='',$mustCreate=0)
  98. * 2238: function getImageDimensions($imageFile)
  99. * 2266: function cacheImageDimensions($identifyResult)
  100. * 2298: function getCachedImageDimensions($imageFile)
  101. * 2332: function getImageScale($info,$w,$h,$options)
  102. * 2438: function file_exists_typo3temp_file($output,$orig='')
  103. *
  104. * SECTION: ImageMagick API functions
  105. * 2499: function imageMagickIdentify($imagefile)
  106. * 2534: function imageMagickExec($input,$output,$params)
  107. * 2557: function combineExec($input,$overlay,$mask,$output, $handleNegation = false)
  108. * 2588: function wrapFileName($inputName)
  109. *
  110. * SECTION: Various IO functions
  111. * 2629: function checkFile($file)
  112. * 2643: function createTempSubDir($dirName)
  113. * 2665: function applyImageMagickToPHPGif(&$im, $command)
  114. * 2691: function gif_or_jpg($type,$w,$h)
  115. * 2708: function output($file)
  116. * 2748: function destroy()
  117. * 2758: function imgTag ($imgInfo)
  118. * 2770: function ImageWrite($destImg, $theImage)
  119. * 2808: function imageGif($destImg, $theImage)
  120. * 2820: function imageCreateFromGif($sourceImg)
  121. * 2831: function imageCreateFromFile($sourceImg)
  122. * 2870: function imagecreate($w, $h)
  123. * 2885: function hexColor($col)
  124. * 2903: function unifyColors(&$img, $colArr, $closest = false)
  125. *
  126. * TOTAL FUNCTIONS: 66
  127. * (This index is automatically created/updated by the extension "extdeveval")
  128. *
  129. */
  130. /**
  131. * Class contains a bunch of cool functions for manipulating graphics with GDlib/Freetype and ImageMagick
  132. * VERY OFTEN used with gifbuilder that extends this class and provides a TypoScript API to using these functions
  133. *
  134. * With TYPO3 4.4 GDlib 1.x support was dropped, also an option from config_default.php:
  135. * $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.
  136. *
  137. * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
  138. * @package TYPO3
  139. * @subpackage t3lib
  140. * @see tslib_gifBuilder
  141. */
  142. class t3lib_stdGraphic {
  143. // Internal configuration, set in init()
  144. var $combineScript = 'combine'; // The ImageMagick filename used for combining two images. This name changed during the versions.
  145. var $noFramePrepended=0; // If set, there is no frame pointer prepended to the filenames.
  146. var $GD2 = 1; // Set, if the GDlib used is version 2. @deprecated as of TYPO3 4.4, as this variables is now always set (GDlib2 always has this method, and PHP recommends to only use imagecreatetruecolor() over imagecreate())
  147. var $imagecopyresized_fix=0; // If set, imagecopyresized will not be called directly. For GD2 (some PHP installs?)
  148. var $gifExtension = 'gif'; // This should be changed to 'png' if you want this class to read/make PNG-files instead!
  149. var $gdlibExtensions = ''; // File formats supported by gdlib. This variable get's filled in "init" method
  150. var $truecolor = TRUE; // Internal variable which get's used to determine wheter GDlib should use function truecolor pendants, @deprecated as of TYPO3 4.4, as this variables is now always set (GDlib2 always has this method, and PHP recommends to only use imagecreatetruecolor() over imagecreate())
  151. var $png_truecolor = false; // Set to true if generated png's should be truecolor by default
  152. var $truecolorColors = 0xffffff; // 16777216 Colors is the maximum value for PNG, JPEG truecolor images (24-bit, 8-bit / Channel)
  153. var $TTFLocaleConv = ''; // Used to recode input to TTF-functions for other charsets. Deprecated since TYPO3 3.6, will be removed in TYPO3 4.6
  154. var $enable_typo3temp_db_tracking = 0; // 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.
  155. var $imageFileExt = 'gif,jpg,jpeg,png,tif,bmp,tga,pcx,ai,pdf'; // 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!
  156. var $webImageExt = 'gif,jpg,jpeg,png'; // Commalist of web image extensions (can be shown by a webbrowser)
  157. var $maskNegate = ''; // Will be ' -negate' if ImageMagick ver 5.2+. See init();
  158. var $NO_IM_EFFECTS = '';
  159. var $cmds = array(
  160. 'jpg' => '',
  161. 'jpeg' => '',
  162. 'gif' => '',
  163. 'png' => '-colors 64'
  164. );
  165. var $NO_IMAGE_MAGICK = '';
  166. var $V5_EFFECTS = 0;
  167. var $im_version_4 = 0;
  168. var $mayScaleUp = 1;
  169. // Variables for testing, alternative usage etc.
  170. var $filenamePrefix=''; // Filename prefix for images scaled in imageMagickConvert()
  171. var $imageMagickConvert_forceFileNameBody=''; // Forcing the output filename of imageMagickConvert() to this value. However after calling imageMagickConvert() it will be set blank again.
  172. var $dontCheckForExistingTempFile = 0; // This flag should always be false. If set true, imageMagickConvert will always write a new file to the tempdir! Used for debugging.
  173. var $dontCompress=0; // Prevents imageMagickConvert() from compressing the gif-files with t3lib_div::gif_compress()
  174. var $dontUnlinkTempFiles=0; // For debugging ONLY!
  175. var $alternativeOutputKey=''; // 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...
  176. // Internal:
  177. var $IM_commands = Array(); // All ImageMagick commands executed is stored in this array for tracking. Used by the Install Tools Image section
  178. var $workArea = Array();
  179. // Constants:
  180. var $tempPath = 'typo3temp/'; // 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.
  181. var $absPrefix = ''; // Prefix for relative paths. Used in "show_item.php" script. Is prefixed the output file name IN imageMagickConvert()
  182. var $scalecmd = '-geometry'; // ImageMagick scaling command; "-geometry" eller "-sample". Used in makeText() and imageMagickConvert()
  183. var $im5fx_blurSteps='1x2,2x2,3x2,4x3,5x3,5x4,6x4,7x5,8x5,9x5'; // Used by v5_blur() to simulate 10 continuous steps of blurring
  184. var $im5fx_sharpenSteps='1x2,2x2,3x2,2x3,3x3,4x3,3x4,4x4,4x5,5x5'; // Used by v5_sharpen() to simulate 10 continuous steps of sharpening.
  185. var $pixelLimitGif = 10000; // This is the limit for the number of pixels in an image before it will be rendered as JPG instead of GIF/PNG
  186. var $colMap = Array ( // Array mapping HTML color names to RGB values.
  187. 'aqua' => Array(0,255,255),
  188. 'black' => Array(0,0,0),
  189. 'blue' => Array(0,0,255),
  190. 'fuchsia' => Array(255,0,255),
  191. 'gray' => Array(128,128,128),
  192. 'green' => Array(0,128,0),
  193. 'lime' => Array(0,255,0),
  194. 'maroon' => Array(128,0,0),
  195. 'navy' => Array(0,0,128),
  196. 'olive' => Array(128,128,0),
  197. 'purple' => Array(128,0,128),
  198. 'red' => Array(255,0,0),
  199. 'silver' => Array(192,192,192),
  200. 'teal' => Array(0,128,128),
  201. 'yellow' => Array(255,255,0),
  202. 'white' => Array(255,255,255)
  203. );
  204. /**
  205. * Charset conversion object:
  206. *
  207. * @var t3lib_cs
  208. */
  209. var $csConvObj;
  210. var $nativeCharset=''; // Is set to the native character set of the input strings.
  211. /**
  212. * Init function. Must always call this when using the class.
  213. * This function will read the configuration information from $GLOBALS['TYPO3_CONF_VARS']['GFX'] can set some values in internal variables.
  214. *
  215. * @return void
  216. */
  217. function init() {
  218. $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
  219. if (function_exists('imagecreatefromjpeg')&&function_exists('imagejpeg')) {
  220. $this->gdlibExtensions .= ',jpg,jpeg';
  221. }
  222. if (function_exists('imagecreatefrompng')&&function_exists('imagepng')) {
  223. $this->gdlibExtensions .= ',png';
  224. }
  225. if (function_exists('imagecreatefromgif')&&function_exists('imagegif')) {
  226. $this->gdlibExtensions .= ',gif';
  227. }
  228. if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['png_truecolor']) {
  229. $this->png_truecolor = true;
  230. }
  231. if (!$gfxConf['im_version_5']) {
  232. $this->im_version_4 = true;
  233. }
  234. // When GIFBUILDER gets used in truecolor mode
  235. // No colors parameter if we generate truecolor images.
  236. if ($this->png_truecolor) {
  237. $this->cmds['png'] = '';
  238. }
  239. // Setting default JPG parameters:
  240. $this->jpegQuality = t3lib_div::intInRange($gfxConf['jpg_quality'], 10, 100, 75);
  241. $this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -sharpen 50 -quality '.$this->jpegQuality;
  242. if ($gfxConf['im_combine_filename']) $this->combineScript=$gfxConf['im_combine_filename'];
  243. if ($gfxConf['im_noFramePrepended']) $this->noFramePrepended=1;
  244. // kept for backwards compatibility, can be turned on manually through localconf.php,
  245. // but not through the installer anymore
  246. $this->imagecopyresized_fix = ($gfxConf['gdlib_2'] === 'no_imagecopyresized_fix' ? 0 : 1);
  247. if ($gfxConf['gdlib_png']) {
  248. $this->gifExtension='png';
  249. }
  250. if ($gfxConf['TTFLocaleConv']) {
  251. $this->TTFLocaleConv = $gfxConf['TTFLocaleConv'];
  252. }
  253. if ($gfxConf['enable_typo3temp_db_tracking']) {
  254. $this->enable_typo3temp_db_tracking = $gfxConf['enable_typo3temp_db_tracking'];
  255. }
  256. $this->imageFileExt = $gfxConf['imagefile_ext'];
  257. // This should be set if ImageMagick ver. 5+ is used.
  258. if ($gfxConf['im_negate_mask']) {
  259. // Boolean. Indicates if the mask images should be inverted first.
  260. // This depends of the ImageMagick version. Below ver. 5.1 this should be false.
  261. // Above ImageMagick version 5.2+ it should be true.
  262. // Just set the flag if the masks works opposite the intension!
  263. $this->maskNegate = ' -negate';
  264. }
  265. if ($gfxConf['im_no_effects']) {
  266. // Boolean. This is necessary if using ImageMagick 5+.
  267. // Approved version for using effects is version 4.2.9.
  268. // Effects in Imagemagick 5+ tends to render very slowly!!
  269. // - therefore must be disabled in order not to perform sharpen, blurring and such.
  270. $this->NO_IM_EFFECTS = 1;
  271. $this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -quality '.$this->jpegQuality;
  272. }
  273. // ... but if 'im_v5effects' is set, don't care about 'im_no_effects'
  274. if ($gfxConf['im_v5effects']) {
  275. $this->NO_IM_EFFECTS = 0;
  276. $this->V5_EFFECTS = 1;
  277. if ($gfxConf['im_v5effects']>0) {
  278. $this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -quality '.intval($gfxConf['jpg_quality']).$this->v5_sharpen(10);
  279. }
  280. }
  281. if (!$gfxConf['im']) {
  282. $this->NO_IMAGE_MAGICK = 1;
  283. }
  284. // Secures that images are not scaled up.
  285. if ($gfxConf['im_noScaleUp']) {
  286. $this->mayScaleUp=0;
  287. }
  288. if (TYPO3_MODE=='FE') {
  289. $this->csConvObj = $GLOBALS['TSFE']->csConvObj;
  290. } elseif(is_object($GLOBALS['LANG'])) { // BE assumed:
  291. $this->csConvObj = $GLOBALS['LANG']->csConvObj;
  292. } else { // The object may not exist yet, so we need to create it now. Happens in the Install Tool for example.
  293. $this->csConvObj = t3lib_div::makeInstance('t3lib_cs');
  294. }
  295. $this->nativeCharset = $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'];
  296. }
  297. /*************************************************
  298. *
  299. * Layering images / "IMAGE" GIFBUILDER object
  300. *
  301. *************************************************/
  302. /**
  303. * Implements the "IMAGE" GIFBUILDER object, when the "mask" property is true.
  304. * 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
  305. * The operation involves ImageMagick for combining.
  306. *
  307. * @param pointer GDlib image pointer
  308. * @param array TypoScript array with configuration for the GIFBUILDER object.
  309. * @param array The current working area coordinates.
  310. * @return void
  311. * @see tslib_gifBuilder::make()
  312. */
  313. function maskImageOntoImage(&$im,$conf,$workArea) {
  314. if ($conf['file'] && $conf['mask']) {
  315. $imgInf = pathinfo($conf['file']);
  316. $imgExt = strtolower($imgInf['extension']);
  317. if (!t3lib_div::inList($this->gdlibExtensions, $imgExt)) {
  318. $BBimage = $this->imageMagickConvert($conf['file'],$this->gifExtension,'','','','','');
  319. } else {
  320. $BBimage = $this->getImageDimensions($conf['file']);
  321. }
  322. $maskInf = pathinfo($conf['mask']);
  323. $maskExt = strtolower($maskInf['extension']);
  324. if (!t3lib_div::inList($this->gdlibExtensions, $maskExt)) {
  325. $BBmask = $this->imageMagickConvert($conf['mask'],$this->gifExtension,'','','','','');
  326. } else {
  327. $BBmask = $this->getImageDimensions($conf['mask']);
  328. }
  329. if ($BBimage && $BBmask) {
  330. $w = imagesx($im);
  331. $h = imagesy($im);
  332. $tmpStr = $this->randomName();
  333. $theImage = $tmpStr.'_img.'.$this->gifExtension;
  334. $theDest = $tmpStr.'_dest.'.$this->gifExtension;
  335. $theMask = $tmpStr.'_mask.'.$this->gifExtension;
  336. // prepare overlay image
  337. $cpImg = $this->imageCreateFromFile($BBimage[3]);
  338. $destImg = imagecreatetruecolor($w, $h);
  339. $Bcolor = ImageColorAllocate($destImg, 0,0,0);
  340. ImageFilledRectangle($destImg, 0, 0, $w, $h, $Bcolor);
  341. $this->copyGifOntoGif($destImg,$cpImg,$conf,$workArea);
  342. $this->ImageWrite($destImg, $theImage);
  343. imageDestroy($cpImg);
  344. imageDestroy($destImg);
  345. // prepare mask image
  346. $cpImg = $this->imageCreateFromFile($BBmask[3]);
  347. $destImg = imagecreatetruecolor($w, $h);
  348. $Bcolor = ImageColorAllocate($destImg, 0, 0, 0);
  349. ImageFilledRectangle($destImg, 0, 0, $w, $h, $Bcolor);
  350. $this->copyGifOntoGif($destImg,$cpImg,$conf,$workArea);
  351. $this->ImageWrite($destImg, $theMask);
  352. imageDestroy($cpImg);
  353. imageDestroy($destImg);
  354. // mask the images
  355. $this->ImageWrite($im, $theDest);
  356. $this->combineExec($theDest,$theImage,$theMask,$theDest, true); // Let combineExec handle maskNegation
  357. $backIm = $this->imageCreateFromFile($theDest); // The main image is loaded again...
  358. if ($backIm) { // ... and if nothing went wrong we load it onto the old one.
  359. ImageColorTransparent($backIm,-1);
  360. $im = $backIm;
  361. }
  362. // unlink files from process
  363. if (!$this->dontUnlinkTempFiles) {
  364. unlink($theDest);
  365. unlink($theImage);
  366. unlink($theMask);
  367. }
  368. }
  369. }
  370. }
  371. /**
  372. * Implements the "IMAGE" GIFBUILDER object, when the "mask" property is false (using only $conf['file'])
  373. *
  374. * @param pointer GDlib image pointer
  375. * @param array TypoScript array with configuration for the GIFBUILDER object.
  376. * @param array The current working area coordinates.
  377. * @return void
  378. * @see tslib_gifBuilder::make(), maskImageOntoImage()
  379. */
  380. function copyImageOntoImage(&$im,$conf,$workArea) {
  381. if ($conf['file']) {
  382. if (!t3lib_div::inList($this->gdlibExtensions, $conf['BBOX'][2])) {
  383. $conf['BBOX']=$this->imageMagickConvert($conf['BBOX'][3],$this->gifExtension,'','','','','');
  384. $conf['file']=$conf['BBOX'][3];
  385. }
  386. $cpImg = $this->imageCreateFromFile($conf['file']);
  387. $this->copyGifOntoGif($im,$cpImg,$conf,$workArea);
  388. imageDestroy($cpImg);
  389. }
  390. }
  391. /**
  392. * Copies two GDlib image pointers onto each other, using TypoScript configuration from $conf and the input $workArea definition.
  393. *
  394. * @param pointer GDlib image pointer, destination (bottom image)
  395. * @param pointer GDlib image pointer, source (top image)
  396. * @param array TypoScript array with the properties for the IMAGE GIFBUILDER object. Only used for the "tile" property value.
  397. * @param array Work area
  398. * @return void Works on the $im image pointer
  399. * @access private
  400. */
  401. function copyGifOntoGif(&$im,$cpImg,$conf,$workArea) {
  402. $cpW = imagesx($cpImg);
  403. $cpH = imagesy($cpImg);
  404. $tile = t3lib_div::intExplode(',',$conf['tile']);
  405. $tile[0] = t3lib_div::intInRange($tile[0],1,20);
  406. $tile[1] = t3lib_div::intInRange($tile[1],1,20);
  407. $cpOff = $this->objPosition($conf,$workArea,Array($cpW*$tile[0],$cpH*$tile[1]));
  408. for ($xt=0;$xt<$tile[0];$xt++) {
  409. $Xstart=$cpOff[0]+$cpW*$xt;
  410. if ($Xstart+$cpW > $workArea[0]) { // if this image is inside of the workArea, then go on
  411. // X:
  412. if ($Xstart < $workArea[0]) {
  413. $cpImgCutX = $workArea[0]-$Xstart;
  414. $Xstart = $workArea[0];
  415. } else {
  416. $cpImgCutX = 0;
  417. }
  418. $w = $cpW-$cpImgCutX;
  419. if ($Xstart > $workArea[0]+$workArea[2]-$w) {
  420. $w = $workArea[0]+$workArea[2]-$Xstart;
  421. }
  422. if ($Xstart < $workArea[0]+$workArea[2]) { // if this image is inside of the workArea, then go on
  423. // Y:
  424. for ($yt=0;$yt<$tile[1];$yt++) {
  425. $Ystart=$cpOff[1]+$cpH*$yt;
  426. if ($Ystart+$cpH > $workArea[1]) { // if this image is inside of the workArea, then go on
  427. if ($Ystart < $workArea[1]) {
  428. $cpImgCutY = $workArea[1]-$Ystart;
  429. $Ystart = $workArea[1];
  430. } else {
  431. $cpImgCutY = 0;
  432. }
  433. $h = $cpH-$cpImgCutY;
  434. if ($Ystart > $workArea[1]+$workArea[3]-$h) {
  435. $h = $workArea[1]+$workArea[3]-$Ystart;
  436. }
  437. if ($Ystart < $workArea[1]+$workArea[3]) { // if this image is inside of the workArea, then go on
  438. $this->imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
  439. }
  440. }
  441. } // Y:
  442. }
  443. }
  444. }
  445. }
  446. /**
  447. * Alternative function for using the similar PHP function imagecopyresized(). Used for GD2 only.
  448. *
  449. * OK, the reason for this stupid fix is the following story:
  450. * GD1.x was capable of copying two images together and combining their palettes! GD2 is apparently not.
  451. * With GD2 only the palette of the dest-image is used which mostly results in totally black images when trying to
  452. * copy a color-ful image onto the destination.
  453. * The GD2-fix is to
  454. * 1) Create a blank TRUE-COLOR image
  455. * 2) Copy the destination image onto that one
  456. * 3) Then do the actual operation; Copying the source (top image) onto that
  457. * 4) ... and return the result pointer.
  458. * 5) Reduce colors (if we do not, the result may become strange!)
  459. * It works, but the resulting images is now a true-color PNG which may be very large.
  460. * So, why not use 'imagetruecolortopalette ($im, TRUE, 256)' - well because it does NOT WORK! So simple is that.
  461. *
  462. * For parameters, see PHP function "imagecopyresized()"
  463. *
  464. * @param pointer see PHP function "imagecopyresized()"
  465. * @param pointer see PHP function "imagecopyresized()"
  466. * @param integer see PHP function "imagecopyresized()"
  467. * @param integer see PHP function "imagecopyresized()"
  468. * @param integer see PHP function "imagecopyresized()"
  469. * @param integer see PHP function "imagecopyresized()"
  470. * @param integer see PHP function "imagecopyresized()"
  471. * @param integer see PHP function "imagecopyresized()"
  472. * @param integer see PHP function "imagecopyresized()"
  473. * @param integer see PHP function "imagecopyresized()"
  474. * @return void
  475. * @access private
  476. * @see t3lib_iconWorks::imagecopyresized()
  477. */
  478. function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h) {
  479. if ($this->imagecopyresized_fix) {
  480. $im_base = imagecreatetruecolor(imagesx($im), imagesy($im)); // Make true color image
  481. imagecopyresized($im_base, $im, 0,0,0,0, imagesx($im),imagesy($im),imagesx($im),imagesy($im)); // Copy the source image onto that
  482. imagecopyresized($im_base, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h); // Then copy the $cpImg onto that (the actual operation!)
  483. $im = $im_base; // Set pointer
  484. if (!$this->truecolor) {
  485. $this->makeEffect($im, Array('value'=>'colors='.t3lib_div::intInRange($this->setup['reduceColors'], 256, $this->truecolorColors, 256))); // Reduce to "reduceColors" colors - make SURE that IM is working then!
  486. }
  487. } else {
  488. imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
  489. }
  490. }
  491. /********************************
  492. *
  493. * Text / "TEXT" GIFBUILDER object
  494. *
  495. ********************************/
  496. /**
  497. * Implements the "TEXT" GIFBUILDER object
  498. *
  499. * @param pointer GDlib image pointer
  500. * @param array TypoScript array with configuration for the GIFBUILDER object.
  501. * @param array The current working area coordinates.
  502. * @return void
  503. * @see tslib_gifBuilder::make()
  504. */
  505. function makeText(&$im,$conf,$workArea) {
  506. // Spacing
  507. list($spacing,$wordSpacing) = $this->calcWordSpacing($conf);
  508. // Position
  509. $txtPos = $this->txtPosition($conf,$workArea,$conf['BBOX']);
  510. $theText = $this->recodeString($conf['text']);
  511. if ($conf['imgMap'] && is_array($conf['imgMap.'])) {
  512. $this->addToMap($this->calcTextCordsForMap($conf['BBOX'][2],$txtPos, $conf['imgMap.']), $conf['imgMap.']);
  513. }
  514. if (!$conf['hideButCreateMap']) {
  515. // Font Color:
  516. $cols=$this->convertColor($conf['fontColor']);
  517. // NiceText is calculated
  518. if (!$conf['niceText']) {
  519. // Font Color is reserved:
  520. if (!$this->truecolor) {
  521. $reduce = t3lib_div::intInRange($this->setup['reduceColors'], 256, $this->truecolorColors, 256);
  522. $this->reduceColors($im, $reduce-49, $reduce-50); // If "reduce-49" colors (or more) are used reduce them to "reduce-50"
  523. }
  524. $Fcolor = ImageColorAllocate($im, $cols[0],$cols[1],$cols[2]);
  525. // antiAliasing is setup:
  526. $Fcolor = ($conf['antiAlias']) ? $Fcolor : -$Fcolor;
  527. for ($a=0; $a<$conf['iterations']; $a++) {
  528. if ($spacing || $wordSpacing) { // If any kind of spacing applys, we use this function:
  529. $this->SpacedImageTTFText($im, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, t3lib_stdGraphic::prependAbsolutePath($conf['fontFile']), $theText, $spacing, $wordSpacing, $conf['splitRendering.']);
  530. } else {
  531. $this->renderTTFText($im, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, $conf['fontFile'], $theText, $conf['splitRendering.'], $conf);
  532. }
  533. }
  534. } else { // NICETEXT::
  535. // options anti_aliased and iterations is NOT available when doing this!!
  536. $w = imagesx($im);
  537. $h = imagesy($im);
  538. $tmpStr = $this->randomName();
  539. $fileMenu = $tmpStr.'_menuNT.'.$this->gifExtension;
  540. $fileColor = $tmpStr.'_colorNT.'.$this->gifExtension;
  541. $fileMask = $tmpStr.'_maskNT.'.$this->gifExtension;
  542. // Scalefactor
  543. $sF = t3lib_div::intInRange($conf['niceText.']['scaleFactor'],2,5);
  544. $newW = ceil($sF*imagesx($im));
  545. $newH = ceil($sF*imagesy($im));
  546. // Make mask
  547. $maskImg = imagecreatetruecolor($newW, $newH);
  548. $Bcolor = ImageColorAllocate($maskImg, 255,255,255);
  549. ImageFilledRectangle($maskImg, 0, 0, $newW, $newH, $Bcolor);
  550. $Fcolor = ImageColorAllocate($maskImg, 0,0,0);
  551. if ($spacing || $wordSpacing) { // If any kind of spacing applys, we use this function:
  552. $this->SpacedImageTTFText($maskImg, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, t3lib_stdGraphic::prependAbsolutePath($conf['fontFile']), $theText, $spacing, $wordSpacing, $conf['splitRendering.'],$sF);
  553. } else {
  554. $this->renderTTFText($maskImg, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, $conf['fontFile'], $theText, $conf['splitRendering.'], $conf, $sF);
  555. }
  556. $this->ImageWrite($maskImg, $fileMask);
  557. ImageDestroy($maskImg);
  558. // Downscales the mask
  559. if ($this->NO_IM_EFFECTS) {
  560. if ($this->maskNegate) {
  561. $command = trim($this->scalecmd.' '.$w.'x'.$h.'!'); // Negate 2 times makes no negate...
  562. } else {
  563. $command = trim($this->scalecmd.' '.$w.'x'.$h.'! -negate');
  564. }
  565. } else {
  566. if ($this->maskNegate) {
  567. $command = trim($conf['niceText.']['before'].' '.$this->scalecmd.' '.$w.'x'.$h.'! '.$conf['niceText.']['after']);
  568. } else {
  569. $command = trim($conf['niceText.']['before'].' '.$this->scalecmd.' '.$w.'x'.$h.'! '.$conf['niceText.']['after'].' -negate');
  570. }
  571. if ($conf['niceText.']['sharpen']) {
  572. if ($this->V5_EFFECTS) {
  573. $command.=$this->v5_sharpen($conf['niceText.']['sharpen']);
  574. } else {
  575. $command.=' -sharpen '.t3lib_div::intInRange($conf['niceText.']['sharpen'],1,99);
  576. }
  577. }
  578. }
  579. $this->imageMagickExec($fileMask,$fileMask,$command);
  580. // Make the color-file
  581. $colorImg = imagecreatetruecolor($w, $h);
  582. $Ccolor = ImageColorAllocate($colorImg, $cols[0],$cols[1],$cols[2]);
  583. ImageFilledRectangle($colorImg, 0, 0, $w, $h, $Ccolor);
  584. $this->ImageWrite($colorImg, $fileColor);
  585. ImageDestroy($colorImg);
  586. // The mask is applied
  587. $this->ImageWrite($im, $fileMenu); // The main pictures is saved temporarily
  588. $this->combineExec($fileMenu,$fileColor,$fileMask, $fileMenu);
  589. $backIm = $this->imageCreateFromFile($fileMenu); // The main image is loaded again...
  590. if ($backIm) { // ... and if nothing went wrong we load it onto the old one.
  591. ImageColorTransparent($backIm,-1);
  592. $im = $backIm;
  593. }
  594. // Deleting temporary files;
  595. if (!$this->dontUnlinkTempFiles) {
  596. unlink($fileMenu);
  597. unlink($fileColor);
  598. unlink($fileMask);
  599. }
  600. }
  601. }
  602. }
  603. /**
  604. * Calculates text position for printing the text onto the image based on configuration like alignment and workarea.
  605. *
  606. * @param array TypoScript array for the TEXT GIFBUILDER object
  607. * @param array Workarea definition
  608. * @param array Bounding box information, was set in tslib_gifBuilder::start()
  609. * @return array [0]=x, [1]=y, [2]=w, [3]=h
  610. * @access private
  611. * @see makeText()
  612. */
  613. function txtPosition($conf,$workArea,$BB) {
  614. $bbox = $BB[2];
  615. $angle=intval($conf['angle'])/180*pi();
  616. $conf['angle']=0;
  617. $straightBB = $this->calcBBox($conf);
  618. // offset, align, valign, workarea
  619. $result=Array(); // [0]=x, [1]=y, [2]=w, [3]=h
  620. $result[2] = $BB[0];
  621. $result[3] = $BB[1];
  622. $w=$workArea[2];
  623. $h=$workArea[3];
  624. switch($conf['align']) {
  625. case 'right':
  626. case 'center':
  627. $factor=abs(cos($angle));
  628. $sign=(cos($angle)<0)?-1:1;
  629. $len1 = $sign*$factor*$straightBB[0];
  630. $len2= $sign*$BB[0];
  631. $result[0] = $w-ceil($len2*$factor+(1-$factor)*$len1);
  632. $factor=abs(sin($angle));
  633. $sign=(sin($angle)<0)?-1:1;
  634. $len1= $sign*$factor*$straightBB[0];
  635. $len2= $sign*$BB[1];
  636. $result[1]=ceil($len2*$factor+(1-$factor)*$len1);
  637. break;
  638. }
  639. switch($conf['align']) {
  640. case 'right':
  641. break;
  642. case 'center':
  643. $result[0] = round(($result[0])/2);
  644. $result[1] = round(($result[1])/2);
  645. break;
  646. default:
  647. $result[0]=0;
  648. $result[1]=0;
  649. break;
  650. }
  651. $result = $this->applyOffset($result,t3lib_div::intExplode(',',$conf['offset']));
  652. $result = $this->applyOffset($result,$workArea);
  653. return $result;
  654. }
  655. /**
  656. * Calculates bounding box information for the TEXT GIFBUILDER object.
  657. *
  658. * @param array TypoScript array for the TEXT GIFBUILDER object
  659. * @return array Array with three keys [0]/[1] being x/y and [2] being the bounding box array
  660. * @access private
  661. * @see txtPosition(), tslib_gifBuilder::start()
  662. */
  663. function calcBBox($conf) {
  664. $sF = $this->getTextScalFactor($conf);
  665. list($spacing,$wordSpacing) = $this->calcWordSpacing($conf, $sF);
  666. $theText = $this->recodeString($conf['text']);
  667. $charInf = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $theText, $conf['splitRendering.'],$sF);
  668. $theBBoxInfo = $charInf;
  669. if ($conf['angle']) {
  670. $xArr = Array($charInf[0],$charInf[2],$charInf[4],$charInf[6]);
  671. $yArr = Array($charInf[1],$charInf[3],$charInf[5],$charInf[7]);
  672. $x=max($xArr)-min($xArr);
  673. $y=max($yArr)-min($yArr);
  674. } else {
  675. $x = ($charInf[2]-$charInf[0]);
  676. $y = ($charInf[1]-$charInf[7]);
  677. }
  678. // Set original lineHeight (used by line breaks):
  679. $theBBoxInfo['lineHeight'] = $y;
  680. if ($spacing || $wordSpacing) { // If any kind of spacing applys, we use this function:
  681. $x=0;
  682. if (!$spacing && $wordSpacing) {
  683. $bits = explode(' ',$theText);
  684. foreach ($bits as $word) {
  685. $word.=' ';
  686. $wordInf = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $word, $conf['splitRendering.'],$sF);
  687. $wordW = ($wordInf[2]-$wordInf[0]);
  688. $x+=$wordW+$wordSpacing;
  689. }
  690. } else {
  691. $utf8Chars = $this->singleChars($theText);
  692. // For each UTF-8 char, do:
  693. foreach($utf8Chars as $char) {
  694. $charInf = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $char, $conf['splitRendering.'],$sF);
  695. $charW = ($charInf[2]-$charInf[0]);
  696. $x+=$charW+(($char==' ')?$wordSpacing:$spacing);
  697. }
  698. }
  699. } elseif (isset($conf['breakWidth']) && $conf['breakWidth'] && $this->getRenderedTextWidth($conf['text'], $conf) > $conf['breakWidth']) {
  700. $maxWidth = 0;
  701. $currentWidth = 0;
  702. $breakWidth = $conf['breakWidth'];
  703. $breakSpace = $this->getBreakSpace($conf, $theBBoxInfo);
  704. $wordPairs = $this->getWordPairsForLineBreak($conf['text']);
  705. // Iterate through all word pairs:
  706. foreach ($wordPairs as $index => $wordPair) {
  707. $wordWidth = $this->getRenderedTextWidth($wordPair, $conf);
  708. if ($index == 0 || $currentWidth + $wordWidth <= $breakWidth) {
  709. $currentWidth+= $wordWidth;
  710. } else {
  711. $maxWidth = max($maxWidth, $currentWidth);
  712. $y+= $breakSpace;
  713. // Restart:
  714. $currentWidth = $wordWidth;
  715. }
  716. }
  717. $x = max($maxWidth, $currentWidth) * $sF;
  718. }
  719. if ($sF > 1) {
  720. $x = ceil($x / $sF);
  721. $y = ceil($y / $sF);
  722. if (is_array($theBBoxInfo)) {
  723. foreach ($theBBoxInfo as &$value) {
  724. $value = ceil($value / $sF);
  725. }
  726. }
  727. }
  728. return array($x, $y, $theBBoxInfo);
  729. }
  730. /**
  731. * Adds an <area> tag to the internal variable $this->map which is used to accumulate the content for an ImageMap
  732. *
  733. * @param array Coordinates for a polygon image map as created by ->calcTextCordsForMap()
  734. * @param array Configuration for "imgMap." property of a TEXT GIFBUILDER object.
  735. * @return void
  736. * @access private
  737. * @see makeText(), calcTextCordsForMap()
  738. */
  739. function addToMap($cords,$conf) {
  740. $JS = $conf['noBlur'] ? '' : ' onfocus="blurLink(this);"';
  741. $this->map.='<area'.
  742. ' shape="poly"'.
  743. ' coords="'.implode(',',$cords).'"'.
  744. ' href="'.htmlspecialchars($conf['url']).'"'.
  745. ($conf['target'] ? ' target="'.htmlspecialchars($conf['target']).'"' : '').
  746. $JS.
  747. (strlen($conf['titleText']) ? ' title="'.htmlspecialchars($conf['titleText']).'"' : '').
  748. ' alt="'.htmlspecialchars($conf['altText']).'" />';
  749. }
  750. /**
  751. * Calculating the coordinates for a TEXT string on an image map. Used in an <area> tag
  752. *
  753. * @param array Coordinates (from BBOX array)
  754. * @param array Offset array
  755. * @param array Configuration for "imgMap." property of a TEXT GIFBUILDER object.
  756. * @return array
  757. * @access private
  758. * @see makeText(), calcTextCordsForMap()
  759. */
  760. function calcTextCordsForMap($cords,$offset, $conf) {
  761. $pars = t3lib_div::intExplode(',',$conf['explode'].',');
  762. $newCords[0] = $cords[0]+$offset[0]-$pars[0];
  763. $newCords[1] = $cords[1]+$offset[1]+$pars[1];
  764. $newCords[2] = $cords[2]+$offset[0]+$pars[0];
  765. $newCords[3] = $cords[3]+$offset[1]+$pars[1];
  766. $newCords[4] = $cords[4]+$offset[0]+$pars[0];
  767. $newCords[5] = $cords[5]+$offset[1]-$pars[1];
  768. $newCords[6] = $cords[6]+$offset[0]-$pars[0];
  769. $newCords[7] = $cords[7]+$offset[1]-$pars[1];
  770. return $newCords;
  771. }
  772. /**
  773. * Printing text onto an image like the PHP function imageTTFText does but in addition it offers options for spacing of letters and words.
  774. * Spacing is done by printing one char at a time and this means that the spacing is rather uneven and probably not very nice.
  775. * See
  776. *
  777. * @param pointer (See argument for PHP function imageTTFtext())
  778. * @param integer (See argument for PHP function imageTTFtext())
  779. * @param integer (See argument for PHP function imageTTFtext())
  780. * @param integer (See argument for PHP function imageTTFtext())
  781. * @param integer (See argument for PHP function imageTTFtext())
  782. * @param integer (See argument for PHP function imageTTFtext())
  783. * @param string (See argument for PHP function imageTTFtext())
  784. * @param string (See argument for PHP function imageTTFtext()). UTF-8 string, possibly with entities in.
  785. * @param integer The spacing of letters in pixels
  786. * @param integer The spacing of words in pixels
  787. * @param array $splitRenderingConf array
  788. * @param integer Scale factor
  789. * @return void
  790. * @access private
  791. */
  792. function SpacedImageTTFText(&$im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $text, $spacing, $wordSpacing, $splitRenderingConf, $sF=1) {
  793. $spacing*=$sF;
  794. $wordSpacing*=$sF;
  795. if (!$spacing && $wordSpacing) {
  796. $bits = explode(' ',$text);
  797. foreach ($bits as $word) {
  798. $word.=' ';
  799. $word = $word;
  800. $wordInf = $this->ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $word, $splitRenderingConf ,$sF);
  801. $wordW = ($wordInf[2]-$wordInf[0]);
  802. $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $word, $splitRenderingConf, $sF);
  803. $x+=$wordW+$wordSpacing;
  804. }
  805. } else {
  806. $utf8Chars = $this->singleChars($text);
  807. // For each UTF-8 char, do:
  808. foreach($utf8Chars as $char) {
  809. $charInf = $this->ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $char, $splitRenderingConf, $sF);
  810. $charW = ($charInf[2]-$charInf[0]);
  811. $this->ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $Fcolor, $fontFile, $char, $splitRenderingConf, $sF);
  812. $x+=$charW+(($char==' ')?$wordSpacing:$spacing);
  813. }
  814. }
  815. }
  816. /**
  817. * Function that finds the right fontsize that will render the textstring within a certain width
  818. *
  819. * @param array The TypoScript properties of the TEXT GIFBUILDER object
  820. * @return integer The new fontSize
  821. * @access private
  822. * @author Rene Fritz <r.fritz@colorcube.de>
  823. * @see tslib_gifBuilder::start()
  824. */
  825. function fontResize($conf) {
  826. // 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!!!!
  827. $maxWidth = intval($conf['maxWidth']);
  828. list($spacing,$wordSpacing) = $this->calcWordSpacing($conf);
  829. if ($maxWidth) {
  830. if ($spacing || $wordSpacing) { // If any kind of spacing applys, we use this function:
  831. return $conf['fontSize'];
  832. // ################ no calc for spacing yet !!!!!!
  833. } else {
  834. do {
  835. // determine bounding box.
  836. $bounds = $this->ImageTTFBBoxWrapper($conf['fontSize'], $conf['angle'], $conf['fontFile'], $this->recodeString($conf['text']), $conf['splitRendering.']);
  837. if ($conf['angle']< 0) {
  838. $pixelWidth = abs($bounds[4]-$bounds[0]);
  839. } elseif ($conf['angle'] > 0) {
  840. $pixelWidth = abs($bounds[2]-$bounds[6]);
  841. } else {
  842. $pixelWidth = abs($bounds[4]-$bounds[6]);
  843. }
  844. // Size is fine, exit:
  845. if ($pixelWidth <= $maxWidth) {
  846. break;
  847. } else {
  848. $conf['fontSize']--;
  849. }
  850. } while ($conf['fontSize']>1);
  851. }//if spacing
  852. }
  853. return $conf['fontSize'];
  854. }
  855. /**
  856. * Wrapper for ImageTTFBBox
  857. *
  858. * @param integer (See argument for PHP function ImageTTFBBox())
  859. * @param integer (See argument for PHP function ImageTTFBBox())
  860. * @param string (See argument for PHP function ImageTTFBBox())
  861. * @param string (See argument for PHP function ImageTTFBBox())
  862. * @param array Split-rendering configuration
  863. * @param integer Scale factor
  864. * @return array Information array.
  865. */
  866. function ImageTTFBBoxWrapper($fontSize, $angle, $fontFile, $string, $splitRendering, $sF=1) {
  867. // Initialize:
  868. $offsetInfo = array();
  869. $stringParts = $this->splitString($string,$splitRendering,$fontSize,$fontFile);
  870. // Traverse string parts:
  871. foreach($stringParts as $strCfg) {
  872. $fontFile = t3lib_stdGraphic::prependAbsolutePath($strCfg['fontFile']);
  873. if (is_readable($fontFile)) {
  874. // Calculate Bounding Box for part:
  875. $calc = ImageTTFBBox(t3lib_div::freetypeDpiComp($sF*$strCfg['fontSize']), $angle, $fontFile, $strCfg['str']);
  876. // Calculate offsets:
  877. if (!count($offsetInfo)) {
  878. $offsetInfo = $calc; // First run, just copy over.
  879. } else {
  880. $offsetInfo[2]+=$calc[2]-$calc[0]+intval($splitRendering['compX'])+intval($strCfg['xSpaceBefore'])+intval($strCfg['xSpaceAfter']);
  881. $offsetInfo[3]+=$calc[3]-$calc[1]-intval($splitRendering['compY'])-intval($strCfg['ySpaceBefore'])-intval($strCfg['ySpaceAfter']);
  882. $offsetInfo[4]+=$calc[4]-$calc[6]+intval($splitRendering['compX'])+intval($strCfg['xSpaceBefore'])+intval($strCfg['xSpaceAfter']);
  883. $offsetInfo[5]+=$calc[5]-$calc[7]-intval($splitRendering['compY'])-intval($strCfg['ySpaceBefore'])-intval($strCfg['ySpaceAfter']);
  884. }
  885. } else {
  886. debug('cannot read file: '.$fontFile, 't3lib_stdGraphic::ImageTTFBBoxWrapper()');
  887. }
  888. }
  889. return $offsetInfo;
  890. }
  891. /**
  892. * Wrapper for ImageTTFText
  893. *
  894. * @param pointer (See argument for PHP function imageTTFtext())
  895. * @param integer (See argument for PHP function imageTTFtext())
  896. * @param integer (See argument for PHP function imageTTFtext())
  897. * @param integer (See argument for PHP function imageTTFtext())
  898. * @param integer (See argument for PHP function imageTTFtext())
  899. * @param integer (See argument for PHP function imageTTFtext())
  900. * @param string (See argument for PHP function imageTTFtext())
  901. * @param string (See argument for PHP function imageTTFtext()). UTF-8 string, possibly with entities in.
  902. * @param array Split-rendering configuration
  903. * @param integer Scale factor
  904. * @return void
  905. */
  906. function ImageTTFTextWrapper($im, $fontSize, $angle, $x, $y, $color, $fontFile, $string, $splitRendering,$sF=1) {
  907. // Initialize:
  908. $stringParts = $this->splitString($string,$splitRendering,$fontSize,$fontFile);
  909. $x = ceil($sF*$x);
  910. $y = ceil($sF*$y);
  911. // Traverse string parts:
  912. foreach($stringParts as $i => $strCfg) {
  913. // Initialize:
  914. $colorIndex = $color;
  915. // Set custom color if any (only when niceText is off):
  916. if ($strCfg['color'] && $sF==1) {
  917. $cols = $this->convertColor($strCfg['color']);
  918. $colorIndex = ImageColorAllocate($im, $cols[0],$cols[1],$cols[2]);
  919. $colorIndex = $color >= 0 ? $colorIndex : -$colorIndex;
  920. }
  921. // Setting xSpaceBefore
  922. if ($i) {
  923. $x+= intval($strCfg['xSpaceBefore']);
  924. $y-= intval($strCfg['ySpaceBefore']);
  925. }
  926. $fontFile = t3lib_stdGraphic::prependAbsolutePath($strCfg['fontFile']);
  927. if (is_readable($fontFile)) {
  928. // Render part:
  929. ImageTTFText($im, t3lib_div::freetypeDpiComp($sF*$strCfg['fontSize']), $angle, $x, $y, $colorIndex, $fontFile, $strCfg['str']);
  930. // Calculate offset to apply:
  931. $wordInf = ImageTTFBBox(t3lib_div::freetypeDpiComp($sF*$strCfg['fontSize']), $angle, t3lib_stdGraphic::prependAbsolutePath($strCfg['fontFile']), $strCfg['str']);
  932. $x+= $wordInf[2]-$wordInf[0]+intval($splitRendering['compX'])+intval($strCfg['xSpaceAfter']);
  933. $y+= $wordInf[5]-$wordInf[7]-intval($splitRendering['compY'])-intval($strCfg['ySpaceAfter']);
  934. } else {
  935. debug('cannot read file: '.$fontFile, 't3lib_stdGraphic::ImageTTFTextWrapper()');
  936. }
  937. }
  938. }
  939. /**
  940. * Splitting a string for ImageTTFBBox up into an array where each part has its own configuration options.
  941. *
  942. * @param string UTF-8 string
  943. * @param array Split-rendering configuration from GIFBUILDER TEXT object.
  944. * @param integer Current fontsize
  945. * @param string Current font file
  946. * @return array Array with input string splitted according to configuration
  947. */
  948. function splitString($string,$splitRendering,$fontSize,$fontFile) {
  949. // Initialize by setting the whole string and default configuration as the first entry.
  950. $result = array();
  951. $result[] = array(
  952. 'str' => $string,
  953. 'fontSize' => $fontSize,
  954. 'fontFile' => $fontFile
  955. );
  956. // Traverse the split-rendering configuration:
  957. // Splitting will create more entries in $result with individual configurations.
  958. if (is_array($splitRendering)) {
  959. $sKeyArray = t3lib_TStemplate::sortedKeyList($splitRendering);
  960. // Traverse configured options:
  961. foreach($sKeyArray as $key) {
  962. $cfg = $splitRendering[$key.'.'];
  963. // Process each type of split rendering keyword:
  964. switch((string)$splitRendering[$key]) {
  965. case 'highlightWord':
  966. if (strlen($cfg['value'])) {
  967. $newResult = array();
  968. // Traverse the current parts of the result array:
  969. foreach($result as $part) {
  970. // Explode the string value by the word value to highlight:
  971. $explodedParts = explode($cfg['value'],$part['str']);
  972. foreach($explodedParts as $c => $expValue) {
  973. if (strlen($expValue)) {
  974. $newResult[] = array_merge($part,array('str' => $expValue));
  975. }
  976. if ($c+1 < count($explodedParts)) {
  977. $newResult[] = array(
  978. 'str' => $cfg['value'],
  979. 'fontSize' => $cfg['fontSize'] ? $cfg['fontSize'] : $part['fontSize'],
  980. 'fontFile' => $cfg['fontFile'] ? $cfg['fontFile'] : $part['fontFile'],
  981. 'color' => $cfg['color'],
  982. 'xSpaceBefore' => $cfg['xSpaceBefore'],
  983. 'xSpaceAfter' => $cfg['xSpaceAfter'],
  984. 'ySpaceBefore' => $cfg['ySpaceBefore'],
  985. 'ySpaceAfter' => $cfg['ySpaceAfter'],
  986. );
  987. }
  988. }
  989. }
  990. // Set the new result as result array:
  991. if (count($newResult)) {
  992. $result = $newResult;
  993. }
  994. }
  995. break;
  996. case 'charRange':
  997. if (strlen($cfg['value'])) {
  998. // Initialize range:
  999. $ranges = t3lib_div::trimExplode(',',$cfg['value'],1);
  1000. foreach($ranges as $i => $rangeDef) {
  1001. $ranges[$i] = t3lib_div::intExplode('-',$ranges[$i]);
  1002. if (!isset($ranges[$i][1])) $ranges[$i][1] = $ranges[$i][0];
  1003. }
  1004. $newResult = array();
  1005. // Traverse the current parts of the result array:
  1006. foreach($result as $part) {
  1007. // Initialize:
  1008. $currentState = -1;
  1009. $bankAccum = '';
  1010. // Explode the string value by the word value to highlight:
  1011. $utf8Chars = $this->singleChars($part['str']);
  1012. foreach($utf8Chars as $utfChar) {
  1013. // Find number and evaluate position:
  1014. $uNumber = $this->csConvObj->utf8CharToUnumber($utfChar);
  1015. $inRange = 0;
  1016. foreach($ranges as $rangeDef) {
  1017. if ($uNumber >= $rangeDef[0] && (!$rangeDef[1] || $uNumber <= $rangeDef[1])) {
  1018. $inRange = 1;
  1019. break;
  1020. }
  1021. }
  1022. if ($currentState==-1) $currentState = $inRange; // Initialize first char
  1023. // Switch bank:
  1024. if ($inRange != $currentState && !t3lib_div::inList('32,10,13,9',$uNumber)) {
  1025. // Set result:
  1026. if (strlen($bankAccum)) {
  1027. $newResult[] = array(
  1028. 'str' => $bankAccum,
  1029. 'fontSize' => $currentState && $cfg['fontSize'] ? $cfg['fontSize'] : $part['fontSize'],
  1030. 'fontFile' => $currentState && $cfg['fontFile'] ? $cfg['fontFile'] : $part['fontFile'],
  1031. 'color' => $currentState ? $cfg['color'] : '',
  1032. 'xSpaceBefore' => $currentState ? $cfg['xSpaceBefore'] : '',
  1033. 'xSpaceAfter' => $currentState ? $cfg['xSpaceAfter'] : '',
  1034. 'ySpaceBefore' => $currentState ? $cfg['ySpaceBefore'] : '',
  1035. 'ySpaceAfter' => $currentState ? $cfg['ySpaceAfter'] : '',
  1036. );
  1037. }
  1038. // Initialize new settings:
  1039. $currentState = $inRange;
  1040. $bankAccum = '';
  1041. }
  1042. // Add char to bank:
  1043. $bankAccum.=$utfChar;
  1044. }
  1045. // Set result for FINAL part:
  1046. if (strlen($bankAccum)) {
  1047. $newResult[] = array(
  1048. 'str' => $bankAccum,
  1049. 'fontSize' => $currentState && $cfg['fontSize'] ? $cfg['fontSize'] : $part['fontSize'],
  1050. 'fontFile' => $currentState && $cfg['fontFile'] ? $cfg['fontFile'] : $part['fontFile'],
  1051. 'color' => $currentState ? $cfg['color'] : '',
  1052. 'xSpaceBefore' => $currentState ? $cfg['xSpaceBefore'] : '',
  1053. 'xSpaceAfter' => $currentState ? $cfg['xSpaceAfter'] : '',
  1054. 'ySpaceBefore' => $currentState ? $cfg['ySpaceBefore'] : '',
  1055. 'ySpaceAfter' => $currentState ? $cfg['ySpaceAfter'] : '',
  1056. );
  1057. }
  1058. }
  1059. // Set the new result as result array:
  1060. if (count($newResult)) {
  1061. $result = $newResult;
  1062. }
  1063. }
  1064. break;
  1065. }
  1066. }
  1067. }
  1068. return $result;
  1069. }
  1070. /**
  1071. * Calculates the spacing and wordSpacing values
  1072. *
  1073. * @param array TypoScript array for the TEXT GIFBUILDER object
  1074. * @param integer TypoScript value from eg $conf['niceText.']['scaleFactor']
  1075. * @return array Array with two keys [0]/[1] being array($spacing,$wordSpacing)
  1076. * @access private
  1077. * @see calcBBox()
  1078. */
  1079. function calcWordSpacing($conf, $scaleFactor=1) {
  1080. $spacing = intval($conf['spacing']);
  1081. $wordSpacing = intval($conf['wordSpacing']);
  1082. $wordSpacing = $wordSpacing?$wordSpacing:$spacing*2;
  1083. $spacing*=$scaleFactor;
  1084. $wordSpacing*=$scaleFactor;
  1085. return array($spacing,$wordSpacing);
  1086. }
  1087. /**
  1088. * Calculates and returns the niceText.scaleFactor
  1089. *
  1090. * @param array TypoScript array for the TEXT GIFBUILDER object
  1091. * @return integer TypoScript value from eg $conf['niceText.']['scaleFactor']
  1092. * @access private
  1093. */
  1094. function getTextScalFactor($conf) {
  1095. if (!$conf['niceText']) {
  1096. $sF = 1;
  1097. } else { // NICETEXT::
  1098. $sF = t3lib_div::intInRange($conf['niceText.']['scaleFactor'],2,5);
  1099. }
  1100. return $sF;
  1101. }
  1102. /**
  1103. * Renders a regular text and takes care of a possible line break automatically.
  1104. *
  1105. * @param pointer (See argument for PHP function imageTTFtext())
  1106. * @param integer (See argument for PHP function imageTTFtext())
  1107. * @param integer (See argument for PHP function imageTTFtext())
  1108. * @param integer (See argument f…

Large files files are truncated, but you can click here to view the full file