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...

Link to comment
Share on other sites

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;
   }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.