I am trying to create a transparent image canvas and then place other random images on top of this canvas. I then finally save the final image as a gif. I have tried the below:
$canvas = imagecreatetruecolor($this->canvas_width, $this->canvas_height);
imagesavealpha($canvas, true);
imagealphablending($canvas, false);
$trans_colour = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
imagefill($canvas, 0, 0, $trans_colour);
However, if there is some unused space left on the canvas then this part is black. I thought this area should be transparent?
Am I applying transparency correctly with the above?
imagesavealphacan not be used for images you eventually intend to save as GIF because GIF images do not have alpha channels. From the docs:So it only works if you are going to save the image as PNG. GIF images can do transparency, but not with an alpha channel; they can only be “transparent” or “not transparent” by defining a particular color as transparent. In PHP you can do that using imagecolortransparent. For example, to make all black transparent, you could do: