Jump to content

png transparency issue with GD


WNO

Recommended Posts

I am having difficulty keeping transparency on my png resizing function.  I have searched all around and tried various solutions.  However, all I keep getting is a black background.  Everything else about the function is working fine.  Any help would be appreciated.

 

Here is the code for transparency that isn't working:

			$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
		imagealphablending($dst_img,false);
		imagesavealpha($dst_img,true);
		$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
		imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $transparent);
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

 

Here is the full function code:

		function createthumb($name,$filename,$new_w,$new_h)
	{
			$system=explode(".",$name);
		if (preg_match("/jpg|jpeg/",$system[1]))
		{
		$src_img=imagecreatefromjpeg($name);
		}
		if (preg_match("/png/",$system[1]))
		{
		$src_img=imagecreatefrompng($name);
		}
		$old_x=imageSX($src_img);
		$old_y=imageSY($src_img);
		if ($old_x > $old_y) 
		{
			$thumb_w=$new_w;
			$thumb_h=$old_y*($new_h/$old_x);
		}
			if ($old_x < $old_y) 
		{
		$thumb_w=$old_x*($new_w/$old_y);
		$thumb_h=$new_h;
		}
		if ($old_x == $old_y) 
		{
		$thumb_w=$new_w;
		$thumb_h=$new_h;
		}
		$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);
		imagealphablending($dst_img,false);
		imagesavealpha($dst_img,true);
		$transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127);
		imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $transparent);
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

		if (preg_match("/png/",$system[1]))
		{
		imagepng($dst_img,$filename); 
		} else {
			imagejpeg($dst_img,$filename); 
		}
		imagedestroy($dst_img); 
		imagedestroy($src_img); 
	}

Link to comment
Share on other sites

It is working now.  My first code was correct.  My issue was in file naming (outside of this script).  My image url kept pointing to the original thumbnail with the background instead of the new one being created (which had been renamed).  Thanks for the help though...

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.