diff -u -ru TYPO3core/t3lib/class.t3lib_stdgraphic.php TYPO3core.gifbuilder/t3lib/class.t3lib_stdgraphic.php
--- TYPO3core/t3lib/class.t3lib_stdgraphic.php	2005-07-02 19:42:31.000000000 +0200
+++ TYPO3core.gifbuilder/t3lib/class.t3lib_stdgraphic.php	2005-10-26 21:55:41.393302264 +0200
@@ -153,6 +153,9 @@
 	var $GD2=0;									// Set, if the GDlib used is version 2.
 	var $imagecopyresized_fix=0;				// If set, imagecopyresized will not be called directly. For GD2 (some PHP installs?)
 	var $gifExtension = 'gif';					// This should be changed to 'png' if you want this class to read/make PNG-files instead!
+	var $gdlibExtensions = '';			// File formats supported by gdlib. This variable get's filled in "init" method
+	var $truecolor = true;					// Set to false if we shall generate images which get reduced in color
+	var $truecolorColors = 0xffffff;			// 16777216 Colors is the maximum value for PNG, JPEG truecolor images (24-bit, 8-bit / Channel)
 	var $TTFLocaleConv = '';					// Used to recode input to TTF-functions for other charsets.
 	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.
 	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!
@@ -167,6 +170,7 @@
 	);
 	var $NO_IMAGE_MAGICK = '';
 	var $V5_EFFECTS = 0;
+	var $im_version_4 = 0;
 	var $mayScaleUp = 1;
 
 		// Variables for testing, alternative usage etc.
@@ -224,8 +228,31 @@
 	function init()	{
 		$gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
 
+		if (function_exists('imagecreatefromjpeg')&&function_exists('imagejpeg'))	{
+			$this->gdlibExtensions .= ',jpg,jpeg';
+		}
+		if (function_exists('imagecreatefrompng')&&function_exists('imagepng'))	{
+			$this->gdlibExtensions .= ',png';
+		}
+		if (function_exists('imagecreatefromgif')&&function_exists('imagegif'))	{
+			$this->gdlibExtensions .= ',gif';
+		}
+		if (!function_exists('imagecreatetruecolor'))	{
+			$this->truecolor = false;
+		}
+		if (!$gfxConf['im_version_5'])	{
+			$this->im_version_4 = true;
+		}
+
+			// When GIFBUILDER gets used in truecolor mode (GD2 required)
+		if ($this->truecolor)	{
+			$this->cmds['png'] = '';	// No colors parameter if we generate truecolor images.
+			$this->cmds['gif'] = '';	// No colors parameter if we generate truecolor images.
+		}
+
 			// Setting default JPG parameters:
-		$this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -sharpen 50 -quality '.intval($gfxConf['im_jpg_quality']);
+		$this->jpegQuality = t3lib_div::intInRange($gfxConf['jpg_quality'], 10, 100, 75);
+		$this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -sharpen 50 -quality '.$this->jpegQuality;
 
 		if ($gfxConf['im_combine_filename'])	$this->combineScript=$gfxConf['im_combine_filename'];
 		if ($gfxConf['im_noFramePrepended'])	$this->noFramePrepended=1;
@@ -261,7 +288,7 @@
 				// - therefore must be disabled in order not to perform sharpen, blurring and such.
 			$this->NO_IM_EFFECTS = 1;
 
-			$this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -quality '.intval($gfxConf['im_jpg_quality']);
+			$this->cmds['jpg'] = $this->cmds['jpeg'] = '-colorspace RGB -quality '.$this->jpegQuality;
 		}
 			// ... but if 'im_v5effects' is set, dont care about 'im_no_effects'
 		if ($gfxConf['im_v5effects'])	{
@@ -325,8 +352,20 @@
 	 */
 	function maskImageOntoImage(&$im,$conf,$workArea)	{
 		if ($conf['file'] && $conf['mask'])	{
-			$BBimage = $this->imageMagickConvert($conf['file'],$this->gifExtension,'','','','','');
-			$BBmask = $this->imageMagickConvert($conf['mask'],$this->gifExtension,'','','','','');
+			$imgInf = pathinfo($conf['file']);
+			$imgExt = strtolower($imgInf['extension']);
+			if (!t3lib_div::inList($this->gdlibExtensions, $imgExt))	{
+				$BBimage = $this->imageMagickConvert($conf['file'],$this->gifExtension,'','','','','');
+			} else	{
+				$BBimage = $this->getImageDimensions($conf['file']);
+			}
+			$maskInf = pathinfo($conf['mask']);
+			$maskExt = strtolower($maskInf['extension']);
+			if (!t3lib_div::inList($this->gdlibExtensions, $maskExt))	{
+				$BBmask = $this->imageMagickConvert($conf['mask'],$this->gifExtension,'','','','','');
+			} else	{
+				$BBmask = $this->getImageDimensions($conf['mask']);
+			}
 			if ($BBimage && $BBmask)	{
 				$w = imagesx($im);
 				$h = imagesy($im);
@@ -337,26 +376,26 @@
 				$theMask2 = $tmpStr.'_mask2.'.trim($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_mask_temp_ext_noloss']);
 						// prepare overlay image
 				$cpImg = $this->imageCreateFromFile($BBimage[3]);
-				$destImg = imagecreate($w,$h);
-				ImageColorAllocate($destImg, 0,0,0);
+				$destImg = $this->imagecreate($w,$h);
+				$Bcolor = ImageColorAllocate($destImg, 0,0,0);
+				ImageFilledRectangle($destImg, 0, 0, $w, $h, $Bcolor);
 				$this->copyGifOntoGif($destImg,$cpImg,$conf,$workArea);
-				$this->ImageGif($destImg, $theImage);
+				$this->ImageWrite($destImg, $theImage);
 				imageDestroy($cpImg);
 				imageDestroy($destImg);
 						// prepare mask image
 				$cpImg = $this->imageCreateFromFile($BBmask[3]);
-				$destImg = imagecreate($w,$h);
-				ImageColorAllocate($destImg, 0,0,0);
+				$destImg = $this->imagecreate($w,$h);
+				$Bcolor = ImageColorAllocate($destImg, 0, 0, 0);
+				ImageFilledRectangle($destImg, 0, 0, $w, $h, $Bcolor);
 				$this->copyGifOntoGif($destImg,$cpImg,$conf,$workArea);
-				$this->ImageGif($destImg, $theMask);
+				$this->ImageWrite($destImg, $theMask);
 				imageDestroy($cpImg);
 				imageDestroy($destImg);
-					// treat the mask
-				$this->imageMagickExec($theMask,$theMask2,'-colorspace GRAY'.$this->maskNegate);
 					// mask the images
-				$this->ImageGif($im, $theDest);
+				$this->ImageWrite($im, $theDest);
 
-				$this->combineExec($theDest,$theImage,$theMask2,$theDest);
+				$this->combineExec($theDest,$theImage,$theMask2,$theDest, true);		// Let combineExec handle maskNegation
 
 				$backIm = $this->imageCreateFromFile($theDest);	// The main image is loaded again...
 				if ($backIm)	{	// ... and if nothing went wrong we load it onto the old one.
@@ -385,7 +424,7 @@
 	 */
 	function copyImageOntoImage(&$im,$conf,$workArea)	{
 		if ($conf['file'])	{
-			if ($conf['BBOX'][2]!=$this->gifExtension)	{
+			if (!t3lib_div::inList($this->gdlibExtensions, $conf['BBOX'][2]))	{
 				$conf['BBOX']=$this->imageMagickConvert($conf['BBOX'][3],$this->gifExtension,'','','','','');
 				$conf['file']=$conf['BBOX'][3];
 			}
@@ -486,11 +525,13 @@
 	 */
 	function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h)	{
 		if ($this->imagecopyresized_fix)	{
-			$im_base = imagecreatetruecolor(imagesx($im), imagesy($im));	// Make true color image
+			$im_base = $this->imagecreate(imagesx($im), imagesy($im));	// Make true color image
 			imagecopyresized($im_base, $im, 0,0,0,0, imagesx($im),imagesy($im),imagesx($im),imagesy($im));	// Copy the source image onto that
 			imagecopyresized($im_base, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);	// Then copy the $cpImg onto that (the actual operation!)
 			$im = $im_base;	// Set pointer
-			$this->makeEffect($im, Array('value'=>'colors=256'));	// Reduce colors to 256 - make SURE that IM is working then!
+			if (!$this->truecolor)	{
+				$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!
+			}
 		} else {
 			imagecopyresized($im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h);
 		}
@@ -517,7 +558,6 @@
 
 
 
-
 	/********************************
 	 *
 	 * Text / "TEXT" GIFBUILDER object
@@ -551,7 +591,10 @@
 				// NiceText is calculated
 			if (!$conf['niceText']) {
 					// Font Color is reserved:
-				$this->reduceColors($im,256, 200);
+				if (!$this->truecolor)	{
+					$reduce = t3lib_div::intInRange($this->setup['reduceColors'], 256, $this->truecolorColors, 256);
+					$this->reduceColors($im, $reduce-49, $reduce-50);	// If "reduce-49" colors (or more) are used reduce them to "reduce-50"
+				}
 				$Fcolor = ImageColorAllocate($im, $cols[0],$cols[1],$cols[2]);
 					// antiAliasing is setup:
 				$Fcolor = ($conf['antiAlias']) ? $Fcolor : -$Fcolor;
@@ -578,15 +621,16 @@
 				$newH = ceil($sF*imagesy($im));
 
 					// Make mask
-				$maskImg = imagecreate($newW, $newH);
-				ImageColorAllocate($maskImg, 255,255,255);
+				$maskImg = $this->imagecreate($newW, $newH);
+				$Bcolor = ImageColorAllocate($maskImg, 255,255,255);
+				ImageFilledRectangle($maskImg, 0, 0, $newW, $newH, $Bcolor);
 				$Fcolor = ImageColorAllocate($maskImg, 0,0,0);
 				if ($spacing || $wordSpacing)	{		// If any kind of spacing applys, we use this function:
 					$this->SpacedImageTTFText($maskImg, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, t3lib_stdGraphic::prependAbsolutePath($conf['fontFile']), $theText, $spacing, $wordSpacing, $conf['splitRendering.'],$sF);
 				} else {
 					$this->ImageTTFTextWrapper($maskImg, $conf['fontSize'], $conf['angle'], $txtPos[0], $txtPos[1], $Fcolor, $conf['fontFile'], $theText, $conf['splitRendering.'],$sF);
 				}
-				$this->ImageGif($maskImg, $fileMask);
+				$this->ImageWrite($maskImg, $fileMask);
 				ImageDestroy($maskImg);
 
 					// Downscales the mask
@@ -610,19 +654,21 @@
 						}
 					}
 				}
+
 				$this->imageMagickExec($fileMask,$fileMask,$command);
 
 					// Make the color-file
-				$colorImg = imagecreate($w,$h);
-				ImageColorAllocate($colorImg, $cols[0],$cols[1],$cols[2]);
-				$this->ImageGif($colorImg, $fileColor);
+				$colorImg = $this->imagecreate($w,$h);
+				$Ccolor = ImageColorAllocate($colorImg, $cols[0],$cols[1],$cols[2]);
+				ImageFilledRectangle($colorImg, 0, 0, $w, $h, $Ccolor);
+				$this->ImageWrite($colorImg, $fileColor);
 				ImageDestroy($colorImg);
 
 					// The mask is applied
-				$this->ImageGif($im, $fileMenu);	// The main pictures is saved temporarily
-
-				$this->combineExec($fileMenu,$fileColor,$fileMask,$fileMenu);
-
+				$this->ImageWrite($im, $fileMenu);	// The main pictures is saved temporarily
+				
+				$this->combineExec($fileMenu,$fileColor,$fileMask, $fileMenu);
+				
 				$backIm = $this->imageCreateFromFile($fileMenu);	// The main image is loaded again...
 				if ($backIm)	{	// ... and if nothing went wrong we load it onto the old one.
 					ImageColorTransparent($backIm,-1);
@@ -1258,19 +1304,21 @@
 			$fileMask = $tmpStr.'_mask.'.$this->gifExtension;
 
 				// BlurColor Image laves
-			$blurColImg = imagecreate($w,$h);
+			$blurColImg = $this->imagecreate($w,$h);
 			$bcols=$this->convertColor($conf['color']);
-			ImageColorAllocate($blurColImg, $bcols[0],$bcols[1],$bcols[2]);
-			$this->ImageGif($blurColImg, $fileColor);
+			$Bcolor = ImageColorAllocate($blurColImg, $bcols[0],$bcols[1],$bcols[2]);
+			ImageFilledRectangle($blurColImg, 0, 0, $w, $h, $Bcolor);
+			$this->ImageWrite($blurColImg, $fileColor);
 			ImageDestroy($blurColImg);
 
 				// The mask is made: BlurTextImage
-			$blurTextImg = imagecreate($w+$blurBorder*2,$h+$blurBorder*2);
-			ImageColorAllocate($blurTextImg, 0,0,0);		// black background
+			$blurTextImg = $this->imagecreate($w+$blurBorder*2,$h+$blurBorder*2);
+			$Bcolor = ImageColorAllocate($blurTextImg, 0,0,0);		// black background
+			ImageFilledRectangle($blurTextImg, 0, 0, $w+$blurBorder*2, $h+$blurBorder*2, $Bcolor);
 			$txtConf['fontColor'] = 'white';
 			$blurBordArr = Array($blurBorder,$blurBorder);
 			$this->makeText($blurTextImg,$txtConf,  $this->applyOffset($workArea,$blurBordArr));
-			$this->ImageGif($blurTextImg, $fileMask);	// dump to temporary file
+			$this->ImageWrite($blurTextImg, $fileMask);	// dump to temporary file
 			ImageDestroy($blurTextImg);	// destroy
 
 
@@ -1289,13 +1337,13 @@
 				}
 			}
 
-			$this->imageMagickExec($fileMask,$fileMask,$command);
+			$this->imageMagickExec($fileMask,$fileMask,$command.' +matte');
 
 			$blurTextImg_tmp = $this->imageCreateFromFile($fileMask);	// the mask is loaded again
 			if ($blurTextImg_tmp)	{	// if nothing went wrong we continue with the blurred mask
 
 					// cropping the border from the mask
-				$blurTextImg = imagecreate($w,$h);
+				$blurTextImg = $this->imagecreate($w,$h);
 				$this->imagecopyresized($blurTextImg, $blurTextImg_tmp, 0, 0, $blurBorder, $blurBorder, $w, $h, $w, $h);
 				ImageDestroy($blurTextImg_tmp);	// Destroy the temporary mask
 
@@ -1313,11 +1361,11 @@
 					$this->outputLevels($blurTextImg,0,$high,$this->maskNegate);	// reducing levels as the opacity demands
 				}
 
-				$this->ImageGif($blurTextImg, $fileMask);	// Dump the mask again
+				$this->ImageWrite($blurTextImg, $fileMask);	// Dump the mask again
 				ImageDestroy($blurTextImg);	// Destroy the mask
 
 					// The pictures are combined
-				$this->ImageGif($im, $fileMenu);	// The main pictures is saved temporarily
+				$this->ImageWrite($im, $fileMenu);	// The main pictures is saved temporarily
 
 				$this->combineExec($fileMenu,$fileColor,$fileMask,$fileMenu);
 
@@ -1376,7 +1424,10 @@
 		$conf['offset']=$cords[0].','.$cords[1];
 		$cords = $this->objPosition($conf,$workArea,Array($cords[2],$cords[3]));
 		$cols=$this->convertColor($conf['color']);
-		$this->reduceColors($im,256, 255);
+		if (!$this->truecolor)	{
+			$reduce = t3lib_div::intInRange($this->setup['reduceColors'], 256, $this->truecolorColors, 256);
+			$this->reduceColors($im, $reduce-1, $reduce-2);	// If "reduce-1" colors (or more) are used reduce them to "reduce-2"
+		}
 		$tmpColor = ImageColorAllocate($im, $cols[0],$cols[1],$cols[2]);
 		imagefilledrectangle($im, $cords[0], $cords[1], $cords[0]+$cords[2]-1, $cords[1]+$cords[3]-1, $tmpColor);
 	}
@@ -1526,9 +1577,10 @@
 		$conf['offset']=$cords[0].','.$cords[1];
 		$cords = $this->objPosition($conf,$this->workArea,Array($cords[2],$cords[3]));
 
-		$newIm = imagecreate($cords[2],$cords[3]);
+		$newIm = $this->imagecreate($cords[2],$cords[3]);
 		$cols=$this->convertColor($conf['backColor']?$conf['backColor']:$this->setup['backColor']);
-		ImageColorAllocate($newIm, $cols[0],$cols[1],$cols[2]);
+		$Bcolor = ImageColorAllocate($newIm, $cols[0],$cols[1],$cols[2]);
+		ImageFilledRectangle($newIm, 0, 0, $cords[2], $cords[3], $Bcolor);
 
 		$newConf = Array();
 		$workArea = Array(0,0,$cords[2],$cords[3]);
@@ -1554,7 +1606,7 @@
 		if ($conf['width'] || $conf['height'] || $conf['params'])	{
 			$tmpStr = $this->randomName();
 			$theFile = $tmpStr.'.'.$this->gifExtension;
-			$this->ImageGif($im, $theFile);
+			$this->ImageWrite($im, $theFile);
 			$theNewFile = $this->imageMagickConvert($theFile,$this->gifExtension,$conf['width'],$conf['height'],$conf['params'],'','');
 			$tmpImg = $this->imageCreateFromFile($theNewFile[3]);
 			if ($tmpImg)	{
@@ -1710,7 +1762,7 @@
 	}
 
 	/**
-	 * Reduce colors in image
+	 * Reduce colors in image dependend on the actual amount of colors (Only works if we are not in truecolor mode)
 	 *
 	 * @param	integer		GDlib Image Pointer
 	 * @param	integer		The max number of colors in the image before a reduction will happen; basically this means that IF the GD image current has the same amount or more colors than $limit define, THEN a reduction is performed.
@@ -1718,18 +1770,35 @@
 	 * @return	void
 	 */
 	function reduceColors(&$im,$limit, $cols)	{
-		if (ImageColorsTotal($im)>=$limit)	{
+		if (!$this->truecolor && ImageColorsTotal($im)>=$limit)	{
 			$this->makeEffect($im, Array('value'=>'colors='.$cols) );
 		}
 	}
 
-
-
-
-
-
-
-
+	/**
+	 * Reduce colors in image using IM and create a palette based image if possible (<=256 colors)
+	 *
+	 * @param	string		Image file to reduce
+	 * @param	integer		Number of colors to reduce the image to.
+	 * @return	string		Reduced file
+	 */
+	function IMreduceColors($file, $cols)	{
+		$fI = t3lib_div::split_fileref($file);
+		$ext = strtolower($fI['fileext']);
+		$result = $this->randomName().'.'.$ext;
+		if (($reduce = t3lib_div::intInRange($cols, 0, ($ext=='gif'?256:$this->truecolorColors), 0))>0)	{
+			$params = ' -colors '.$reduce;
+			if (!$this->im_version_4)	{
+					// IM4 doesn't have this options but forces them automatically if applicaple (<256 colors in image)
+				if ($reduce<=256)	{ $params .= ' -type Palette'; }
+				if ($ext=='png' && $reduce<=256)	{ $prefix = 'png8:'; }
+			}
+			$this->imageMagickExec($file, $prefix.$result, $params);
+			return $result;
+		}
+			// Nothing to perform
+		return $file;
+	}
 
 
 
@@ -2392,9 +2461,17 @@
 	 * @param	string		The relative (to PATH_site) image filepath, output filename (written to)
 	 * @return	void
 	 */
-	function combineExec($input,$overlay,$mask,$output)	{
+	function combineExec($input,$overlay,$mask,$output, $handleNegation = false)	{
 		if (!$this->NO_IMAGE_MAGICK)	{
-			$cmd = t3lib_div::imageMagickCommand('combine', '-compose over '.$this->wrapFileName($input).' '.$this->wrapFileName($overlay).' '.$this->wrapFileName($mask).' '.$this->wrapFileName($output));
+			$params = '-colorspace GRAY +matte';
+			if ($handleNegation)	{
+				if ($this->maskNegate)	{
+					$params .= ' '.$this->maskNegate;
+				}
+			}
+			$theMask = $this->randomName.'.'.$this->gifExtension;
+			$this->imageMagickExec($mask, $theMask, $params);
+			$cmd = t3lib_div::imageMagickCommand('combine', '-compose over +matte '.$this->wrapFileName($input).' '.$this->wrapFileName($overlay).' '.$this->wrapFileName($theMask).' '.$this->wrapFileName($output));		// +matte = no alpha layer in output
 			$this->IM_commands[] = Array ($output,$cmd);
 
 			$ret = exec($cmd);
@@ -2491,7 +2568,7 @@
 	function applyImageMagickToPHPGif(&$im, $command)	{
 		$tmpStr = $this->randomName();
 		$theFile = $tmpStr.'.'.$this->gifExtension;
-		$this->ImageGif($im, $theFile);
+		$this->ImageWrite($im, $theFile);
 		$this->imageMagickExec($theFile,$theFile,$command);
 		$tmpImg = $this->imageCreateFromFile($theFile);
 		if ($tmpImg)	{
@@ -2536,32 +2613,25 @@
 			ereg('([^\.]*)$',$file,$reg);
 			$ext=strtolower($reg[0]);
 			switch($ext)	{
-				case $this->gifExtension:
-					if ($this->ImageGif($this->im, $file))	{
-						// ImageMagick operations
-						if($this->setup['reduceColors'])	{
-							$this->imageMagickExec($file,$file,' -colors '.t3lib_div::intInRange($this->setup['reduceColors'],2,255));
-							t3lib_div::gif_compress($file,'');	// Compress with IM (lzw) or GD (rle)      (Workaround for the absence of lzw-compression in GD)
-						} else {
-							t3lib_div::gif_compress($file, 'IM');		// Compress with IM! (adds extra compression, LZW from ImageMagick)     (Workaround for the absence of lzw-compression in GD)
+				case 'gif':
+				case 'png':
+					if ($this->ImageWrite($this->im, $file))	{
+							// ImageMagick operations
+						if ($this->setup['reduceColors'])	{
+							$reduced = $this->IMreduceColors($file, $this->setup['reduceColors']);
+							unlink($file);
+							copy($reduced, $file);
 						}
+						t3lib_div::gif_compress($file, 'IM');		// Compress with IM! (adds extra compression, LZW from ImageMagick)     (Workaround for the absence of lzw-compression in GD)
 					}
 				break;
 				case 'jpg':
 				case 'jpeg':
-					$tmpStr = $this->randomName();
-					$theFile = $tmpStr.'.'.$this->gifExtension;
-					if ($this->ImageGif($this->im, $theFile))	{
-						// ImageMagick operations
-						$operations='';
-						if($this->setup['quality'])	{
-							$operations.=' -quality '.t3lib_div::intInRange($this->setup['quality'],10,100);
-						}
-						$this->imageMagickExec($theFile,$file,$operations);
-						if (!$this->dontUnlinkTempFiles)	{
-							unlink($theFile);
-						}
+					$quality = 0;	// Use the default
+					if($this->setup['quality'])	{
+						$quality = t3lib_div::intInRange($this->setup['quality'],10,100);
 					}
+					if ($this->ImageWrite($this->im, $file, $quality));
 				break;
 			}
 			$GLOBALS['TEMP_IMAGES_ON_PAGE'][]=$file;
@@ -2594,26 +2664,58 @@
 	 *
 	 * @param	pointer		The GDlib image resource pointer
 	 * @param	string		The filename to write to
-	 * @return	mixed		The output of either imageGif or imagePng based on whether the $this->gifExtension was set to "gif" or "png"
+	 * @return	mixed		The output of either imageGif, imagePng or imageJpeg based on the filename to write
 	 * @see maskImageOntoImage(), scale(), output()
 	 */
-	function ImageGif($destImg, $theImage)	{
+	function imageWrite($destImg, $theImage, $quality = 0)	{
 		imageinterlace ($destImg,0);
-		if ($this->gifExtension=='gif')	{
-			return ImageGif($destImg, $theImage);
-		}
-		if ($this->gifExtension=='png')	{
-			return ImagePng($destImg, $theImage);
+		$ext = strtolower(substr($theImage, strrpos($theImage, '.')+1));
+		switch ($ext)	{
+			case 'jpg':
+			case 'jpeg':
+				if (!$quality)	{
+					$quality = $this->jpegQuality;
+				}
+				if (function_exists('imageJpeg'))	{
+					return imageJpeg($destImg, $theImage, $quality);
+				}
+			break;
+			case 'gif':
+				if (function_exists('imageGif'))	{
+					return imageGif($destImg, $theImage);
+				}
+			break;
+			case 'png':
+				if (function_exists('imagePng'))	{
+					return ImagePng($destImg, $theImage);
+				}
+			break;
 		}
+		return false;		// Extension invalid or write-function does not exist
+	}
+	
+	
+
+	/**
+	 * Writes the input GDlib image pointer to file. Now just a wrapper to ImageWrite.
+	 *
+	 * @param	pointer		The GDlib image resource pointer
+	 * @param	string		The filename to write to
+	 * @return	mixed		The output of either imageGif, imagePng or imageJpeg based on the filename to write
+	 * @see imageWrite()
+	 * @deprecated
+	 */
+	function imageGif($destImg, $theImage)	{
+		return $this->imageWrite($destImg, $theImage);
 	}
 
 	/**
 	 * This function has been renamed and only exists for providing backwards compatibility.
 	 * Please use $this->imageCreateFromFile() instead.
 	 *
-	 * @param	string		Image filename
+	 * @param		string		Image filename
 	 * @return	pointer		Image Resource pointer
-	 * @depreciated
+	 * @deprecated
 	 */
 	function imageCreateFromGif($sourceImg)	{
 		return $this->imageCreateFromFile($sourceImg);
@@ -2623,27 +2725,114 @@
 	 * Creates a new GDlib image resource based on the input image filename.
 	 * If it fails creating a image from the input file a blank gray image with the dimensions of the input image will be created instead.
 	 *
-	 * @param	string		Image filename
+	 * @param		string		Image filename
 	 * @return	pointer		Image Resource pointer
 	 */
 	function imageCreateFromFile($sourceImg)	{
 		$imgInf = pathinfo($sourceImg);
 		$ext = strtolower($imgInf['extension']);
-
-		if ($ext=='gif' && function_exists('imagecreatefromgif'))	{
-			return imageCreateFromGif($sourceImg);
-		} elseif ($ext=='png' && function_exists('imagecreatefrompng'))	{
-			return imageCreateFromPng($sourceImg);
-		} elseif (($ext=='jpg' || $ext=='jpeg') && function_exists('imagecreatefromjpeg'))	{
-			return imageCreateFromJpeg($sourceImg);
+		
+		switch ($ext)	{
+			case 'gif':
+				if (function_exists('imagecreatefromgif'))	{
+					return imageCreateFromGif($sourceImg);
+				}
+			break;
+			case 'png':
+				if (function_exists('imagecreatefrompng'))	{
+					return imageCreateFromPng($sourceImg);
+				}
+			break;
+			case 'jpg':
+			case 'jpeg':
+				if (function_exists('imagecreatefromjpeg'))	{
+					return imageCreateFromJpeg($sourceImg);
+				}
+			break;
 		}
 
 		// If non of the above:
 		$i = @getimagesize($sourceImg);
-		$im = imagecreate($i[0],$i[1]);
-		ImageColorAllocate($im, 128,128,128);
+		$im = $this->imagecreate($i[0],$i[1]);
+		$Bcolor = ImageColorAllocate($im, 128,128,128);
+		ImageFilledRectangle($im, 0, 0, $i[0], $i[1], $Bcolor);
 		return $im;
 	}
+
+
+	/**
+	 * Creates a new GD image resource. Wrapper for imagecreate(truecolor) depended if GD2 is used.
+	 *
+	 * @param		integer		Width
+	 * @param		integer		Height
+	 * @return	pointer		Image Resource pointer
+	 */
+	function imagecreate($w, $h)	{
+		if($this->truecolor && $this->GD2 && function_exists('imagecreatetruecolor'))	{
+			return imagecreatetruecolor($w, $h);
+		} else	{
+			return imagecreate($w, $h);
+		}
+		
+	}
+
+	/**
+	 * Returns the HEX color value for an RGB color array
+	 *
+	 * @param		array		RGB color array
+	 * @return	string	HEX color value
+	 */
+	function hexColor($col)	{
+		$r = dechex($col[0]);
+		if (strlen($r)<2)	{ $r = '0'.$r; }
+		$g = dechex($col[1]);
+		if (strlen($g)<2)	{ $g = '0'.$g; }
+		$b = dechex($col[2]);
+		if (strlen($b)<2)	{ $b = '0'.$b; }
+		return '#'.$r.$g.$b;
+	}
+
+	/**
+	 * Unifies all colors given in the colArr color array to the first color in the array.
+	 *
+	 * @param		pointer	Image resource
+	 * @param		array		array containing RGB color arrays
+	 * @return	integer	The index of the unified color
+	 */
+	function unifyColors(&$img, $colArr)	{
+		$retCol = false;
+		if (is_array($colArr)&&function_exists('imagepng')&&function_exists('imagecreatefrompng'))	{
+			$origName = $preName = $this->randomName().'.png';
+			$postName = $this->randomName().'.'.trim($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_mask_temp_ext_noloss']);
+			$this->imageWrite($img, $preName);
+			$firstCol = array_shift($colArr);
+			$firstColArr = $this->convertColor($firstCol);
+			$firstCol = $this->hexColor($firstColArr);
+			while(list(,$transparentColor)=each($colArr))	{
+				$transparentColor = $this->convertColor($transparentColor);
+				$transparentColor = $this->hexColor($transparentColor);
+				$cmd = '-fill "'.$firstCol.'" -opaque "'.$transparentColor.'"';
+				$this->imageMagickExec($preName, $postName, $cmd);
+				$preName = $postName;
+			}
+			$this->imageMagickExec($postName, $origName, '');
+			if (@is_file($origName))	{
+				$tmpImg = $this->imageCreateFromFile($origName);
+				if ($tmpImg)	{
+					$img = $tmpImg;
+					$retCol = ImageColorExact ($img, $firstColArr[0], $firstColArr[1], $firstColArr[2]);
+				}
+			}
+				// unlink files from process
+			if (!$this->dontUnlinkTempFiles)	{
+				unlink($origName);
+				unlink($postName);
+			}
+		}
+		return $retCol;
+	}
+
+
 }
 
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_stdgraphic.php'])	{
diff -u -ru TYPO3core/t3lib/config_default.php TYPO3core.gifbuilder/t3lib/config_default.php
--- TYPO3core/t3lib/config_default.php	2005-10-26 11:38:43.000000000 +0200
+++ TYPO3core.gifbuilder/t3lib/config_default.php	2005-10-26 21:49:17.330688656 +0200
@@ -43,7 +43,7 @@
 		'im_noScaleUp' => 0,					// Boolean. If set, images are not being scaled up if told so (in t3lib/stdgraphics.php)
 		'im_combine_filename' => 'combine',		// String. Latest ImageMagick versions has changed the name of combine to composite. Configure here if needed.
 		'im_noFramePrepended' => 0,				// Boolean. If set, the [x] frame indicator is NOT prepended to filenames in stdgraphic. Some IM5+ version didn't work at all with the typical [0]-prefix, which allow multipage pdf's and animated gif's to be scaled only for the first frame/page and that seriously cuts down rendering time. Set this flag only if your ImageMagick version cannot find the files. Notice that changing this flag causes temporary filenames to change, thus the server will begin scaling images again which were previously cached.
-		'im_jpg_quality' => 70,					// Integer. Default JPEG generation quality
+		'jpg_quality' => 70,					// Integer. Default JPEG generation quality
 		'enable_typo3temp_db_tracking' => 0,	// Boolean. 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.
 		'TTFLocaleConv' => '',					// String. Enter locale conversion string used to recode input to TrueType functions. Eg. 'cp1250..UTF-8'. Works ONLY if 'recode' is enabled in PHP. Depreciated from ver. 3.6.0 of TYPO3. Set up [BE][forceCharset] as strings are automatically converted from databsae charset to UTF-8.
 		'TTFdpi' => '72',						// Integer. Enter how many dpi the FreeType module uses. Freetype1 should be set to 72. Freetype2 should be set to 96 (otherwise fonts are rendered way bigger than FreeType1). This works as a global scaling factor for Freetype.
@@ -303,6 +303,9 @@
 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_imvMaskState'])	{
 	$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_negate_mask']=$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_negate_mask']?0:1;
 }
+if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_jpg_quality'])	{
+	$GLOBALS['TYPO3_CONF_VARS']['GFX']['jpg_quality'] = $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_jpg_quality'];
+}
 
 	// simple debug function which prints output immediately
 function xdebug($var='',$br=0)	{
diff -u -ru TYPO3core/typo3/sysext/cms/tslib/class.tslib_content.php TYPO3core.gifbuilder/typo3/sysext/cms/tslib/class.tslib_content.php
--- TYPO3core/typo3/sysext/cms/tslib/class.tslib_content.php	2005-10-26 11:38:43.000000000 +0200
+++ TYPO3core.gifbuilder/typo3/sysext/cms/tslib/class.tslib_content.php	2005-10-26 22:09:59.010924568 +0200
@@ -4536,7 +4536,7 @@
 							if (is_array($maskArray) && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im'])	{
 									// Filename:
 								$fI = t3lib_div::split_fileref($theImage);
-								$dest = $gifCreator->tempPath.$hash.'.'.($fI['fileext']==$gifCreator->gifExtension ? $gifCreator->gifExtension : 'jpg');
+								$dest = $gifCreator->tempPath.$hash.'.'.(strtolower($fI['fileext'])==$gifCreator->gifExtension ? $gifCreator->gifExtension : 'jpg');
 								if (!@file_exists($dest))	{		// Generate!
 									$m_mask= $maskImages['m_mask'];
 									$m_bgImg = $maskImages['m_bgImg'];
@@ -4591,9 +4591,23 @@
 									}
 								}
 									// Finish off
+								if ($fileArray['reduceColors']&&is_file($dest))	{
+									$reduced = $gifCreator->IMreduceColors($dest, $fileArray['reduceColors']);
+									if (is_file($reduced))	{
+										unlink($dest);
+										rename($reduced, $dest);
+									}
+								}
 								$GLOBALS['TSFE']->tmpl->fileCache[$hash]= $gifCreator->getImageDimensions($dest);
 							} else {		// Normal situation:
 								$GLOBALS['TSFE']->tmpl->fileCache[$hash]= $gifCreator->imageMagickConvert($theImage,$fileArray['ext'],$fileArray['width'],$fileArray['height'],$fileArray['params'],$fileArray['frame'],$options);
+								if ($fileArray['reduceColors']&&is_file($GLOBALS['TSFE']->tmpl->fileCache[$hash][3]))	{
+									$reduced = $gifCreator->IMreduceColors($GLOBALS['TSFE']->tmpl->fileCache[$hash][3], $fileArray['reduceColors']);
+									if (is_file($reduced))	{
+										unlink($GLOBALS['TSFE']->tmpl->fileCache[$hash][3]);
+										rename($reduced, $GLOBALS['TSFE']->tmpl->fileCache[$hash][3]);
+									}
+								}
 							}
 							$GLOBALS['TSFE']->tmpl->fileCache[$hash]['origFile'] = $theImage;
 							$GLOBALS['TSFE']->tmpl->fileCache[$hash]['origFile_mtime'] = @filemtime($theImage);	// This is needed by tslib_gifbuilder, ln 100ff in order for the setup-array to create a unique filename hash.
@@ -7241,4 +7255,4 @@
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_content.php'])	{
 	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['tslib/class.tslib_content.php']);
 }
-?>
+?>
\ No newline at end of file
diff -u -ru TYPO3core/typo3/sysext/cms/tslib/class.tslib_gifbuilder.php TYPO3core.gifbuilder/typo3/sysext/cms/tslib/class.tslib_gifbuilder.php
--- TYPO3core/typo3/sysext/cms/tslib/class.tslib_gifbuilder.php	2005-04-14 01:12:02.000000000 +0200
+++ TYPO3core.gifbuilder/typo3/sysext/cms/tslib/class.tslib_gifbuilder.php	2005-10-26 21:49:17.333688200 +0200
@@ -132,6 +132,7 @@
 			$this->setup = $conf;
 			$this->data = $data;
 
+
 			/* Hook preprocess gifbuilder conf 
 			 * Added by Julle for 3.8.0
 			 *
@@ -173,8 +174,8 @@
 				$cObj->start($this->data);
 				$this->setup['backColor'] = trim($cObj->stdWrap($this->setup['backColor'], $this->setup['backColor.']));
 			}
-			if (!$this->setup['backColor'])	{$this->setup['backColor']='white';}
-
+			if (!$this->setup['backColor'])	{ $this->setup['backColor']='white'; }
+			
 				// Transparent GIFs
 				// not working with reduceColors
 				// there's an option for IM: -transparent colors
@@ -344,13 +345,14 @@
 		$XY = $this->XY;
 
 			// Gif-start
-		$this->im = imagecreate($XY[0],$XY[1]);
+		$this->im = $this->imagecreate($XY[0],$XY[1]);
 		$this->w = $XY[0];
 		$this->h = $XY[1];
 
 			// backColor is set
-		$cols=$this->convertColor($this->setup['backColor']);
-		ImageColorAllocate($this->im, $cols[0],$cols[1],$cols[2]);
+		$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:
 		if (is_array($this->setup))	{
@@ -431,28 +433,19 @@
 				}
 			}
 		}
-			// Auto transparent background is set
+
+
 		if ($this->setup['transparentBackground'])	{
-			imagecolortransparent($this->im, imagecolorat($this->im, 0, 0));
-		}
-			// TransparentColors are set
-		if (is_array($this->setup['transparentColor_array']))	{
-			reset($this->setup['transparentColor_array']);
-			while(list(,$transparentColor)=each($this->setup['transparentColor_array']))	{
-				$cols=$this->convertColor($transparentColor);
-				if ($this->setup['transparentColor.']['closest'])	{
-					$colIndex = ImageColorClosest ($this->im, $cols[0],$cols[1],$cols[2]);
-				} else {
-					$colIndex = ImageColorExact ($this->im, $cols[0],$cols[1],$cols[2]);
-				}
-				if ($colIndex > -1) {
-					ImageColorTransparent($this->im, $colIndex);
-				} else {
-					ImageColorTransparent($this->im, ImageColorAllocate($this->im, $cols[0],$cols[1],$cols[2]));
-				}
-				break;		// Originally we thought of letting many colors be defined as transparent, but GDlib seems to accept only one definition. Therefore we break here. Maybe in the future this 'break' will be cancelled if a method of truly defining many transparent colors could be found.
+				// Auto transparent background is set
+			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']);
+			if ($Tcolor)	{
+				imagecolortransparent($this->im, $Tcolor);
 			}
 		}
+
 	}
 
 
diff -u -ru TYPO3core/typo3/sysext/install/mod/class.tx_install.php TYPO3core.gifbuilder/typo3/sysext/install/mod/class.tx_install.php
--- TYPO3core/typo3/sysext/install/mod/class.tx_install.php	2005-09-18 20:43:37.000000000 +0200
+++ TYPO3core.gifbuilder/typo3/sysext/install/mod/class.tx_install.php	2005-10-26 21:59:15.852699480 +0200
@@ -2844,7 +2844,7 @@
 						if (!@is_file($mask))	die("Error: ".$mask." was not a file");
 
 					$output = $imageProc->tempPath.$imageProc->filenamePrefix.t3lib_div::shortMD5($imageProc->alternativeOutputKey."combine1").".jpg";
-					$imageProc->combineExec($input,$overlay,$mask,$output);
+					$imageProc->combineExec($input,$overlay,$mask,$output, true);
 					$fileInfo = $imageProc->getImageDimensions($output);
 					$result = $this->displayTwinImage($fileInfo[3],$imageProc->IM_commands);
 					$this->message($headCode,"Combine using a GIF mask with only black and white",$result[0],$result[1]);
@@ -2858,7 +2858,7 @@
 						if (!@is_file($overlay))	die("Error: ".$overlay." was not a file");
 						if (!@is_file($mask))	die("Error: ".$mask." was not a file");
 					$output = $imageProc->tempPath.$imageProc->filenamePrefix.t3lib_div::shortMD5($imageProc->alternativeOutputKey."combine2").".jpg";
-					$imageProc->combineExec($input,$overlay,$mask,$output);
+					$imageProc->combineExec($input,$overlay,$mask,$output, true);
 					$fileInfo = $imageProc->getImageDimensions($output);
 					$result = $this->displayTwinImage($fileInfo[3],$imageProc->IM_commands);
 					$this->message($headCode,"Combine using a JPG mask with graylevels",$result[0],$result[1]);
@@ -2883,8 +2883,9 @@
 				if ($gdActive)	{
 					// GD with box
 					$imageProc->IM_commands=array();
-					$im = imageCreate(170,136);
-					ImageColorAllocate ($im, 0, 0, 0);
+					$im = $imageProc->imageCreate(170,136);
+					$Bcolor = ImageColorAllocate ($im, 0, 0, 0);
+					ImageFilledRectangle($im, 0, 0, 170, 136, $Bcolor);
 					$workArea=array(0,0,170,136);
 					$conf=array(
 						"dimensions" => "10,50,150,36",
@@ -2892,7 +2893,7 @@
 					);
 					$imageProc->makeBox($im,$conf,$workArea);
 					$output = $imageProc->tempPath.$imageProc->filenamePrefix.t3lib_div::shortMD5("GDbox").".".$imageProc->gifExtension;
-					$imageProc->ImageGif ($im,$output);
+					$imageProc->ImageWrite($im,$output);
 					$fileInfo = $imageProc->getImageDimensions($output);
 					$result = $this->displayTwinImage($fileInfo[3],$imageProc->IM_commands);
 					$this->message($headCode,"Create simple image",$result[0],$result[1]);
@@ -2909,7 +2910,7 @@
 					$conf["color"]="olive";
 					$imageProc->makeBox($im,$conf,$workArea);
 					$output = $imageProc->tempPath.$imageProc->filenamePrefix.t3lib_div::shortMD5("GDfromImage+box").".".$imageProc->gifExtension;
-					$imageProc->ImageGif ($im,$output);
+					$imageProc->ImageWrite($im,$output);
 					$fileInfo = $imageProc->getImageDimensions($output);
 					$GDWithBox_filesize = @filesize($output);
 					$result = $this->displayTwinImage($fileInfo[3],$imageProc->IM_commands);
@@ -2918,8 +2919,9 @@
 
 					// GD with text
 					$imageProc->IM_commands=array();
-					$im = imageCreate(170,136);
-					ImageColorAllocate ($im, 128,128,150);
+					$im = $imageProc->imageCreate(170,136);
+					$Bcolor = ImageColorAllocate ($im, 128,128,150);
+					ImageFilledRectangle($im, 0, 0, 170, 136, $Bcolor);
 					$workArea=array(0,0,170,136);
 					$conf=array(
 						"iterations" => 1,
@@ -2935,7 +2937,7 @@
 					$imageProc->makeText($im,$conf,$workArea);
 
 					$output = $imageProc->tempPath.$imageProc->filenamePrefix.t3lib_div::shortMD5("GDwithText").".".$imageProc->gifExtension;
-					$imageProc->ImageGif ($im,$output);
+					$imageProc->ImageWrite($im,$output);
 					$fileInfo = $imageProc->getImageDimensions($output);
 					$result = $this->displayTwinImage($fileInfo[3],$imageProc->IM_commands);
 					$this->message($headCode,"Render text with TrueType font",$result[0],$result[1]);
@@ -2947,7 +2949,7 @@
 						$imageProc->makeText($im,$conf,$workArea);
 
 						$output = $imageProc->tempPath.$imageProc->filenamePrefix.t3lib_div::shortMD5("GDwithText-niceText").".".$imageProc->gifExtension;
-						$imageProc->ImageGif ($im,$output);
+						$imageProc->ImageWrite($im,$output);
 						$fileInfo = $imageProc->getImageDimensions($output);
 						$result = $this->displayTwinImage($fileInfo[3],$imageProc->IM_commands, array("Note on 'niceText':","'niceText' is a concept that tries to improve the antialiasing of the rendered type by actually rendering the textstring in double size on a black/white mask, downscaling the mask and masking the text onto the image through this mask. This involves ImageMagick 'combine'/'composite' and 'convert'."));
 						$this->message($headCode,"Render text with TrueType font using 'niceText' option",
@@ -2976,7 +2978,7 @@
 						$imageProc->makeText($im,$conf,$workArea);
 
 						$output = $imageProc->tempPath.$imageProc->filenamePrefix.t3lib_div::shortMD5("GDwithText-niceText-shadow").".".$imageProc->gifExtension;
-						$imageProc->ImageGif ($im,$output);
+						$imageProc->ImageWrite($im,$output);
 						$fileInfo = $imageProc->getImageDimensions($output);
 						$result = $this->displayTwinImage($fileInfo[3],$imageProc->IM_commands, array("Note on drop shadows:","Drop shadows are done my using ImageMagick to blur a mask through which the drop shadow is generated. The blurring of the mask only works in ImageMagick 4.2.9 and <i>not</i> ImageMagick 5 - which is why you may see a hard and not soft shadow."));
 						$this->message($headCode,"Render 'niceText' with a shadow under",
@@ -4783,4 +4785,4 @@
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/install/mod/class.tx_install.php'])	{
 	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/install/mod/class.tx_install.php']);
 }
-?>
\ No newline at end of file
+?>
