Author Topic: GD transparency problem  (Read 866 times)

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
GD transparency problem
« on: November 28, 2009, 04:49:00 PM »
I load PNG image, then create temporary image, next copy part of the source image to the temporary image and finally save the temporary image as new PNG.

Everything works fine, except for transparency...


Quote
$ims=imagecreatefrompng($sourcefile);
imagealphablending($ims, true);
imagesavealpha($ims, true);

$imd=imagecreatetruecolor($imgw,$imgh);
imagealphablending($imd, true);
imagesavealpha($imd, true);

imagecopy($imd,$ims,0,0,$src_x,$src_y,$imgw,$imgh);

imagepng($imd,$filesave);

Offline Harkins

  • Level 28
  • **
  • Posts: 424
  • Reputation: +11/-2
  • Coder, blogger, entrepreneur.
    • View Profile
    • Push CX - Blog
Re: GD transparency problem
« Reply #1 on: November 28, 2009, 05:35:02 PM »
I think you may have to use imagesavealpha() when working with PNGs.

Visit #bbg on irc.freenode.net to talk browser games anytime.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: GD transparency problem
« Reply #2 on: November 28, 2009, 06:50:54 PM »
I think you may have to use imagesavealpha() when working with PNGs.
But I used it, check the code posted above. I even used it twice :D

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Creating online addictions, one game at a time:

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: GD transparency problem
« Reply #4 on: November 28, 2009, 07:10:04 PM »
BTW quirks like this in the GD library made me switch to using imagemagick

Two lines of code in IM will do the same thing :)

Creating online addictions, one game at a time:

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: GD transparency problem
« Reply #5 on: November 28, 2009, 07:12:04 PM »
http://www.php.net/manual/en/function.imagecolortransparent.php
Yes, but I load an image that already has transparency. I do not know which colour it is. I just want to load a big image with transparency and cut it into smaller pieces.

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: GD transparency problem
« Reply #6 on: November 28, 2009, 07:29:30 PM »
Yes but as the notes in the manual state there are other things to consider when trying to do this like what type of PNG file are you using. 8 bit then you need to define a color, 24/32 bit then there is a function someone wrote that will find the transparency value.

When you get into doing graphics like this there is more to it then just PNG/GIF, for instance, true color or a pallet image will dictate what you can do to the image and how easy it is to manipulate it.

Finally what GD lib are you using, as you said you use PHP4 so I'm not sure what's version of the GD lib came with 4.x which is also another thing to consider.



Creating online addictions, one game at a time:

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: GD transparency problem
« Reply #7 on: November 29, 2009, 04:33:18 AM »
... Geeez, I thought it is high level image manipulation library, I feel like I'm back to DirectX :D

OK, lower expectations then. All images are 8 bit.

Copied palette from source, but how to find which colour is transpoarent in the source palette (at the moment I'm reading from pixel 0,0 since it is transparrent in all my images, but would like to have something more flexible)?


Code: [Select]
$ims=imagecreatefrompng($sourcefile);
$imd=imagecreate($imgw,$imgh);

imagepalettecopy($imd,$ims);

echo " ColoursTotal[S]: ".imagecolorstotal($ims);
echo " ColoursTotal[D]: ".imagecolorstotal($imd);
echo "<br>";

// SET TRANSPARENT TO COLOUR FROM PIXEL (0,0)
if($gettransparentfrom00)
{
$rgb = imagecolorat($ims, 0, 0);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$trs = ImageColorAllocate($ims, $r, $g, $b);
$transparent_s=imagecolortransparent($ims,$trs);
$trs = ImageColorAllocate($imd, $r, $g, $b);
$transparent_d=imagecolortransparent($imd,$trs);
echo"Get transparent colour from (0,0) = $transparent_s and $transparent_d<br>";
}


imagecopy($imd,$ims,0,0,$src_x,$src_y,$imgw,$imgh);

imagepng($imd,$filesave);

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal