PHP GD imagecolorallocatealpha只生成灰色文本

PHP GD imagecolorallocatealpha只生成灰色文本,php,gd,Php,Gd,imagecolorallocatealpha()只会使文本变为灰色,这有什么原因吗 <?php header('Content-Type: image/png'); function checkImg($imgname) { $im = @imagecreatefrompng($imgname); if(!$im) { $im = imagecreatetruecolor(150, 30); $bgc = imagecolorallocate($im, 25

imagecolorallocatealpha()只会使文本变为灰色,这有什么原因吗

<?php
header('Content-Type: image/png');

function checkImg($imgname) {
    $im = @imagecreatefrompng($imgname);

if(!$im) {
    $im  = imagecreatetruecolor(150, 30);
    $bgc = imagecolorallocate($im, 255, 255, 255);
    $tc  = imagecolorallocate($im, 0, 0, 0);

    imagefilledrectangle($im, 0, 0, 150, 30, $bgc);

    imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
}

return $im;
}

$hr = 48;

$tOne = "VALID FOR";
$tTwo = $hr." HOURS";

$img = checkImg('img.png');

$font = 'helr67w.ttf';
$size = 9;

$red = imagecolorallocatealpha($img, 255, 0, 0, 75);

imagettftext($img, $size, 0, 225, 132, $red, $font, $tOne);
imagettftext($img, $size, 0, 225, 144, $red, $font, $tTwo);

imagepng($img);
imagedestroy($img);
?>

在代码中,您没有将图像设置为支持alpha通道。我可以想象这是问题的根源:

function checkImg($imgname) {
    $im = @imagecreatefrompng($imgname);

    if(!$im) {
        $im  = imagecreatetruecolor(150, 30);
        $bgc = imagecolorallocate($im, 255, 255, 255);
        $tc  = imagecolorallocate($im, 0, 0, 0);
        imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
        imagestring($im, 1, 5, 5, 'Error loading ' . $imgname, $tc);
    }

    // Turn off alpha blending and set alpha flag
    imagealphablending($im, true);
    imagesavealpha($im, true);
    return $im;
}

请参见和

请添加
var\u dump($red)
并告诉它是哪个值。检查仍然不起作用。这里是一个屏幕截图,即使设置了alpha标志:文本仍然是灰色的。
$red
的内容是什么?请检查您的GD库版本,并检查您的浏览器是否支持带有alpha通道的PNG。我没有问题让你的原始代码工作,将检查我的修改。你在第一位是正确的。我的问题是客户给了我一个8比特的PNG。我保存为truecolor,效果很好。谢谢。@Chris Ensell:很酷,它很有帮助,将更新代码以使其正确,我已交换了一个布尔值(false而不是true)。