I'm trying to resize an image on the fly, but if the image contains transparency, it isn't preserved. I'm a bit confused by all the various alpha related GD functions. Here's what I have. I'm sure I need to add something around the imageCopyResampled part.

$info = getimagesize( $file );

//-----------------
// create the image
//-----------------
switch ( $info[2] )
	{
	case 1: $im_org = imageCreateFromGIF( $file );	break;
	case 2: $im_org = imageCreateFromJPEG( $file );	break;
	case 3: $im_org = imageCreateFromPNG( $file );	break;
	}

//--------------------------------
// do we have to resize the image?
//--------------------------------
if ( $info[0] > $maxWidth )
	{
	$width = $maxWidth;
	$height = round( ( $width / $info[0] ) * $info[1] );
	$im_new = imageCreateTrueColor( $width, $height );
	imageCopyResampled( $im_new, $im_org, 0, 0, 0, 0, $width, $height, $info[0], $info[1]);
	}
else
	{
	$im_new = $im_org;
	}

//------------------------------
// send the image to the browser
//------------------------------
switch ( $info[2] )
	{
	case 1: 
		{
		@header("Content-type: image/gif");
		imageTrueColorToPalette( $im_new, TRUE, 256 );
		imageGIF( $im_new );
		break;
		}
	case 2:
		{
		@header("Content-type: image/jpeg");
		imageJPEG( $im_new );
		break;
		}
	case 3:
		{
		@header("Content-type: image/png");
		imagePNG( $im_new );
		break;
		}
	}

imageDestroy( $im_org );
imageDestroy( $im_new );

    I think I solved the transparency for PNG images by adding the following before the imageCopyResampled function:

    if ( $info[2] == 3 ) // if type is PNG
    	{
    	imagealphablending( $im_new, FALSE );
    	imagesavealpha( $im_new, TRUE );
    	}

    But I still don't have transparency for GIFs.

    Any ideas?

      I think I figured it out, and since I couldn't find a solution anywhere else, I figured I'd post what I found out here for anyone else who needs help with this.

      Basically, for GIF images, in order to preserve the transparency in the new resized image, you can't use a true color image as a destination. You need to create a regular palette image as the destination image and designate the first color as transparent.

      $info = getimagesize( $file ); 
      
      //----------------- 
      // create the image 
      //----------------- 
      switch ( $info[2] ) // which type of image is it?
          { 
          case 1: $im_org = imageCreateFromGIF( $file );    break; 
          case 2: $im_org = imageCreateFromJPEG( $file );    break; 
          case 3: $im_org = imageCreateFromPNG( $file );    break; 
          } 
      
      //-----------------
      // resize the image
      //-----------------
      if ( $info[2] == 1 ) // create a palette image for GIFs
      	{
      	$im_new = imageCreate( $newWidth, $newHeight );
      	$transparent = imagecolorallocatealpha( $im_new, 255, 255, 255, 127 );
      	imagecolortransparent( $im_new, $transparent );
      	}
      else // create a true color image for PNGs and JPEGs
      	{
      	$im_new = imageCreateTrueColor( $newWidth, $newHeight );
      	}
      if ( $info[2] == 3 ) // alphablending OFF and savealpha ON for PNGs
      	{
      	imagealphablending( $im_new, FALSE );
      	imagesavealpha( $im_new, TRUE );
      	}
      imageCopyResampled( $im_new, $im_org, 0, 0, 0, 0, $newWidth, $newHeight, $info[0], $info[1]);
      
      //------------------------------ 
      // send the image to the browser 
      //------------------------------ 
      switch ( $info[2] ) 
          { 
          case 1: 
              { 
              @header("Content-type: image/gif"); 
              imageGIF( $im_new ); 
              break; 
              } 
          case 2: 
              { 
              @header("Content-type: image/jpeg"); 
              imageJPEG( $im_new ); 
              break; 
              } 
          case 3: 
              { 
              @header("Content-type: image/png"); 
              imagePNG( $im_new ); 
              break; 
              } 
          } 
      
      imageDestroy( $im_org ); 
      imageDestroy( $im_new ); 
      
        Write a Reply...