I have 2 PNG-24 images with transparency. What I want to do is take the first image (which is a jersey), adjust the color using GD, then take the 2nd image (jersey sleeves) and adjust the color using GD, then overlay image 2 on top of image 1. Everything works except when I overlay image 2 on image 1, the alpha is gone and is replaced by white. (If I don't colorize image 2, and instead lay it over image 1, then it works correctly, but of course isn't colorized like it needs to be.)
Here is the code I have right now:
// Compose 2 images
function image_compose_jersey( &$src, &$ovr, $ovr_x, $ovr_y)
{
imagecopy($src, $ovr, $ovr_x, $ovr_y, 0, 0, imagesx($ovr), imagesy($ovr) );
}
// Colorize an image using RGB values
function image_colorize($im_src, $r, $g, $b, $target_image)
{
$im_jersey = ImageCreate(imagesx($im_src),imagesy($im_src));
for ($c = 0; $c < 256; $c++)
{
ImageColorAllocate($im_jersey, max($r,$c), max($g,$c), max($b,$c));
}
ImageCopyMerge($im_jersey,$im_src,0,0,0,0, imagesx($im_src), imagesy($im_src), 100);
// Output new image
imagepng($im_jersey, $target_image);
}
// RGB values used to adjust color of image
$r = $GET['r'];
$g = $GET['g'];
$b = $_GET['b'];
// Open a blank bg
$photoImage = ImageCreateFromPNG('jersey_blank.png');
// Add the jersey
$jersey = ImageCreateFromPNG('jersey_neg.png');
$jersey_colorized = image_colorize($jersey, $r, $g, $b, 'jersey_colorized.png');
$jersey_colorizedb = ImageCreateFromPNG('jersey_colorized.png');
image_compose_jersey( $photoImage, $jersey_colorizedb, 0, 0 );
// Add the highlights
$highlights = ImageCreateFromPNG('highlights_neg.png');
$highlights_colorized = image_colorize($highlights, $r-50, $g, $b, 'highlights_colorized.png');
$highlights_colorizedb = ImageCreateFromPNG('highlights_colorized.png');
image_compose_jersey( $photoImage, $highlights_colorizedb, 0, 0 );
// Output final image
Imagepng($photoImage, 'jersey_final.png');
I'm relatively new to using the GD Library, so any tips would be much appreciated. Maybe there's a better way to do what I need to do, or at least a way that works fully. TIA.