...

View Full Version : Transparency lost when using imagecopy in PHP



nathanelmori
10-18-2009, 12:34 AM
Hi

I am desperately trying to create an animated gif from two png files.

I found a great class that does this, and it works fine, except for the transparency, which is lost.

So I decided to narrow down the whole thing to try and locate the origin of the problem, and - lo and behold - the problem has nothing to do with the animation, but with the gd library.

In short, I can't even create an image from png and display it.

Here's a simple version of the code:


<html>

<body bgcolor="red">
<?php

$im = imagecreatefrompng("thepic.png");
imagealphablending($im, true);
imagesavealpha($im, true);


$test = imagecreatetruecolor(imagesx($im), imagesy($im));
imagealphablending($test, true);
imagesavealpha($test, true);
imagecopy($test, $im, 0, 0, 0, 0, imagesx($test), imagesy($test));


header('content-type: image/png');
imagepng($test, "test.png");

?>
<img src=test.png>
</body>

</html>

For some reason, the variable $test completely lost the original transparency (showing the default 'black' instead of the original transparent colors of the source), although I turned all the possible tags. However, $im still has the transparency and saving it into a file and showing it would work wonders. Alas, the reason I'm stuck in this conundrum is because I need to copy a part of $im into $test, and I can't complete that without losing the transparency.

HELP!! PLEASE!!!

barkermn01
10-18-2009, 12:37 AM
Hi

I am desperately trying to create an animated gif from two png files.

I found a great class that does this, and it works fine, except for the transparency, which is lost.

So I decided to narrow down the whole thing to try and locate the origin of the problem, and - lo and behold - the problem has nothing to do with the animation, but with the gd library.

In short, I can't even create an image from png and display it.

Here's a simple version of the code:


<html>

<body bgcolor="red">

<?php

$im = imagecreatefrompng("thepic.png");
imagealphablending($im, true);
imagesavealpha($im, true);


$test = imagecreatetruecolor(imagesx($im), imagesy($im));
imagealphablending($test, true);
imagesavealpha($test, true);
imagecopy($test, $im, 0, 0, 0, 0, imagesx($test), imagesy($test));


header('content-type: image/png');
imagepng($test, "test.png");

?>
<img src=test.png>
</body>

</html>

For some reason, the variable $test completely lost the original transparency (showing the default 'black' instead of the original transparent colors of the source), although I turned all the possible tags. However, $im still has the transparency and saving it into a file and showing it would work wonders. Alas, the reason I'm stuck in this conundrum is because I need to copy a part of $im into $test, and I can't complete that without losing the transparency.

HELP!! PLEASE!!!

i think this might be a class limitation,

check the php.net/gd stuff for it sorry i cant be more help i don't do GD library



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum