I came across the problem of having a page where any image could be uploaded, then I would need to work with it as a true color image with transparency. The problem came with palette images with transparency (e.g. GIF images), the transparent parts changed to black (no matter what color was actually representing transparent) when I used imagecopy to convert the image to true color.
To convert an image to true color with the transparency as well, the following code works (assuming $img is your image resource):
<?php
$w = imagesx($img);
$h = imagesy($img);
if (!imageistruecolor($img)) {
$original_transparency = imagecolortransparent($img);
if ($original_transparency >= 0) {
$rgb = imagecolorsforindex($img, $original_transparency);
$original_transparency = ($rgb['red'] << 16) | ($rgb['green'] << 8) | $rgb['blue'];
imagecolortransparent($img, imagecolorallocate($img, 0, 0, 0));
}
$truecolor = imagecreatetruecolor($w, $h);
imagealphablending($img, false);
imagesavealpha($img, true);
imagecopy($truecolor, $img, 0, 0, 0, 0, $w, $h);
imagedestroy($img);
$img = $truecolor;
if ($original_transparency >= 0) {
imagealphablending($img, false);
imagesavealpha($img, true);
for ($x = 0; $x < $w; $x++)
for ($y = 0; $y < $h; $y++)
if (imagecolorat($img, $x, $y) == $original_transparency)
imagesetpixel($img, $x, $y, 127 << 24);
}
}
?>
And now $img is a true color image resource