1

When I perform and image overlay in php using the GD library, I always get a black background, however, all the images overlay correctly. Can someone help?

<?php

    $images = array( $_GET['color'], $_GET['face'], $_GET['hat'] );


    $img = imagecreatetruecolor(58, 75);

    imagealphablending($img, true);
    imagesavealpha($img, true);

    imagecolorallocate($img, 255, 205, 255);
    imagecolorallocate($img, 255, 255, 255);
    imagecolortransparent($img, $white);

imagefilledrectangle($img, 0, 0, $imgWidth, $imgHeight, $white);
    foreach($images as $fn) {

        $cur = imagecreatefrompng($fn);
        imagealphablending($cur, true);
        imagesavealpha($cur, true);


        imagecopy($img, $cur, 0, 0, 0, 0, 58, 75);


        imagedestroy($cur);
    }   


    header('Content-Type: image/png');
    imagepng($img);

?>
Seth
  • 2,043
  • 5
  • 20
  • 23

2 Answers2

0
// Create an image

$img = imagecreatetruecolor($imgWidth, $imgHeight);

$white = imagecolorallocate($img, 255, 255, 255);

// Make the background white

imagefilledrectangle($img, 0, 0, $imgWidth, $imgHeight, $white);

...could help.

Ilyssis
  • 4,849
  • 7
  • 24
  • 30
0

This is a common problem, and the answer is already available on stack overflow; the answer there fixes the problem perfectly. You may want to try searching harder :)

I would suggest that you can make your life easier by using the vastly more powerful (but unfortunately poorly documented) imagick library if you're going to try to do anything more than the most basic image manipulation; it's faster, easier (again, once you get past the documentation) and more powerful.

Community
  • 1
  • 1
El Yobo
  • 14,823
  • 5
  • 60
  • 78
  • I have been searching for 3 days, and nothing seemed to work. – Seth Dec 12 '10 at 14:27
  • @Seth: As noted in the discussion El Yobo linked, you should allocate a dummy color before allocating the white as you cannot use the first allocated color as transparency. Also, you use `$white` in your code before defining it. – nico Dec 12 '10 at 14:38