从使用imagepng()创建的PNG获取Alpha

P. *_*ank 10 php png

我试着获得alpha一个PNG.我正在这样做imagepng().我的问题是alpha只返回0.

我使用alpha制作PNG的代码

$x = 1;
$y = 1;
$gd = imagecreatetruecolor($x, $y); 
imagesetpixel($gd, 0,0, imagecolorallocatealpha($gd, 200,200,200,1));
imageAlphaBlending($gd, false);
imageSaveAlpha($gd, true);
imagepng($gd,"test.png");
imagedestroy($gd);
Run Code Online (Sandbox Code Playgroud)

我的阅读PNG alpha的代码

$im = imagecreatefrompng("test.png");
$rgb = imagecolorat($im, 0, 0);
$colors = imagecolorsforindex($im, $rgb);
$red = (int) $colors["red"]; 
$blue = (int) $colors["blue"];
$green = (int) $colors["green"]; 
$alpha = (int) $colors["alpha"]; // return only 0
Run Code Online (Sandbox Code Playgroud)

我不知道为什么它只返回0而不是1.

nap*_*zba 7

你应该调用imageAlphaBlendingimageSaveAlpha调用之前imagesetpixel:

imageAlphaBlending($gd, false);
imageSaveAlpha($gd, true);
imagesetpixel($gd, 0,0, imagecolorallocatealpha($gd, 200,200,200,1));
Run Code Online (Sandbox Code Playgroud)