[BUGFIX] backColor transparent causes black background or flickering
[Packages/TYPO3.CMS.git] / typo3 / sysext / cms / tslib / class.tslib_gifbuilder.php
index 8b6fafa..3aeb29c 100644 (file)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2010 Kasper Skårhøj (kasperYYYY@typo3.com)
+*  (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
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
  * Generating gif/png-files from TypoScript
  * Used by the menu-objects and imgResource in TypoScript.
  *
  * Generating gif/png-files from TypoScript
  * Used by the menu-objects and imgResource in TypoScript.
  *
- * $Id$
  * Revised for TYPO3 3.6 June/2003 by Kasper Skårhøj
  *
  * Revised for TYPO3 3.6 June/2003 by Kasper Skårhøj
  *
- * @author     Kasper Skårhøj <kasperYYYY@typo3.com>
+ * @author Kasper Skårhøj <kasperYYYY@typo3.com>
  */
  */
-/**
- * [CLASS/FUNCTION INDEX of SCRIPT]
- *
- *
- *
- *  102: class tslib_gifBuilder extends t3lib_stdGraphic
- *  129:     function start($conf,$data)
- *  315:     function gifBuild()
- *  343:     function make()
- *
- *              SECTION: Various helper functions
- *  486:     function checkTextObj($conf)
- *  566:     function calcOffset($string)
- *  615:     function getResource($file,$fileArray)
- *  632:     function checkFile($file)
- *  643:     function fileName($pre)
- *  659:     function extension()
- *
- * TOTAL FUNCTIONS: 9
- * (This index is automatically created/updated by the extension "extdeveval")
- *
- */
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 
 /**
  * GIFBUILDER extension class.
 
 /**
  * GIFBUILDER extension class.
  * $gifCreator = t3lib_div::makeInstance('tslib_gifbuilder');
  * $gifCreator->init();
  * $theImage='';
  * $gifCreator = t3lib_div::makeInstance('tslib_gifbuilder');
  * $gifCreator->init();
  * $theImage='';
- * if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib'])    {
- * $gifCreator->start($fileArray,$this->data);
+ * if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
+ * $gifCreator->start($fileArray, $this->data);
  * $theImage = $gifCreator->gifBuild();
  * }
  * return $gifCreator->getImageDimensions($theImage);
  *
  * $theImage = $gifCreator->gifBuild();
  * }
  * return $gifCreator->getImageDimensions($theImage);
  *
- * @author     Kasper Skårhøj <kasperYYYY@typo3.com>
+ * @author Kasper Skårhøj <kasperYYYY@typo3.com>
  * @package TYPO3
  * @subpackage tslib
  * @package TYPO3
  * @subpackage tslib
- * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=377&cHash=e00ac666f3
  */
 class tslib_gifBuilder extends t3lib_stdGraphic {
 
                // Internal
  */
 class tslib_gifBuilder extends t3lib_stdGraphic {
 
                // Internal
-       var $im = '';           // the main image
-       var $w = 0;                     // the image-width
-       var $h = 0;                     // the image-height
-       var $map;                       // map-data
+               // the main image
+       var $im = '';
+               // the image-width
+       var $w = 0;
+               // the image-height
+       var $h = 0;
+               // map-data
+       var $map;
        var $workArea;
        var $workArea;
-       var $setup = Array ();          // This holds the operational setup for gifbuilder. Basically this is a TypoScript array with properties.
-       var $combinedTextStrings = array();             // Contains all text strings used on this image
-       var $combinedFileNames = array();               // Contains all filenames (basename without extension) used on this image
-       var $data = Array();            // This is the array from which data->field: [key] is fetched. So this is the current record!
-       var $objBB = Array();
+               // This holds the operational setup for gifbuilder. Basically this is a TypoScript array with properties.
+       var $setup = array();
+               // Contains all text strings used on this image
+       var $combinedTextStrings = array();
+               // Contains all filenames (basename without extension) used on this image
+       var $combinedFileNames = array();
+               // This is the array from which data->field: [key] is fetched. So this is the current record!
+       var $data = array();
+       var $objBB = array();
        var $myClassName = 'gifbuilder';
        var $myClassName = 'gifbuilder';
-       var $charRangeMap=array();
+       var $charRangeMap = array();
 
        /**
         * Initialization of the GIFBUILDER objects, in particular TEXT and IMAGE. This includes finding the bounding box, setting dimensions and offset values before the actual rendering is started.
 
        /**
         * Initialization of the GIFBUILDER objects, in particular TEXT and IMAGE. This includes finding the bounding box, setting dimensions and offset values before the actual rendering is started.
@@ -121,46 +85,43 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
         * Should be called after the ->init() function which initializes the parent class functions/variables in general.
         * The class tslib_gmenu also uses gifbuilder and here there is an interesting use since the function findLargestDims() from that class calls the init() and start() functions to find the total dimensions before starting the rendering of the images.
         *
         * Should be called after the ->init() function which initializes the parent class functions/variables in general.
         * The class tslib_gmenu also uses gifbuilder and here there is an interesting use since the function findLargestDims() from that class calls the init() and start() functions to find the total dimensions before starting the rendering of the images.
         *
-        * @param       array           TypoScript properties for the GIFBUILDER session. Stored internally in the variable ->setup
-        * @param       array           The current data record from tslib_cObj. Stored internally in the variable ->data
-        * @return      void
+        * @param array $conf TypoScript properties for the GIFBUILDER session. Stored internally in the variable ->setup
+        * @param array $data The current data record from tslib_cObj. Stored internally in the variable ->data
+        * @return void
         * @see tslib_cObj::getImgResource(), tslib_gmenu::makeGifs(), tslib_gmenu::findLargestDims()
         */
         * @see tslib_cObj::getImgResource(), tslib_gmenu::makeGifs(), tslib_gmenu::findLargestDims()
         */
-       function start($conf,$data)     {
+       function start($conf, $data) {
 
 
-               if (is_array($conf))    {
+               if (is_array($conf)) {
                        $this->setup = $conf;
                        $this->data = $data;
                        $this->cObj =t3lib_div::makeInstance('tslib_cObj');
                        $this->cObj->start($this->data);
 
                        $this->setup = $conf;
                        $this->data = $data;
                        $this->cObj =t3lib_div::makeInstance('tslib_cObj');
                        $this->cObj->start($this->data);
 
-
-                       /* Hook preprocess gifbuilder conf
-                        * Added by Julle for 3.8.0
-                        *
-                        * Let's you pre-process the gifbuilder configuration. for
-                        * example you can split a string up into lines and render each
-                        * line as TEXT obj, see extension julle_gifbconf
-                        */
-
+                       // Hook preprocess gifbuilder conf
+                       // Added by Julle for 3.8.0
+                       //
+                       // Let's you pre-process the gifbuilder configuration. for
+                       // example you can split a string up into lines and render each
+                       // line as TEXT obj, see extension julle_gifbconf
                        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_gifbuilder.php']['gifbuilder-ConfPreProcess']))    {
                                foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_gifbuilder.php']['gifbuilder-ConfPreProcess'] as $_funcRef)    {
                                        $_params = $this->setup;
                        if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_gifbuilder.php']['gifbuilder-ConfPreProcess']))    {
                                foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_gifbuilder.php']['gifbuilder-ConfPreProcess'] as $_funcRef)    {
                                        $_params = $this->setup;
-                                       $this->setup = t3lib_div::callUserFunction($_funcRef,$_params,$this);
+                                       $this->setup = t3lib_div::callUserFunction($_funcRef, $_params, $this);
                                }
                        }
 
                                // Initializing global Char Range Map
                        $this->charRangeMap = array();
                                }
                        }
 
                                // Initializing global Char Range Map
                        $this->charRangeMap = array();
-                       if (is_array($GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.']))   {
-                               foreach($GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'] as $cRMcfgkey => $cRMcfg)        {
-                                       if (is_array($cRMcfg))  {
+                       if (is_array($GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'])) {
+                               foreach($GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'] as $cRMcfgkey => $cRMcfg) {
+                                       if (is_array($cRMcfg)) {
 
                                                        // Initializing:
 
                                                        // Initializing:
-                                               $cRMkey = $GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'][substr($cRMcfgkey,0,-1)];
+                                               $cRMkey = $GLOBALS['TSFE']->tmpl->setup['_GIFBUILDER.']['charRangeMap.'][substr($cRMcfgkey, 0, -1)];
                                                $this->charRangeMap[$cRMkey] = array();
                                                $this->charRangeMap[$cRMkey]['charMapConfig'] =  $cRMcfg['charMapConfig.'];
                                                $this->charRangeMap[$cRMkey] = array();
                                                $this->charRangeMap[$cRMkey]['charMapConfig'] =  $cRMcfg['charMapConfig.'];
-                                               $this->charRangeMap[$cRMkey]['cfgKey'] = substr($cRMcfgkey,0,-1);
+                                               $this->charRangeMap[$cRMkey]['cfgKey'] = substr($cRMcfgkey, 0, -1);
                                                $this->charRangeMap[$cRMkey]['multiplicator'] = (double)$cRMcfg['fontSizeMultiplicator'];
                                                $this->charRangeMap[$cRMkey]['pixelSpace'] = intval($cRMcfg['pixelSpaceFontSizeRef']);
                                        }
                                                $this->charRangeMap[$cRMkey]['multiplicator'] = (double)$cRMcfg['fontSizeMultiplicator'];
                                                $this->charRangeMap[$cRMkey]['pixelSpace'] = intval($cRMcfg['pixelSpaceFontSizeRef']);
                                        }
@@ -171,30 +132,25 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
                        $sKeyArray=t3lib_TStemplate::sortedKeyList($this->setup);
 
                                // Setting the background color, passing it through stdWrap
                        $sKeyArray=t3lib_TStemplate::sortedKeyList($this->setup);
 
                                // Setting the background color, passing it through stdWrap
-                       if ($conf['backColor.'] || $conf['backColor'])  {
+                       if ($conf['backColor.'] || $conf['backColor']) {
                                $this->setup['backColor'] = isset($this->setup['backColor.'])
                                        ? trim($this->cObj->stdWrap($this->setup['backColor'], $this->setup['backColor.']))
                                        : $this->setup['backColor'];
                        }
                        if (!$this->setup['backColor']) { $this->setup['backColor']='white'; }
 
                                $this->setup['backColor'] = isset($this->setup['backColor.'])
                                        ? trim($this->cObj->stdWrap($this->setup['backColor'], $this->setup['backColor.']))
                                        : $this->setup['backColor'];
                        }
                        if (!$this->setup['backColor']) { $this->setup['backColor']='white'; }
 
-                       if ($conf['transparentColor.'] || $conf['transparentColor'])    {
+                       if ($conf['transparentColor.'] || $conf['transparentColor']) {
                                $this->setup['transparentColor_array'] = isset($this->setup['transparentColor.'])
                                        ? explode('|', trim($this->cObj->stdWrap($this->setup['transparentColor'], $this->setup['transparentColor.'])))
                                        : explode('|', trim($this->setup['transparentColor']));
                        }
 
                                $this->setup['transparentColor_array'] = isset($this->setup['transparentColor.'])
                                        ? explode('|', trim($this->cObj->stdWrap($this->setup['transparentColor'], $this->setup['transparentColor.'])))
                                        : explode('|', trim($this->setup['transparentColor']));
                        }
 
-                               // Transparency does not properly work when, GIFs or 8-bit PNGs are generated or reduceColors is set -- disable truecolor flag so they get generated "natively" in 8-bit.
-                               // not working with reduceColors and truecolor images
-                       if(isset($this->setup['transparentBackground.'])) {
-                               $this->setup['transparentBackground'] = $this->cOjb->stdWrap($this->setup['transparentBackground'], $this->setup['transparentBackground.']);
+                       if (isset($this->setup['transparentBackground.'])) {
+                               $this->setup['transparentBackground'] = $this->cObj->stdWrap($this->setup['transparentBackground'], $this->setup['transparentBackground.']);
                        }
                        }
-                       if(isset($this->setup['reduceColors.'])) {
-                               $this->setup['reduceColors'] = $this->cOjb->stdWrap($this->setup['reduceColors'], $this->setup['reduceColors.']);
+                       if (isset($this->setup['reduceColors.'])) {
+                               $this->setup['reduceColors'] = $this->cObj->stdWrap($this->setup['reduceColors'], $this->setup['reduceColors.']);
                        }
                        }
-                       if (($this->setup['transparentBackground'] || is_array($this->setup['transparentColor_array'])) && ($this->gifExtension=='gif' || !$this->png_truecolor || $this->setup['reduceColors']))       {
-                               $this->truecolor = false;
-                       }
 
                                // Set default dimensions
                        if (isset($this->setup['XY.'])) {
 
                                // Set default dimensions
                        if (isset($this->setup['XY.'])) {
@@ -202,23 +158,22 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
                        }
                        if (!$this->setup['XY'])        {$this->setup['XY']='120,50';}
 
                        }
                        if (!$this->setup['XY'])        {$this->setup['XY']='120,50';}
 
-
                                // Checking TEXT and IMAGE objects for files. If any errors the objects are cleared.
                                // The Bounding Box for the objects is stored in an array
                        foreach($sKeyArray as $theKey) {
                                $theValue = $this->setup[$theKey];
 
                                // Checking TEXT and IMAGE objects for files. If any errors the objects are cleared.
                                // The Bounding Box for the objects is stored in an array
                        foreach($sKeyArray as $theKey) {
                                $theValue = $this->setup[$theKey];
 
-                               if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
+                               if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
                                                // Swipes through TEXT and IMAGE-objects
                                                // Swipes through TEXT and IMAGE-objects
-                                       switch($theValue)       {
+                                       switch($theValue) {
                                                case 'TEXT':
                                                case 'TEXT':
-                                                       if ($this->setup[$theKey.'.'] = $this->checkTextObj($conf))     {
+                                                       if ($this->setup[$theKey.'.'] = $this->checkTextObj($conf)) {
 
                                                                        // Adjust font width if max size is set:
                                                                $maxWidth = isset($this->setup[$theKey.'.']['maxWidth.'])
                                                                        ? $this->cObj->stdWrap($this->setup[$theKey.'.']['maxWidth'], $this->setup[$theKey.'.']['maxWidth.'])
                                                                        : $this->setup[$theKey.'.']['maxWidth'];
 
                                                                        // Adjust font width if max size is set:
                                                                $maxWidth = isset($this->setup[$theKey.'.']['maxWidth.'])
                                                                        ? $this->cObj->stdWrap($this->setup[$theKey.'.']['maxWidth'], $this->setup[$theKey.'.']['maxWidth.'])
                                                                        : $this->setup[$theKey.'.']['maxWidth'];
-                                                               if ($maxWidth)  {
+                                                               if ($maxWidth) {
                                                                        $this->setup[$theKey.'.']['fontSize'] = $this->fontResize($this->setup[$theKey.'.']); //RTF - this has to be done before calcBBox
                                                                }
 
                                                                        $this->setup[$theKey.'.']['fontSize'] = $this->fontResize($this->setup[$theKey.'.']); //RTF - this has to be done before calcBBox
                                                                }
 
@@ -230,15 +185,15 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
                                                        }
                                                break;
                                                case 'IMAGE':
                                                        }
                                                break;
                                                case 'IMAGE':
-                                                       $fileInfo = $this->getResource($conf['file'],$conf['file.']);
-                                                       if ($fileInfo)  {
-                                                               $this->combinedFileNames[] = preg_replace('/\.[[:alnum:]]+$/','',basename($fileInfo[3]));
+                                                       $fileInfo = $this->getResource($conf['file'], $conf['file.']);
+                                                       if ($fileInfo) {
+                                                               $this->combinedFileNames[] = preg_replace('/\.[[:alnum:]]+$/', '', basename($fileInfo[3]));
                                                                $this->setup[$theKey.'.']['file'] = $fileInfo[3];
                                                                $this->setup[$theKey.'.']['BBOX'] = $fileInfo;
                                                                $this->objBB[$theKey] = $fileInfo;
                                                                $this->setup[$theKey.'.']['file'] = $fileInfo[3];
                                                                $this->setup[$theKey.'.']['BBOX'] = $fileInfo;
                                                                $this->objBB[$theKey] = $fileInfo;
-                                                               if ($conf['mask'])      {
-                                                                       $maskInfo = $this->getResource($conf['mask'],$conf['mask.']);
-                                                                       if ($maskInfo)  {
+                                                               if ($conf['mask']) {
+                                                                       $maskInfo = $this->getResource($conf['mask'], $conf['mask.']);
+                                                                       if ($maskInfo) {
                                                                                $this->setup[$theKey.'.']['mask'] = $maskInfo[3];
                                                                        } else {
                                                                                $this->setup[$theKey.'.']['mask'] = '';
                                                                                $this->setup[$theKey.'.']['mask'] = $maskInfo[3];
                                                                        } else {
                                                                                $this->setup[$theKey.'.']['mask'] = '';
@@ -250,11 +205,11 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
                                                break;
                                        }
                                                // Checks if disabled is set... (this is also done in menu.php / imgmenu!!)
                                                break;
                                        }
                                                // Checks if disabled is set... (this is also done in menu.php / imgmenu!!)
-                                       if ($conf['if.'])       {
+                                       if ($conf['if.']) {
                                                $cObj =t3lib_div::makeInstance('tslib_cObj');
                                                $cObj->start($this->data);
 
                                                $cObj =t3lib_div::makeInstance('tslib_cObj');
                                                $cObj->start($this->data);
 
-                                               if (!$cObj->checkIf($conf['if.']))      {
+                                               if (!$cObj->checkIf($conf['if.'])) {
                                                        unset($this->setup[$theKey]);
                                                        unset($this->setup[$theKey.'.']);
                                                }
                                                        unset($this->setup[$theKey]);
                                                        unset($this->setup[$theKey.'.']);
                                                }
@@ -265,12 +220,12 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
                                // Calculate offsets on elements
                        $this->setup['XY'] = $this->calcOffset($this->setup['XY']);
 
                                // Calculate offsets on elements
                        $this->setup['XY'] = $this->calcOffset($this->setup['XY']);
 
-                       if(isset($this->setup['offset.'])) {
+                       if (isset($this->setup['offset.'])) {
                                $this->setup['offset'] = $this->cObj->stdWrap($this->setup['offset'], $this->setup['offset.']);
                        }
                        $this->setup['offset'] = $this->calcOffset($this->setup['offset']);
 
                                $this->setup['offset'] = $this->cObj->stdWrap($this->setup['offset'], $this->setup['offset.']);
                        }
                        $this->setup['offset'] = $this->calcOffset($this->setup['offset']);
 
-                       if(isset($this->setup['workArea.'])) {
+                       if (isset($this->setup['workArea.'])) {
                                $this->setup['workArea'] = $this->cObj->stdWrap($this->setup['workArea'], $this->setup['workArea.']);
                        }
                        $this->setup['workArea'] = $this->calcOffset($this->setup['workArea']);
                                $this->setup['workArea'] = $this->cObj->stdWrap($this->setup['workArea'], $this->setup['workArea.']);
                        }
                        $this->setup['workArea'] = $this->calcOffset($this->setup['workArea']);
@@ -278,53 +233,53 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
                        foreach ($sKeyArray as $theKey) {
                                $theValue=$this->setup[$theKey];
 
                        foreach ($sKeyArray as $theKey) {
                                $theValue=$this->setup[$theKey];
 
-                               if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
-                                       switch($theValue)       {
+                               if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
+                                       switch($theValue) {
                                                case 'TEXT':
                                                case 'IMAGE':
                                                case 'TEXT':
                                                case 'IMAGE':
-                                                       if(isset($this->setup[$theKey.'.']['offset.'])) {
+                                                       if (isset($this->setup[$theKey.'.']['offset.'])) {
                                                                $this->setup[$theKey.'.']['offset'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['offset'], $this->setup[$theKey.'.']['offset.']);
                                                        }
                                                                $this->setup[$theKey.'.']['offset'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['offset'], $this->setup[$theKey.'.']['offset.']);
                                                        }
-                                                       if ($this->setup[$theKey.'.']['offset'])        {
+                                                       if ($this->setup[$theKey.'.']['offset']) {
                                                                $this->setup[$theKey.'.']['offset'] = $this->calcOffset($this->setup[$theKey.'.']['offset']);
                                                        }
                                                break;
                                                case 'BOX':
                                                case 'ELLIPSE':
                                                                $this->setup[$theKey.'.']['offset'] = $this->calcOffset($this->setup[$theKey.'.']['offset']);
                                                        }
                                                break;
                                                case 'BOX':
                                                case 'ELLIPSE':
-                                                       if(isset($this->setup[$theKey.'.']['dimensions.'])) {
+                                                       if (isset($this->setup[$theKey.'.']['dimensions.'])) {
                                                                $this->setup[$theKey.'.']['dimensions'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['dimensions'], $this->setup[$theKey.'.']['dimensions.']);
                                                        }
                                                                $this->setup[$theKey.'.']['dimensions'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['dimensions'], $this->setup[$theKey.'.']['dimensions.']);
                                                        }
-                                                       if ($this->setup[$theKey.'.']['dimensions'])    {
+                                                       if ($this->setup[$theKey.'.']['dimensions']) {
                                                                $this->setup[$theKey.'.']['dimensions'] = $this->calcOffset($this->setup[$theKey.'.']['dimensions']);
                                                        }
                                                break;
                                                case 'WORKAREA':
                                                                $this->setup[$theKey.'.']['dimensions'] = $this->calcOffset($this->setup[$theKey.'.']['dimensions']);
                                                        }
                                                break;
                                                case 'WORKAREA':
-                                                       if(isset($this->setup[$theKey.'.']['set.'])) {
+                                                       if (isset($this->setup[$theKey.'.']['set.'])) {
                                                                $this->setup[$theKey.'.']['set'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['set'], $this->setup[$theKey.'.']['set.']);
                                                        }
                                                                $this->setup[$theKey.'.']['set'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['set'], $this->setup[$theKey.'.']['set.']);
                                                        }
-                                                       if ($this->setup[$theKey.'.']['set'])   {
+                                                       if ($this->setup[$theKey.'.']['set']) {
                                                                $this->setup[$theKey.'.']['set'] = $this->calcOffset($this->setup[$theKey.'.']['set']);
                                                        }
                                                break;
                                                case 'CROP':
                                                                $this->setup[$theKey.'.']['set'] = $this->calcOffset($this->setup[$theKey.'.']['set']);
                                                        }
                                                break;
                                                case 'CROP':
-                                                       if(isset($this->setup[$theKey.'.']['crop.'])) {
+                                                       if (isset($this->setup[$theKey.'.']['crop.'])) {
                                                                $this->setup[$theKey.'.']['crop'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['crop'], $this->setup[$theKey.'.']['crop.']);
                                                        }
                                                                $this->setup[$theKey.'.']['crop'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['crop'], $this->setup[$theKey.'.']['crop.']);
                                                        }
-                                                       if ($this->setup[$theKey.'.']['crop'])  {
+                                                       if ($this->setup[$theKey.'.']['crop']) {
                                                                $this->setup[$theKey.'.']['crop'] = $this->calcOffset($this->setup[$theKey.'.']['crop']);
                                                        }
                                                break;
                                                case 'SCALE':
                                                                $this->setup[$theKey.'.']['crop'] = $this->calcOffset($this->setup[$theKey.'.']['crop']);
                                                        }
                                                break;
                                                case 'SCALE':
-                                                       if(isset($this->setup[$theKey.'.']['width.'])) {
+                                                       if (isset($this->setup[$theKey.'.']['width.'])) {
                                                                $this->setup[$theKey.'.']['width'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['width'], $this->setup[$theKey.'.']['width.']);
                                                        }
                                                                $this->setup[$theKey.'.']['width'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['width'], $this->setup[$theKey.'.']['width.']);
                                                        }
-                                                       if ($this->setup[$theKey.'.']['width']) {
+                                                       if ($this->setup[$theKey.'.']['width']) {
                                                                $this->setup[$theKey.'.']['width'] = $this->calcOffset($this->setup[$theKey.'.']['width']);
                                                        }
                                                                $this->setup[$theKey.'.']['width'] = $this->calcOffset($this->setup[$theKey.'.']['width']);
                                                        }
-                                                       if(isset($this->setup[$theKey.'.']['height.'])) {
+                                                       if (isset($this->setup[$theKey.'.']['height.'])) {
                                                                $this->setup[$theKey.'.']['height'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['height'], $this->setup[$theKey.'.']['height.']);
                                                        }
                                                                $this->setup[$theKey.'.']['height'] = $this->cObj->stdWrap($this->setup[$theKey.'.']['height'], $this->setup[$theKey.'.']['height.']);
                                                        }
-                                                       if ($this->setup[$theKey.'.']['height'])        {
+                                                       if ($this->setup[$theKey.'.']['height']) {
                                                                $this->setup[$theKey.'.']['height'] = $this->calcOffset($this->setup[$theKey.'.']['height']);
                                                        }
                                                break;
                                                                $this->setup[$theKey.'.']['height'] = $this->calcOffset($this->setup[$theKey.'.']['height']);
                                                        }
                                                break;
@@ -332,7 +287,7 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
                                }
                        }
                                // Get trivial data
                                }
                        }
                                // Get trivial data
-                       $XY = t3lib_div::intExplode(',',$this->setup['XY']);
+                       $XY = t3lib_div::intExplode(',', $this->setup['XY']);
                        $maxWidth = isset($this->setup['maxWidth.'])
                                ? intval($this->cObj->stdWrap($this->setup['maxWidth'], $this->setup['maxWidth.']))
                                : intval($this->setup['maxWidth']);
                        $maxWidth = isset($this->setup['maxWidth.'])
                                ? intval($this->cObj->stdWrap($this->setup['maxWidth'], $this->setup['maxWidth.']))
                                : intval($this->setup['maxWidth']);
@@ -340,30 +295,33 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
                                ? intval($this->cObj->stdWrap($this->setup['maxHeight'], $this->setup['maxHeight.']))
                                : intval($this->setup['maxHeight']);
 
                                ? intval($this->cObj->stdWrap($this->setup['maxHeight'], $this->setup['maxHeight.']))
                                : intval($this->setup['maxHeight']);
 
-                       $XY[0] = t3lib_div::intInRange($XY[0],1, $maxWidth?$maxWidth:2000);
-                       $XY[1] = t3lib_div::intInRange($XY[1],1, $maxHeight?$maxHeight:2000);
+                       $XY[0] = t3lib_utility_Math::forceIntegerInRange($XY[0], 1, $maxWidth?$maxWidth:2000);
+                       $XY[1] = t3lib_utility_Math::forceIntegerInRange($XY[1], 1, $maxHeight?$maxHeight:2000);
                        $this->XY = $XY;
                        $this->w = $XY[0];
                        $this->h = $XY[1];
                        $this->XY = $XY;
                        $this->w = $XY[0];
                        $this->h = $XY[1];
-                       $this->OFFSET = t3lib_div::intExplode(',',$this->setup['offset']);
-
-                       $this->setWorkArea($this->setup['workArea']);   // this sets the workArea
-                       $this->defaultWorkArea = $this->workArea;       // this sets the default to the current;
+                       $this->OFFSET = t3lib_div::intExplode(',', $this->setup['offset']);
+                               // this sets the workArea
+                       $this->setWorkArea($this->setup['workArea']);
+                               // this sets the default to the current;
+                       $this->defaultWorkArea = $this->workArea;
                }
        }
 
        /**
                }
        }
 
        /**
-        * Initiates the image file generation if ->setup is true and if the file did not exist already.
+        * Initiates the image file generation if ->setup is TRUE and if the file did not exist already.
         * Gets filename from fileName() and if file exists in typo3temp/ dir it will - of course - not be rendered again.
         * Otherwise rendering means calling ->make(), then ->output(), then ->destroy()
         *
         * Gets filename from fileName() and if file exists in typo3temp/ dir it will - of course - not be rendered again.
         * Otherwise rendering means calling ->make(), then ->output(), then ->destroy()
         *
-        * @return      string          The filename for the created GIF/PNG file. The filename will be prefixed "GB_"
+        * @return string The filename for the created GIF/PNG file. The filename will be prefixed "GB_"
         * @see make(), fileName()
         */
         * @see make(), fileName()
         */
-       function gifBuild()     {
-               if ($this->setup)       {
-                       $gifFileName = $this->fileName('GB/');  // Relative to PATH_site
-                       if (!file_exists($gifFileName)) {               // File exists
+       function gifBuild() {
+               if ($this->setup) {
+                               // Relative to PATH_site
+                       $gifFileName = $this->fileName('GB/');
+                               // File exists
+                       if (!file_exists($gifFileName)) {
 
                                        // Create temporary directory if not done:
                                $this->createTempSubDir('GB/');
 
                                        // Create temporary directory if not done:
                                $this->createTempSubDir('GB/');
@@ -383,178 +341,192 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
         * Creates a GDlib resource in $this->im and works on that
         * Called by gifBuild()
         *
         * Creates a GDlib resource in $this->im and works on that
         * Called by gifBuild()
         *
-        * @return      void
+        * @return void
         * @access private
         * @see gifBuild()
         * @access private
         * @see gifBuild()
-        * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=378&cHash=3c2ae4a1ab
         */
         */
-       function make() {
+       function make() {
                        // Get trivial data
                $XY = $this->XY;
 
                        // Get trivial data
                $XY = $this->XY;
 
+                       // Reset internal properties
+               $this->saveAlphaLayer = FALSE;
+
                        // Gif-start
                $this->im = imagecreatetruecolor($XY[0], $XY[1]);
                $this->w = $XY[0];
                $this->h = $XY[1];
 
                        // Gif-start
                $this->im = imagecreatetruecolor($XY[0], $XY[1]);
                $this->w = $XY[0];
                $this->h = $XY[1];
 
-                       // backColor is set
-               $BGcols = $this->convertColor($this->setup['backColor']);
-               $Bcolor = ImageColorAllocate($this->im, $BGcols[0],$BGcols[1],$BGcols[2]);
-               ImageFilledRectangle($this->im, 0, 0, $XY[0], $XY[1], $Bcolor);
+                       // Transparent layer as background if set and requirements are met
+               if (!empty($this->setup['backColor'])
+                       && $this->setup['backColor'] === 'transparent'
+                       && $this->png_truecolor
+                       && !$this->setup['reduceColors']
+                       && (empty($this->setup['format']) || $this->setup['format'] === 'png')) {
+
+                               // Set transparency properties
+                       imagesavealpha($this->im, TRUE);
+
+                               // Fill with a transparent background
+                       $transparentColor = imagecolorallocatealpha($this->im, 0, 0, 0, 127);
+                       imagefill($this->im, 0, 0, $transparentColor);
+
+                               // Set internal properties to keep the transparency over the rendering process
+                       $this->saveAlphaLayer = TRUE;
+                               // Force PNG in case no format is set
+                       $this->setup['format'] = 'png';
+               } else {
+
+                               // Fill the background with the given color
+                       $BGcols = $this->convertColor($this->setup['backColor']);
+                       $Bcolor = ImageColorAllocate($this->im, $BGcols[0], $BGcols[1], $BGcols[2]);
+                       ImageFilledRectangle($this->im, 0, 0, $XY[0], $XY[1], $Bcolor);
+               }
 
                        // Traverse the GIFBUILDER objects an render each one:
 
                        // Traverse the GIFBUILDER objects an render each one:
-               if (is_array($this->setup))     {
+               if (is_array($this->setup)) {
                        $sKeyArray=t3lib_TStemplate::sortedKeyList($this->setup);
                        $sKeyArray=t3lib_TStemplate::sortedKeyList($this->setup);
-                       foreach($sKeyArray as $theKey)  {
+                       foreach($sKeyArray as $theKey) {
                                $theValue=$this->setup[$theKey];
                                $theValue=$this->setup[$theKey];
-                               if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
+                               if (intval($theKey) && $conf=$this->setup[$theKey.'.']) {
                                        $isStdWrapped = array();
                                        foreach($conf as $key => $value) {
                                        $isStdWrapped = array();
                                        foreach($conf as $key => $value) {
-                                               $parameter = rtrim($key,'.');
-                                               if(!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
+                                               $parameter = rtrim($key, '.');
+                                               if (!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
                                                        $conf[$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter.'.']);
                                                        $isStdWrapped[$parameter] = 1;
                                                }
                                        }
                                                        $conf[$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter.'.']);
                                                        $isStdWrapped[$parameter] = 1;
                                                }
                                        }
-                                       switch($theValue)       {
+                                       switch($theValue) {
                                                        // Images
                                                case 'IMAGE':
                                                        // Images
                                                case 'IMAGE':
-                                                       if ($conf['mask'])      {
-                                                               $this->maskImageOntoImage($this->im,$conf,$this->workArea);
+                                                       if ($conf['mask']) {
+                                                               $this->maskImageOntoImage($this->im, $conf, $this->workArea);
                                                        } else {
                                                        } else {
-                                                               $this->copyImageOntoImage($this->im,$conf,$this->workArea);
+                                                               $this->copyImageOntoImage($this->im, $conf, $this->workArea);
                                                        }
                                                break;
 
                                                        // Text
                                                case 'TEXT':
                                                        }
                                                break;
 
                                                        // Text
                                                case 'TEXT':
-                                                       if (!$conf['hide'])     {
-                                                               if (is_array($conf['shadow.'])) {
+                                                       if (!$conf['hide']) {
+                                                               if (is_array($conf['shadow.'])) {
                                                                        $isStdWrapped = array();
                                                                        foreach($conf['shadow.'] as $key => $value) {
                                                                        $isStdWrapped = array();
                                                                        foreach($conf['shadow.'] as $key => $value) {
-                                                                               $parameter = rtrim($key,'.');
-                                                                               if(!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
+                                                                               $parameter = rtrim($key, '.');
+                                                                               if (!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
                                                                                        $conf['shadow.'][$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter.'.']);
                                                                                        $isStdWrapped[$parameter] = 1;
                                                                                }
                                                                        }
                                                                                        $conf['shadow.'][$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter.'.']);
                                                                                        $isStdWrapped[$parameter] = 1;
                                                                                }
                                                                        }
-                                                                       $this->makeShadow($this->im,$conf['shadow.'],$this->workArea,$conf);
+                                                                       $this->makeShadow($this->im, $conf['shadow.'], $this->workArea, $conf);
                                                                }
                                                                }
-                                                               if (is_array($conf['emboss.'])) {
+                                                               if (is_array($conf['emboss.'])) {
                                                                        $isStdWrapped = array();
                                                                        foreach($conf['emboss.'] as $key => $value) {
                                                                        $isStdWrapped = array();
                                                                        foreach($conf['emboss.'] as $key => $value) {
-                                                                               $parameter = rtrim($key,'.');
-                                                                               if(!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
+                                                                               $parameter = rtrim($key, '.');
+                                                                               if (!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
                                                                                        $conf['emboss.'][$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter.'.']);
                                                                                        $isStdWrapped[$parameter] = 1;
                                                                                }
                                                                        }
                                                                                        $conf['emboss.'][$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter.'.']);
                                                                                        $isStdWrapped[$parameter] = 1;
                                                                                }
                                                                        }
-                                                                       $this->makeEmboss($this->im,$conf['emboss.'],$this->workArea,$conf);
+                                                                       $this->makeEmboss($this->im, $conf['emboss.'], $this->workArea, $conf);
                                                                }
                                                                }
-                                                               if (is_array($conf['outline.']))        {
+                                                               if (is_array($conf['outline.'])) {
                                                                        $isStdWrapped = array();
                                                                        foreach($conf['outline.'] as $key => $value) {
                                                                        $isStdWrapped = array();
                                                                        foreach($conf['outline.'] as $key => $value) {
-                                                                               $parameter = rtrim($key,'.');
-                                                                               if(!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
+                                                                               $parameter = rtrim($key, '.');
+                                                                               if (!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
                                                                                        $conf['outline.'][$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter.'.']);
                                                                                        $isStdWrapped[$parameter] = 1;
                                                                                }
                                                                        }
                                                                                        $conf['outline.'][$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter.'.']);
                                                                                        $isStdWrapped[$parameter] = 1;
                                                                                }
                                                                        }
-                                                                       $this->makeOutline($this->im,$conf['outline.'],$this->workArea,$conf);
+                                                                       $this->makeOutline($this->im, $conf['outline.'], $this->workArea, $conf);
                                                                }
                                                                $conf['imgMap']=1;
                                                                }
                                                                $conf['imgMap']=1;
-                                                               $this->makeText($this->im,$conf,$this->workArea);
+                                                               $this->makeText($this->im, $conf, $this->workArea);
                                                        }
                                                break;
 
                                                        // Text effects:
                                                case 'OUTLINE':
                                                        }
                                                break;
 
                                                        // Text effects:
                                                case 'OUTLINE':
-                                                       if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.']))   {
-                                                               $this->makeOutline($this->im,$conf,$this->workArea,$txtConf);
+                                                       if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.'])) {
+                                                               $this->makeOutline($this->im, $conf, $this->workArea, $txtConf);
                                                        }
                                                break;
                                                case 'EMBOSS':
                                                        }
                                                break;
                                                case 'EMBOSS':
-                                                       if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.']))   {
-                                                               $this->makeEmboss($this->im,$conf,$this->workArea,$txtConf);
+                                                       if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.'])) {
+                                                               $this->makeEmboss($this->im, $conf, $this->workArea, $txtConf);
                                                        }
                                                break;
                                                case 'SHADOW':
                                                        }
                                                break;
                                                case 'SHADOW':
-                                                       if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.']))   {
-                                                               $this->makeShadow($this->im,$conf,$this->workArea,$txtConf);
+                                                       if ($this->setup[$conf['textObjNum']]=='TEXT' && $txtConf=$this->checkTextObj($this->setup[$conf['textObjNum'].'.'])) {
+                                                               $this->makeShadow($this->im, $conf, $this->workArea, $txtConf);
                                                        }
                                                break;
 
                                                        // Other
                                                case 'BOX':
                                                        }
                                                break;
 
                                                        // Other
                                                case 'BOX':
-                                                       $this->makeBox($this->im,$conf,$this->workArea);
+                                                       $this->makeBox($this->im, $conf, $this->workArea);
                                                break;
                                                case 'EFFECT':
                                                break;
                                                case 'EFFECT':
-                                                       $this->makeEffect($this->im,$conf);
+                                                       $this->makeEffect($this->im, $conf);
                                                break;
                                                case 'ADJUST':
                                                break;
                                                case 'ADJUST':
-                                                       $this->adjust($this->im,$conf);
+                                                       $this->adjust($this->im, $conf);
                                                break;
                                                case 'CROP':
                                                break;
                                                case 'CROP':
-                                                       $this->crop($this->im,$conf);
+                                                       $this->crop($this->im, $conf);
                                                break;
                                                case 'SCALE':
                                                break;
                                                case 'SCALE':
-                                                       $this->scale($this->im,$conf);
+                                                       $this->scale($this->im, $conf);
                                                break;
                                                case 'WORKAREA':
                                                break;
                                                case 'WORKAREA':
-                                                       if ($conf['set'])       {
-                                                               $this->setWorkArea($conf['set']);       // this sets the workArea
+                                                       if ($conf['set']) {
+                                                                       // this sets the workArea
+                                                               $this->setWorkArea($conf['set']);
                                                        }
                                                        }
-                                                       if (isset($conf['clear']))      {
-                                                               $this->workArea = $this->defaultWorkArea;       // this sets the current to the default;
+                                                       if (isset($conf['clear'])) {
+                                                                       // This sets the current to the default;
+                                                               $this->workArea = $this->defaultWorkArea;
                                                        }
                                                break;
                                                case 'ELLIPSE':
                                                        $this->makeEllipse($this->im, $conf, $this->workArea);
                                                break;
                                                        }
                                                break;
                                                case 'ELLIPSE':
                                                        $this->makeEllipse($this->im, $conf, $this->workArea);
                                                break;
-                                       }                                       
+                                       }
                                }
                        }
                }
 
                                }
                        }
                }
 
-
-               if ($this->setup['transparentBackground'])      {
-                               // Auto transparent background is set
-                       $Bcolor = ImageColorClosest($this->im, $BGcols[0], $BGcols[1], $BGcols[2]);
-                       imagecolortransparent($this->im, $Bcolor);
-               } elseif (is_array($this->setup['transparentColor_array']))     {
-                               // Multiple transparent colors are set. This is done via the trick that all transparent colors get converted to one color and then this one gets set as transparent as png/gif can just have one transparent color.
-                       $Tcolor = $this->unifyColors($this->im, $this->setup['transparentColor_array'], intval($this->setup['transparentColor.']['closest']));
-                       if ($Tcolor>=0) {
-                               imagecolortransparent($this->im, $Tcolor);
+                       // Preserve alpha transparency
+               if (!$this->saveAlphaLayer) {
+                       if ($this->setup['transparentBackground']) {
+                                       // Auto transparent background is set
+                               $Bcolor = ImageColorClosest($this->im, $BGcols[0], $BGcols[1], $BGcols[2]);
+                               imagecolortransparent($this->im, $Bcolor);
+                       } elseif (is_array($this->setup['transparentColor_array'])) {
+                                       // Multiple transparent colors are set. This is done via the trick that all transparent colors get
+                                       // converted to one color and then this one gets set as transparent as png/gif can just have one
+                                       // transparent color.
+                               $Tcolor = $this->unifyColors(
+                                       $this->im,
+                                       $this->setup['transparentColor_array'],
+                                       intval($this->setup['transparentColor.']['closest'])
+                               );
+                               if ($Tcolor >= 0) {
+                                       imagecolortransparent($this->im, $Tcolor);
+                               }
                        }
                }
                        }
                }
-
        }
 
        }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
        /*********************************************
         *
         * Various helper functions
         *
         ********************************************/
 
        /*********************************************
         *
         * Various helper functions
         *
         ********************************************/
 
-
        /**
         * Initializing/Cleaning of TypoScript properties for TEXT GIFBUILDER objects
         *
        /**
         * Initializing/Cleaning of TypoScript properties for TEXT GIFBUILDER objects
         *
@@ -562,33 +534,41 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
         * Finds the title if its a 'variable' (instantiates a cObj and loads it with the ->data record)
         * Performs caseshift if any.
         *
         * Finds the title if its a 'variable' (instantiates a cObj and loads it with the ->data record)
         * Performs caseshift if any.
         *
-        * @param       array           GIFBUILDER object TypoScript properties
-        * @return      array           Modified $conf array IF the "text" property is not blank
+        * @param array $conf GIFBUILDER object TypoScript properties
+        * @return array Modified $conf array IF the "text" property is not blank
         * @access private
         */
         * @access private
         */
-       function checkTextObj($conf)    {
+       function checkTextObj($conf) {
+               $cObj = t3lib_div::makeInstance('tslib_cObj');
+               $cObj->start($this->data);
+
                $isStdWrapped = array();
                foreach($conf as $key => $value) {
                $isStdWrapped = array();
                foreach($conf as $key => $value) {
-                       $parameter = rtrim($key,'.');
-                       if(!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
-                               $conf[$parameter] = $this->cObj->stdWrap($conf[$parameter], $conf[$parameter.'.']);
+                       $parameter = rtrim($key, '.');
+                       if (!$isStdWrapped[$parameter] && isset($conf[$parameter.'.'])) {
+                               $conf[$parameter] = $cObj->stdWrap($conf[$parameter], $conf[$parameter . '.']);
                                $isStdWrapped[$parameter] = 1;
                        }
                }
                $conf['fontFile']=$this->checkFile($conf['fontFile']);
                                $isStdWrapped[$parameter] = 1;
                        }
                }
                $conf['fontFile']=$this->checkFile($conf['fontFile']);
-               if (!$conf['fontFile']){$conf['fontFile']='t3lib/fonts/nimbus.ttf';}
-               if (!$conf['iterations']){$conf['iterations'] = 1;}
-               if (!$conf['fontSize']){$conf['fontSize']=12;}
-               if ($conf['spacing'] || $conf['wordSpacing'])   {               // If any kind of spacing applys, we cannot use angles!!
-                       $conf['angle']=0;
+               if (!$conf['fontFile']) {
+                       $conf['fontFile'] = 't3lib/fonts/nimbus.ttf';
+               }
+               if (!$conf['iterations']) {
+                       $conf['iterations'] = 1;
+               }
+               if (!$conf['fontSize']) {
+                       $conf['fontSize'] = 12;
+               }
+                       // If any kind of spacing applys, we cannot use angles!!
+               if ($conf['spacing'] || $conf['wordSpacing']) {
+                       $conf['angle'] = 0;
                }
                if (!isset($conf['antiAlias'])){$conf['antiAlias']=1;}
                }
                if (!isset($conf['antiAlias'])){$conf['antiAlias']=1;}
-               $cObj =t3lib_div::makeInstance('tslib_cObj');
-               $cObj->start($this->data);
 
                $conf['fontColor'] = trim($conf['fontColor']);
                        // Strip HTML
 
                $conf['fontColor'] = trim($conf['fontColor']);
                        // Strip HTML
-               if (!$conf['doNotStripHTML'])   {
+               if (!$conf['doNotStripHTML']) {
                        $conf['text'] = strip_tags($conf['text']);
                }
                $this->combinedTextStrings[] = strip_tags($conf['text']);
                        $conf['text'] = strip_tags($conf['text']);
                }
                $this->combinedTextStrings[] = strip_tags($conf['text']);
@@ -596,44 +576,49 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
                        // Max length = 100 if automatic line braks are not defined:
                if (!isset($conf['breakWidth']) || !$conf['breakWidth']) {
                        $tlen = (intval($conf['textMaxLength']) ? intval($conf['textMaxLength']) : 100);
                        // Max length = 100 if automatic line braks are not defined:
                if (!isset($conf['breakWidth']) || !$conf['breakWidth']) {
                        $tlen = (intval($conf['textMaxLength']) ? intval($conf['textMaxLength']) : 100);
-                       $conf['text'] = substr($conf['text'], 0, $tlen);
+                       if ($this->nativeCharset) {
+                               $conf['text'] = $this->csConvObj->substr($this->nativeCharset, $conf['text'], 0, $tlen);
+                       } else {
+                               $conf['text'] = substr($conf['text'], 0, $tlen);
+                       }
                }
                }
-               if ((string)$conf['text']!='')  {
+               if ((string)$conf['text']!='') {
 
                                // Char range map thingie:
                        $fontBaseName = basename($conf['fontFile']);
 
                                // Char range map thingie:
                        $fontBaseName = basename($conf['fontFile']);
-                       if (is_array($this->charRangeMap[$fontBaseName]))       {
+                       if (is_array($this->charRangeMap[$fontBaseName])) {
 
                                        // Initialize splitRendering array:
 
                                        // Initialize splitRendering array:
-                               if (!is_array($conf['splitRendering.']))        {
+                               if (!is_array($conf['splitRendering.'])) {
                                        $conf['splitRendering.'] = array();
                                }
 
                                $cfgK = $this->charRangeMap[$fontBaseName]['cfgKey'];
                                        $conf['splitRendering.'] = array();
                                }
 
                                $cfgK = $this->charRangeMap[$fontBaseName]['cfgKey'];
-                               if (!isset($conf['splitRendering.'][$cfgK]))    {       // Do not impose settings if a splitRendering object already exists:
+                                       // Do not impose settings if a splitRendering object already exists:
+                               if (!isset($conf['splitRendering.'][$cfgK])) {
                                                // Set configuration:
                                        $conf['splitRendering.'][$cfgK] = 'charRange';
                                        $conf['splitRendering.'][$cfgK.'.'] = $this->charRangeMap[$fontBaseName]['charMapConfig'];
 
                                                // Set configuration:
                                        $conf['splitRendering.'][$cfgK] = 'charRange';
                                        $conf['splitRendering.'][$cfgK.'.'] = $this->charRangeMap[$fontBaseName]['charMapConfig'];
 
-                                               // multiplicator of fontsize:
-                                       if ($this->charRangeMap[$fontBaseName]['multiplicator'])        {
+                                               // Multiplicator of fontsize:
+                                       if ($this->charRangeMap[$fontBaseName]['multiplicator']) {
                                                $conf['splitRendering.'][$cfgK.'.']['fontSize'] = round($conf['fontSize'] * $this->charRangeMap[$fontBaseName]['multiplicator']);
                                        }
                                                $conf['splitRendering.'][$cfgK.'.']['fontSize'] = round($conf['fontSize'] * $this->charRangeMap[$fontBaseName]['multiplicator']);
                                        }
-                                               // multiplicator of pixelSpace:
-                                       if ($this->charRangeMap[$fontBaseName]['pixelSpace'])   {
-                                               $travKeys = array('xSpaceBefore','xSpaceAfter','ySpaceBefore','ySpaceAfter');
-                                               foreach($travKeys as $pxKey)    {
-                                                       if (isset($conf['splitRendering.'][$cfgK.'.'][$pxKey])) {
+                                               // Multiplicator of pixelSpace:
+                                       if ($this->charRangeMap[$fontBaseName]['pixelSpace']) {
+                                               $travKeys = array('xSpaceBefore', 'xSpaceAfter', 'ySpaceBefore', 'ySpaceAfter');
+                                               foreach($travKeys as $pxKey) {
+                                                       if (isset($conf['splitRendering.'][$cfgK.'.'][$pxKey])) {
                                                                $conf['splitRendering.'][$cfgK.'.'][$pxKey] = round($conf['splitRendering.'][$cfgK.'.'][$pxKey] * ($conf['fontSize'] / $this->charRangeMap[$fontBaseName]['pixelSpace']));
                                                        }
                                                }
                                        }
                                }
                        }
                                                                $conf['splitRendering.'][$cfgK.'.'][$pxKey] = round($conf['splitRendering.'][$cfgK.'.'][$pxKey] * ($conf['fontSize'] / $this->charRangeMap[$fontBaseName]['pixelSpace']));
                                                        }
                                                }
                                        }
                                }
                        }
-                       if (is_array($conf['splitRendering.'])) {
-                               foreach($conf['splitRendering.'] as $key => $value)     {
-                                       if (is_array($conf['splitRendering.'][$key]))   {
-                                               if (isset($conf['splitRendering.'][$key]['fontFile']))  {
+                       if (is_array($conf['splitRendering.'])) {
+                               foreach($conf['splitRendering.'] as $key => $value) {
+                                       if (is_array($conf['splitRendering.'][$key])) {
+                                               if (isset($conf['splitRendering.'][$key]['fontFile'])) {
                                                        $conf['splitRendering.'][$key]['fontFile'] = $this->checkFile($conf['splitRendering.'][$key]['fontFile']);
                                                }
                                        }
                                                        $conf['splitRendering.'][$key]['fontFile'] = $this->checkFile($conf['splitRendering.'][$key]['fontFile']);
                                                }
                                        }
@@ -651,11 +636,11 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
         * Input: 2+2, 2*3, 123, [10.w]
         * Output: 4,6,123,45  (provided that the width of object in position 10 was 45 pixels wide)
         *
         * Input: 2+2, 2*3, 123, [10.w]
         * Output: 4,6,123,45  (provided that the width of object in position 10 was 45 pixels wide)
         *
-        * @param       string          The string to resolve/calculate the result of. The string is divided by a comma first and each resulting part is calculated into an integer.
-        * @return      string          The resolved string with each part (separated by comma) returned separated by comma
+        * @param string $string The string to resolve/calculate the result of. The string is divided by a comma first and each resulting part is calculated into an integer.
+        * @return string The resolved string with each part (separated by comma) returned separated by comma
         * @access private
         */
         * @access private
         */
-       function calcOffset($string)    {
+       function calcOffset($string) {
                $value = array();
                $numbers = t3lib_div::trimExplode(',', $this->calculateFunctions($string));
 
                $value = array();
                $numbers = t3lib_div::trimExplode(',', $this->calculateFunctions($string));
 
@@ -674,69 +659,80 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
        /**
         * Returns an "imgResource" creating an instance of the tslib_cObj class and calling tslib_cObj::getImgResource
         *
        /**
         * Returns an "imgResource" creating an instance of the tslib_cObj class and calling tslib_cObj::getImgResource
         *
-        * @param       string          Filename value OR the string "GIFBUILDER", see documentation in TSref for the "datatype" called "imgResource"
-        * @param       array           TypoScript properties passed to the function. Either GIFBUILDER properties or imgResource properties, depending on the value of $file (whether that is "GIFBUILDER" or a file reference)
-        * @return      array           Returns an array with file information if an image was returned. Otherwise false.
+        * @param string $file Filename value OR the string "GIFBUILDER", see documentation in TSref for the "datatype" called "imgResource"
+        * @param array $fileArray TypoScript properties passed to the function. Either GIFBUILDER properties or imgResource properties, depending on the value of $file (whether that is "GIFBUILDER" or a file reference)
+        * @return array Returns an array with file information if an image was returned. Otherwise FALSE.
         * @access private
         * @see tslib_cObj::getImgResource()
         * @access private
         * @see tslib_cObj::getImgResource()
-        * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=315&cHash=63b593a934
-        * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=282&cHash=831a95115d
         */
         */
-       function getResource($file,$fileArray)  {
-               if (!t3lib_div::inList($this->imageFileExt, $fileArray['ext'])) {
+       function getResource($file, $fileArray) {
+               if (!t3lib_div::inList($this->imageFileExt, $fileArray['ext'])) {
                        $fileArray['ext'] = $this->gifExtension;
                }
                $cObj =t3lib_div::makeInstance('tslib_cObj');
                $cObj->start($this->data);
                        $fileArray['ext'] = $this->gifExtension;
                }
                $cObj =t3lib_div::makeInstance('tslib_cObj');
                $cObj->start($this->data);
-               return $cObj->getImgResource($file,$fileArray);
+               return $cObj->getImgResource($file, $fileArray);
        }
 
        /**
         * Returns the reference to a "resource" in TypoScript.
         *
        }
 
        /**
         * Returns the reference to a "resource" in TypoScript.
         *
-        * @param       string          The resource value.
-        * @return      string          Returns the relative filepath
+        * @param string $file The resource value.
+        * @return string Returns the relative filepath
         * @access private
         * @see t3lib_TStemplate::getFileName()
         */
         * @access private
         * @see t3lib_TStemplate::getFileName()
         */
-       function checkFile($file)       {
+       function checkFile($file) {
                return $GLOBALS['TSFE']->tmpl->getFileName($file);
        }
 
        /**
         * Calculates the GIFBUILDER output filename/path based on a serialized, hashed value of this->setup
         *
                return $GLOBALS['TSFE']->tmpl->getFileName($file);
        }
 
        /**
         * Calculates the GIFBUILDER output filename/path based on a serialized, hashed value of this->setup
         *
-        * @param       string          Filename prefix, eg. "GB_"
-        * @return      string          The relative filepath (relative to PATH_site)
+        * @param string $pre Filename prefix, eg. "GB_"
+        * @return string The relative filepath (relative to PATH_site)
         * @access private
         */
         * @access private
         */
-       function fileName($pre) {
+       function fileName($pre) {
 
                $meaningfulPrefix = '';
 
                if ($GLOBALS['TSFE']->config['config']['meaningfulTempFilePrefix']) {
 
                $meaningfulPrefix = '';
 
                if ($GLOBALS['TSFE']->config['config']['meaningfulTempFilePrefix']) {
+                       /** @var $basicFileFunctions t3lib_basicFileFunctions */
+                       $basicFileFunctions = t3lib_div::makeInstance('t3lib_basicFileFunctions');
+
                        $meaningfulPrefix = implode('_', array_merge($this->combinedTextStrings, $this->combinedFileNames));
                        $meaningfulPrefix = implode('_', array_merge($this->combinedTextStrings, $this->combinedFileNames));
-                               // strip everything non-ascii
-                       $meaningfulPrefix = preg_replace('/[^A-Za-z0-9_-]/', '', trim($meaningfulPrefix));
-                       $meaningfulPrefix = substr($meaningfulPrefix, 0, intval($GLOBALS['TSFE']->config['config']['meaningfulTempFilePrefix'])) . '_';
+                       $meaningfulPrefix = $basicFileFunctions->cleanFileName($meaningfulPrefix);
+                       $meaningfulPrefixLength = intval($GLOBALS['TSFE']->config['config']['meaningfulTempFilePrefix']);
+                       if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['UTF8filesystem']) {
+                               /** @var $t3libCsInstance t3lib_cs */
+                               $t3libCsInstance = t3lib_div::makeInstance('t3lib_cs');
+                               $meaningfulPrefix = $t3libCsInstance->substr('utf-8', $meaningfulPrefix, 0, $meaningfulPrefixLength);
+                       } else {
+                               $meaningfulPrefix = substr($meaningfulPrefix, 0, $meaningfulPrefixLength);
+                       }
+                       $meaningfulPrefix .= '_';
                }
 
                }
 
-                       // WARNING: In PHP5 I discovered that rendering with freetype of Japanese letters was totally corrupt. Not only the wrong glyphs are printed but also some memory stack overflow resulted in strange additional chars - and finally the reason for this investigation: The Bounding box data was changing all the time resulting in new images being generated all the time. With PHP4 it works fine.
-               return $this->tempPath.
-                               $pre.
+                       // WARNING: In PHP5 I discovered that rendering with freetype of Japanese letters was totally corrupt.
+                       // Not only the wrong glyphs are printed but also some memory stack overflow resulted in strange additional
+                       // chars - and finally the reason for this investigation: The Bounding box data was changing all the time
+                       // resulting in new images being generated all the time. With PHP4 it works fine.
+               return $this->tempPath .
+                               $pre .
                                $meaningfulPrefix .
                                $meaningfulPrefix .
-                               t3lib_div::shortMD5(serialize($this->setup)).
-                               '.'.$this->extension();
+                               t3lib_div::shortMD5(serialize($this->setup)) .
+                               '.' . $this->extension();
        }
 
        /**
         * Returns the file extension used in the filename
         *
        }
 
        /**
         * Returns the file extension used in the filename
         *
-        * @return      string          Extension; "jpg" or "gif"/"png"
+        * @return string Extension; "jpg" or "gif"/"png"
         * @access private
         */
        function extension() {
         * @access private
         */
        function extension() {
-               switch(strtolower($this->setup['format']))      {
+               switch(strtolower($this->setup['format'])) {
                        case 'jpg':
                        case 'jpeg':
                                return 'jpg';
                        case 'jpg':
                        case 'jpeg':
                                return 'jpg';
@@ -756,9 +752,9 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
        /**
         * Calculates the value concerning the dimensions of objects.
         *
        /**
         * Calculates the value concerning the dimensions of objects.
         *
-        * @param       string          $string: The string to be calculated (e.g. "[20.h]+13")
-        * @return      integer         The calculated value (e.g. "23")
-        * @see         calcOffset()
+        * @param string $string The string to be calculated (e.g. "[20.h]+13")
+        * @return integer The calculated value (e.g. "23")
+        * @see calcOffset()
         */
        protected function calculateValue($string) {
                $calculatedValue = 0;
         */
        protected function calculateValue($string) {
                $calculatedValue = 0;
@@ -809,8 +805,8 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
         * Calculates special functions:
         * + max([10.h], [20.h])        -> gets the maximum of the given values
         *
         * Calculates special functions:
         * + max([10.h], [20.h])        -> gets the maximum of the given values
         *
-        * @param       string          $string: The raw string with functions to be calculated
-        * @return      string          The calculated values
+        * @param string $string The raw string with functions to be calculated
+        * @return string The calculated values
         */
        protected function calculateFunctions($string) {
                if (preg_match_all('#max\(([^)]+)\)#', $string, $matches)) {
         */
        protected function calculateFunctions($string) {
                if (preg_match_all('#max\(([^)]+)\)#', $string, $matches)) {
@@ -831,19 +827,13 @@ class tslib_gifBuilder extends t3lib_stdGraphic {
        /**
         * Calculates the maximum of a set of values defined like "[10.h],[20.h],1000"
         *
        /**
         * Calculates the maximum of a set of values defined like "[10.h],[20.h],1000"
         *
-        * @param       string          $string: The string to be used to calculate the maximum (e.g. "[10.h],[20.h],1000")
-        * @return      integer         The maxium value of the given comma separated and calculated values
+        * @param string $string The string to be used to calculate the maximum (e.g. "[10.h],[20.h],1000")
+        * @return integer The maxium value of the given comma separated and calculated values
         */
        protected function calculateMaximum($string) {
         */
        protected function calculateMaximum($string) {
-               $parts = t3lib_div::trimExplode(',', $this->calcOffset($string), true);
+               $parts = t3lib_div::trimExplode(',', $this->calcOffset($string), TRUE);
                $maximum = (count($parts) ? max($parts) : 0);
                return $maximum;
        }
 }
                $maximum = (count($parts) ? max($parts) : 0);
                return $maximum;
        }
 }
-
-
-if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_gifbuilder.php'])       {
-       include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_gifbuilder.php']);
-}
-
 ?>
 ?>