The input image has a magenta background.
The outputted image always has a black background.
Both attempts give same results. I want it to be alpha background, not black background.
Attempt #1:
imagesavealpha($image, true);
imagealphablending($image, false);
$trans = imagecolorallocate($image, 255, 0, 255);
imagecolortransparent($image, $trans);
imagepng($image, $label . '.png');
Attempt #2:
imagesavealpha($image, true);
imagealphablending($image, false);
$trans = imagecolorallocatealpha($image, 0, 0, 0, 0);
for ($x = 0; $x < imagesx($image); ++$x) {
for ($y = 0; $y < imagesy($image); ++$y) {
$rgb = imagecolorat($image, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if ($r == 255 && $g == 0 && $b == 255) {
imagesetpixel($image, $x, $y, $trans);
}
}
}
imagepng($image, $label . '.png');