Jump to content

Resize GIF file without losing transparency


Ninjakreborn

Recommended Posts

<?php
   function resize($width,$height) {
	// Setup new image
	$new_image = imagecreatetruecolor($width, $height);

	if( $this->image_type == IMAGETYPE_JPEG ) {
	} elseif( $this->image_type == IMAGETYPE_GIF ) {
		imagealphablending($new_image, false);
		imagesavealpha($new_image,true);
		$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
		imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
		imagecolortransparent  ( $new_image, $transparent);
	} elseif( $this->image_type == IMAGETYPE_PNG ) {
		// These parameters are required for handling PNG files.
		imagealphablending($new_image, false);
		imagesavealpha($new_image,true);
		$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
		imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
	}

	// Resize image
	imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
	$this->image = $new_image;
   }
?>

The stuff for the PNG and JPG work perfectly...but the GIF is not working. I tried the same thing that I am doing for PNG files and it's not working.

Any advice on how to resize a GIF file and have it show up correctly..the full sized image works fine...but not when it's resized...

Just for your information this is what I ended up putting together.  Based off what I had (that was already working for JPG and PNG files, I have taken stuff from the resource you pointed to and it started working. Below is the final function I am using to resize that seems to work across the board for all image types.

 

<?php
   function resize($width,$height) {
	// Setup new image
	$new_image = imagecreatetruecolor($width, $height);

	if( $this->image_type == IMAGETYPE_JPEG ) {
	} elseif( $this->image_type == IMAGETYPE_GIF) {
		$trnprt_indx = imagecolortransparent($this->image);

		// If we have a specific transparent color
		if ($trnprt_indx >= 0) {
			// Get the original image's transparent color's RGB values
			$trnprt_color    = imagecolorsforindex($this->image, $trnprt_indx);
			// Allocate the same color in the new image resource
			$trnprt_indx    = imagecolorallocate($new_image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);				
			// Completely fill the background of the new image with allocated color.
			imagefill($new_image, 0, 0, $trnprt_indx);				
			// Set the background color for new image to transparent
			imagecolortransparent($new_image, $trnprt_indx);
		}
	} elseif( $this->image_type == IMAGETYPE_PNG ) {
		// These parameters are required for handling PNG files.
		imagealphablending($new_image, false);
		imagesavealpha($new_image,true);
		$transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
		imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
	}

	// Resize image
	imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
	$this->image = $new_image;
   }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.