Author Topic: php GD problems with transparency.  (Read 1280 times)

Offline Sim

  • Level 13
  • *
  • Posts: 104
  • Reputation: +1/-1
    • View Profile
    • Online RPG Creator
php GD problems with transparency.
« on: February 10, 2011, 09:54:01 AM »
I am trying to overlap these images to create a new character. I am having problems with my transparency stuff...

Code:

Code: [Select]
<?php
session_start
();


//body is never empty
$im imagecreatefrompng("images/chars/body" str_replace(' ''',$_SESSION['body'] . ".png"));

//set pixel color for transparent
$green imagecolorallocate($im511530);

// Make the background transparent
imagecolortransparent($im$green);

//bottom
if(!empty($_SESSION['bot']))
{
$newImage imagecreatefrompng("images/chars/bot" str_replace(' ''',$_SESSION['bot']) . ".png");
//set pixel color for transparent
$green imagecolorallocate($newImage511530);

// Make the background transparent
imagecolortransparent($newImage$green);

imagecopymerge($newImage$im000048640);
}

//top
if(!empty($_SESSION['top']))
{
if(!empty($_SESSION['bot']))
{
$newImage imagecreatefrompng("images/chars/top" str_replace(' ''',$_SESSION['top']) . ".png");
//set pixel color for transparent
$green imagecolorallocate($newImage511530);

// Make the background transparent
imagecolortransparent($newImage$green);

// Copy and merge
imagecopymerge($newImage$im000048640);

// Content type
header('Content-type: image/png');
imagepng($newImage);
}
else
{
$newImager imagecreatefrompng("images/chars/bot" str_replace(' ''',$_SESSION['bot']) . ".png");
//set pixel color for transparent
$green imagecolorallocate($newImager511530);

// Make the background transparent
imagecolortransparent($newImager$green);

imagecopymerge($newImager$im000048640);

// Content type
header('Content-type: image/png');
imagepng($newImager);
}
}



?>


Offline Nox

  • Level 35
  • **
  • Posts: 767
  • Reputation: +12/-2
    • View Profile
Re: php GD problems with transparency.
« Reply #1 on: February 10, 2011, 11:21:32 AM »
Meet us at an IRC irc.freenode.net #bbg as well
https://vimeo.com/36579366 (a must-watch) | Join BOINC - no longer a hype, but you can help never the less

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: php GD problems with transparency.
« Reply #2 on: February 10, 2011, 11:23:51 AM »
I need more information.  I'm guessing you're trying to place the head/armor on the generic body?  What results are you getting when you try to make the switch?  Is it throwing an error?  Or, are you trying to obtain some sort of 'transparency' as stated in your post?  

Offline Sim

  • Level 13
  • *
  • Posts: 104
  • Reputation: +1/-1
    • View Profile
    • Online RPG Creator
Re: php GD problems with transparency.
« Reply #3 on: February 11, 2011, 07:24:14 AM »
Still having problems...
Doesn't keep my colors from all 3 images. It like lightens them.

http://phpengines.info/tactical/indexcreate.php

Code: [Select]
<?php
session_start
();

$rgb "32, 156, 0";

//body is never empty
$im imagecreatefrompng("images/chars/body" str_replace(' ''',$_SESSION['body'] . ".png"));


//set pixel color for transparent
$green imagecolorallocate($im321560);

// Make the background transparent
imagecolortransparent($im$green);

// Turn off alpha blending and set alpha flag
imagealphablending($imfalse);
imagesavealpha($imtrue);
//bottom
if(!empty($_SESSION['bot']))
{
$newImage imagecreatefrompng("images/chars/bot" str_replace(' ''',$_SESSION['bot']) . ".png");
//set pixel color for transparent
$green imagecolorallocate($newImage321560);

// Make the background transparent
imagecolortransparent($newImage$green);

// Turn off alpha blending and set alpha flag
imagealphablending($newImagefalse);
imagesavealpha($newImagetrue);
imagecopymerge($im$newImage0000486475);

//set pixel color for transparent
$green imagecolorallocate($im321560);

// Make the background transparent
imagecolortransparent($im$green);
}


//top
if(!empty($_SESSION['top']))
{
$newImager imagecreatefrompng("images/chars/top" str_replace(' ''',$_SESSION['top']) . ".png");
//set pixel color for transparent
$green imagecolorallocate($newImager3215675);

// Make the background transparent
imagecolortransparent($newImager$green);
// Turn off alpha blending and set alpha flag
imagealphablending($newImagerfalse);
imagesavealpha($newImagertrue);
imagecopymerge($im$newImager0000486475);
}

// Content type
header('Content-type: image/png');
imagepng($im);

?>
« Last Edit: February 11, 2011, 07:28:07 AM by Sim »

Offline CygnusX

  • Level 24
  • *
  • Posts: 303
  • Reputation: +3/-2
    • View Profile
    • Lords of Midnight
Re: php GD problems with transparency.
« Reply #4 on: February 11, 2011, 07:37:37 AM »
2 things:

1) you only need to declare the value of $green once.  Doing it twice is not good practice.

2)  I suggest reading through all the comments on php's site for the functions you're using.  There seem to be many problems with transparencies, alpha bleeding, etc.  I can't 'see' what your image looks like, so its hard to diagnose the problem. 

Offline Sim

  • Level 13
  • *
  • Posts: 104
  • Reputation: +1/-1
    • View Profile
    • Online RPG Creator
Re: php GD problems with transparency.
« Reply #5 on: February 11, 2011, 07:54:33 AM »
Since my last post, i was trying to make it work so you may not have seen what it originally looked like. I have read  through all the function's i been using several times. I just can't figure it out. below is what the image looks like.

Offline Chris

  • Game Owner
  • Level 35
  • *
  • Posts: 2,217
  • Reputation: +28/-1
    • View Profile
Re: php GD problems with transparency.
« Reply #6 on: February 11, 2011, 11:22:11 AM »
Try converting all images to 24bit. GD has problems if you mix palette based images and true colour images.

Generally, GD has flaws, it does not give you errors when you do something "wrong" and just do not do it instead. The worst things is that these "wrong" things are not intuitive (like palette images behaviour).

Offline codestryke

  • Administrator
  • Level 33
  • *****
  • Posts: 589
  • Reputation: +22/-0
    • View Profile
    • eXtremeCast Games
Re: php GD problems with transparency.
« Reply #7 on: February 11, 2011, 01:17:10 PM »
Code: [Select]
<?php
  $imgOutput 
imagecreatetruecolor(4864);
  
imagefill($imgOutput000);

  
$imgBody imagecreatefrompng("body.png");
  
imagecolortransparent($imgBodyimagecolorat($imgBody00));

  
$imgBeard imagecreatefrompng("beard.png");
  
imagecolortransparent($imgBeardimagecolorat($imgBeard00));

  
$imgArmour imagecreatefrompng("armour.png");
  
imagecolortransparent($imgArmourimagecolorat($imgArmour00));
  
  
imagecopymerge($imgOutput$imgBody00004864100);
  
imagecopymerge($imgOutput$imgBeard00004864100);
  
imagecopymerge($imgOutput$imgArmour00004864100);

  
imagepng($imgOutput'./imagecolortransparent.png');
  
imagedestroy($imgOutput);
  exit;
?>


This will output the result to a black background, if you would like that changed to a different color or image change the first two lines that define the output. I did not define a var for the transparency color as they may be slightly different across the images.

« Last Edit: February 11, 2011, 01:19:18 PM by codestryke »
Creating online addictions, one game at a time:

Offline Mufasa

  • Game Owner
  • Level 18
  • *
  • Posts: 189
  • Reputation: +3/-0
  • Maniac Developer
    • View Profile
Re: php GD problems with transparency.
« Reply #8 on: February 11, 2011, 05:11:48 PM »
I know it's not a solution to the GD problem, but we've moved to Image Magick for all our processing needs. SO. Much. Better. If you CAN do it, I'd highly recommend it.

Offline Sim

  • Level 13
  • *
  • Posts: 104
  • Reputation: +1/-1
    • View Profile
    • Online RPG Creator
Re: php GD problems with transparency.
« Reply #9 on: February 11, 2011, 05:46:05 PM »
I know it's not a solution to the GD problem, but we've moved to Image Magick for all our processing needs. SO. Much. Better. If you CAN do it, I'd highly recommend it.

I'm having problems with Image Magick and png's too. Not the same problems, but when trying to use png's for animated gif's.

Offline Sim

  • Level 13
  • *
  • Posts: 104
  • Reputation: +1/-1
    • View Profile
    • Online RPG Creator
Re: php GD problems with transparency.
« Reply #10 on: February 11, 2011, 05:57:18 PM »
works like a charm!!! Thanks coderstryke.

Code: [Select]
<?php
  $imgOutput 
imagecreatetruecolor(4864);
  
imagefill($imgOutput000);

  
$imgBody imagecreatefrompng("body.png");
  
imagecolortransparent($imgBodyimagecolorat($imgBody00));

  
$imgBeard imagecreatefrompng("beard.png");
  
imagecolortransparent($imgBeardimagecolorat($imgBeard00));

  
$imgArmour imagecreatefrompng("armour.png");
  
imagecolortransparent($imgArmourimagecolorat($imgArmour00));
  
  
imagecopymerge($imgOutput$imgBody00004864100);
  
imagecopymerge($imgOutput$imgBeard00004864100);
  
imagecopymerge($imgOutput$imgArmour00004864100);

  
imagepng($imgOutput'./imagecolortransparent.png');
  
imagedestroy($imgOutput);
  exit;
?>


This will output the result to a black background, if you would like that changed to a different color or image change the first two lines that define the output. I did not define a var for the transparency color as they may be slightly different across the images.



 


SimplePortal 2.3.3 © 2008-2010, SimplePortal