Page 1 of 1

imageCopyResampled and transparent images

Posted: Sat Apr 08, 2006 10:53 pm
by Kazper
I seem to be having a bit of a problem. Below is a snippet of the important part of the code I'm trying to modify:

Code: Select all

function render() {
		header("Content-type: image/" . $this->type);
		if ($this->read_cache) {
			readfile($this->cache_folder . $this->cache_file);
		} else {
			$this->font_file = $this->_generateFontPath();

			$dimensions = $this->_getTextSize();

			$this->_img = @ImageCreateTrueColor($dimensions['width'], $dimensions['height']);
			$black = @ImageColorAllocate($this->_img, 0,0,0);

			$bgcolour = $this->allocate_colour($this->bgcolour);
			imagefill($this->_img, 0, 0, $bgcolour);
			ImageColorTransparent($this->_img, $bgcolour);

			$this->width = $dimensions['width'];
			$this->height = $dimensions['height'];

			//Add the shadow
			if ($this->shadow) {
			    imagettftext($this->_img, $this->size, 0, ($this->padding[3] * WP_IMAGEREPLACE_RESAMPLE_SIZE) + (WP_IMAGEREPLACE_SHADOW_SHIFT * WP_IMAGEREPLACE_RESAMPLE_SIZE), ($this->size + ($this->padding[0] * WP_IMAGEREPLACE_RESAMPLE_SIZE) + (WP_IMAGEREPLACE_SHADOW_SHIFT * WP_IMAGEREPLACE_RESAMPLE_SIZE)), $this->allocate_colour(WP_IMAGEREPLACE_SHADOW_COLOUR), $this->font_file, $this->text);
			}

			//Add the text
			imagettftext($this->_img, $this->size, 0, ($this->padding[3] * WP_IMAGEREPLACE_RESAMPLE_SIZE), ($this->size + ($this->padding[0] * WP_IMAGEREPLACE_RESAMPLE_SIZE)), $this->allocate_colour($this->colour), $this->font_file, $this->text);

			//Resample to view size
			$im = imagecreatetruecolor($this->width/WP_IMAGEREPLACE_RESAMPLE_SIZE, $this->height/WP_IMAGEREPLACE_RESAMPLE_SIZE);
			if (function_exists('imageantialias')) { //Create transparant background
				imageAntiAlias($im,true);
				imagealphablending($im, false);
				imagesavealpha($im,true);
				$transparent = imagecolorallocatealpha($im, 255, 255, 255, 127);
				imagefilledrectangle($im, 0, 0, $this->width/WP_IMAGEREPLACE_RESAMPLE_SIZE, $this->height/WP_IMAGEREPLACE_RESAMPLE_SIZE, $transparent);
			}
			imagecopyresampled($im, $this->_img, 0, 0, 0, 0, $this->width/WP_IMAGEREPLACE_RESAMPLE_SIZE, $this->height/WP_IMAGEREPLACE_RESAMPLE_SIZE, $this->width, $this->height);
			imagedestroy($this->_img);
			$this->_img = $im;
The problem here is in the imagecopyresampled line. The first image ($this->_img) is properly transparent, but needs to be resampled down in size considerably. The second image created ($im) is also transparent (albeit emplty of course) after the imagefilledrectangle function.

However, when those two - properly transparent images, with the same transparent color (white, 255, 255, 255) are combined and resampled with the imagecopyresampled function the resulting image is NOT transparent, but get a white background. This is no matter if I output the image in gif or png, and no matter if I view it in IE, FF or Safari. It is no longer transparent.

If I use imagecopyresized instead of imagecopyresampled it does retain transparency properly, but the resulting image is of unusable quality due to the large downsizing without resampling. To me this looks like a bug in imagecopyresampled, but I haven't been able to find anything about it anywhere. Perhaps someone here can help me with the problem?