PHP/GD – transparent background

imagecolortransparent is probably not what you want here if you’re merging images, as single-colour transparency is nasty.

Instead, try it with a transparent fill mask like so:

<?php
$image = imagecreatetruecolor(100, 100);

// Transparent Background
imagealphablending($image, false);
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
imagefill($image, 0, 0, $transparency);
imagesavealpha($image, true);

// Drawing over
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 25, 25, 75, 75, $black);

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

Leave a Comment