Seems imagecolorallocatealpha
's alpha parameter is non-standard. Uses 0 to 127, where 127 is fully-transparent.
Color-keying image not working in PHP
-
11-04-2022 - |
문제
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');
해결책
다른 팁
Your attepnt #1 looks good to me, just don't touch the alphablending.
To avoid matters with transparency, I'm used to set it just after creating my image :
$image = imagecreatetruecolor($width, $height);
$trans = imagecolorallocate($image, 0xFF, 0x0, 0xFF);
imagefill($image, 0, 0, $trans);
imagecolortransparent($image, $trans);
Here, I'm sure my whole image background is transparent and I just need to put my stuffs in it.
제휴하지 않습니다 StackOverflow