views:

838

answers:

4

I have the following, it works except the gif is suppose to be transparent and it is creating a black background, if I change all the (imagesavealpha, etc) stuff to the $container then it created a transparent background for the text I add to this image. How would I go about getting rid of the black background? Basically this is a signature type image. I write stuff too, which I don't think you need to see.

  $im = imagecreatefromgif("bg.gif");

  $container = imagecreatetruecolor(400, 160);
  imagesavealpha($im, true);
  imagealphablending($im, false);

  $trans_colour = imagecolorallocatealpha($im, 0, 0, 0, 127);
  $w = imagecolorallocate($container, 255, 255, 255);

  imagefill($im, 0, 0, $trans_colour);

  imagecopymerge($container, $im, 0, 0, 0, 0, 460, 180, 100);
A: 

I use these two functions to allow you to create a new transparent truecolour image...

function image_createtruecolortransparent($x,$y) 
{
 $i = imagecreatetruecolor($x,$y);
 $b = imagecreatefromstring(base64_decode(image_blankpng()));
 imagealphablending($i,false);
 imagesavealpha($i,true);
 imagecopyresized($i,$b,0,0,0,0,$x,$y,imagesx($b),imagesy($b));
 return $i;
}

function image_blankpng()
{
 $c = "iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29m";
 $c .= "dHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAADqSURBVHjaYvz//z/DYAYAAcTEMMgBQAANegcCBNCg";
 $c .= "dyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAAN";
 $c .= "egcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQ";
 $c .= "oHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAADXoHAgTQoHcgQAANegcCBNCgdyBAAA16BwIE0KB3IEAA";
 $c .= "DXoHAgTQoHcgQAANegcCBNCgdyBAgAEAMpcDTTQWJVEAAAAASUVORK5CYII=";

 return $c;
}

Which should do what you need. Just replace your function call to imagecreatetruecolor(400, 160); with image_createtruecolortransparent(400, 160); and include both functions.

Meep3D
A: 

This page is very useful in the PHP online manual: http://us2.php.net/manual/en/function.imagecolortransparent.php#89927. I linked to a specific comment, but it's always a good idea to at least have a look at most of them.

Mr. Smith
A: 

Thanks Meep3D, but that reply does not address the question asked - how to preserve the transparent background in a GIF (NOT png) image.

A: