2 Replies - 988 Views - Last Post: 28 March 2013 - 07:56 AM Rate Topic: -----

#1 codabra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-August 12

Problem with re-sizing transparent images

Posted 28 March 2013 - 06:37 AM

This problem seems to be pretty common but none of the tutorials I've read have seemed to fix my issue ( or i just don't really know what I'm doing ).

The Problem :
When re-sizing transparent images the images background turns black.

What i thought were two possible solutions
imagealphablending($thumbnail, false);
imagesavealpha($thumbnail, true);


and or

imagecolortransparent($temp_image, imagecolorallocatealpha($temp_image, 0, 0, 0, 127));
imagealphablending($thumbnail, false);
imagesavealpha($thumbnail, true);



which i put the code here nothing changes.

if($type == "image/jpeg") {
		header('Content-type: image/jpeg');
		$thumb = imagecreatefromjpeg($image);
	} elseif($type == "image/png") {
		header('Content-type: image/png');
		$thumb = imagecreatefrompng($image);
	} else {
		return false;
	}

	$temp_image = imagecreatetruecolor($width, $height);
	imagecopyresampled($temp_image, $thumb, 0, 0, 0, 0, $width, $height, $image_width, $image_height);	

	$thumbnail = imagecreatetruecolor($width, $height);	
	imagecopyresampled($thumbnail, $temp_image, 0, 0, 0, 0, $width, $height, $width, $height);

        //Code placed here

	imagealphablending($thumbnail, false);
	imagesavealpha($thumbnail, true);	
	
	if($type == "image/jpeg") {
		imagejpeg($thumbnail);
	} else {
		imagepng($thumbnail);		
	}

	imagedestroy($temp_image);
	imagedestroy($thumbnail);

}	



If i place the code above "imagecopyresampled" the images are broken. Same with the code that uses "imagecolortransparent"

I also thought maybe it's not $thumbnail - so i tried $image and $thumb as a replacement.
Where should i be placing this transparency fix? I read you should place it after the new image was declared.

Any help on this situation would be greatly appreciated. I know there other scripts out there, but i'd like to stick with my current.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with re-sizing transparent images

#2 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6180
  • View blog
  • Posts: 10,683
  • Joined: 28-September 07

Re: Problem with re-sizing transparent images

Posted 28 March 2013 - 07:38 AM

imagecreatetruecolor is causing your black background. Have you tried using imagesavealpha immediately after imagecreatetruecolor, before the resampling?

$temp_image = imagecreatetruecolor($width, $height);
imagesavealpha($temp_image, true); 
imagecopyresampled($temp_image, $thumb, 0, 0, 0, 0, $width, $height, $image_width, $image_height); 



Or something to that effect?

That may or may not help your situation. Just tossing ideas out there.

It looks to me like you're taking your transparent image, giving it a black background, and trying to resample it with the transparent copy of itself... thus showing the black background of the resaved image. Does that make sense? My logic could be off here. I haven't had any coffee yet today.
Was This Post Helpful? 0
  • +
  • -

#3 codabra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 13-August 12

Re: Problem with re-sizing transparent images

Posted 28 March 2013 - 07:56 AM

It makes sense ( but i'm a noob so that doesn't say much . I tried something like that before ( and now again ) and no luck.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1