Hello,

I was told to post this here:

When rotating an image with imagerotate, you have to specify a background color to fill in the uncovered zone after the rotation. When working with single-color transparency, this is not a problem. You just specify the color returned from imagecolortransparent. Well, this doesn't work. When doing something like this:
PHP Code:
$image imagecreatefromgif('diamond.gif');
$bg imagecolortransparent($image);
$image imagerotate($image45$bg);
imagecolortransparent($image$bg); 
The transparency and the uncovered zone gets black, with a strange border around the original rotated image. I found that a way to fix this, was to create a truecolor image, fill it with the transparent color of the original image over, copy the original image over to the new truecolor image, rotate the new truecolor image, and set the transparent color of the new rotated image to the transparent color of the original image. Like this:
PHP Code:
$simage imagecreatefromgif('diamond.gif');

$w imagesx($simage);
$h imagesy($image);
$bg imagecolortransparent($image);

$timage imagecreatetruecolor($w$h);
imagefill($timage00$bg);
imagecopy($timage$simage0000$w$h);

$image imagerotate($timage45$bg);
imagecolortransparent($image$bg); 
Now this works, a bit bothersome, but it works. Now what if the source image ($simage) is a png image with alpha transparency? Since alpha transparency is not based on a specific color, I cannot use imagecolortransparent to find the fill image. Ok, perhaps I could allocate a color with imagecolorallocatealpha, setting the alpha to 127 (completely translucent):
PHP Code:
$simage imagecreatefrompng('diamond.png');
imagealphablending($simagetrue);
imagesavealpha($simagetrue);

$bg imagecolorallocatealpha($simage000127);
$image imagerotate($simage45$bg);
imagealphablending($imagetrue);
imagesavealpha($imagetrue); 
Nope, still won't work. But if I try to set $bg as the transparent color, like this:
PHP Code:
$simage imagecreatefrompng('diamond.png');
imagealphablending($simagetrue);
imagesavealpha($simagetrue);

$bg imagecolorallocatealpha($simage000127);

$image imagerotate($simage45$bg);
imagecolortransparent($image$bg); 
It works! Who-who! Or so it seems. Because what really just happened here is that imagerotate, forgetting all about transparency, converts $bg to a non-alpha color (in this case, black), and fills the uncovered zone with this color. So if the image contains some black, those areas will also get transparent. NOT wanted. So this is where I'm stuck. Copying the original image over to another truecolor image (as in the GIF example) doesn't work either. Same result as the above.

So, can anyone help me out here? I'm stuck! I'm beginning to get pretty angry at the imagerotate function, which is messing with my alpha channel... Also, anyone know any better solutions instead of copying over the original image to a truecolor one?

Thank you,

- joh