PHP imagettftext总是绘制黑色

时间:2010-12-20 13:58:59

标签: php gd imagettftext

基本上,当我绘制文字时,它会像这样结束黑色:http://i.stack.imgur.com/z675F.png 而不是我在PHP和功能中分配的颜色。 代码:

    $finalImage = imagecreatefrompng($imageFile);
    $logo = imagecreatefrompng($logoImage);
    imagecopy($finalImage, $logo, $logoPosition['x'], $logoPosition['y'], 0, 0, imagesx($logo), imagesy($logo));
    $font = "arial.ttf";
    $fontSize = 10;
    $yOffSet = 15;
    $white = imagecolorallocate($finalImage, 255, 255, 255);
    foreach($pixelArray as $key => $x) {
        foreach($valueArray[$key] as $valueText) {

            imagettftext($finalImage, $fontSize, 0, $x, $yOffSet, $white, $font, $valueText);
            $yOffSet += 15;
        }
        $yOffSet = 15;
    }
    if($miscText != null) {
        foreach($miscText as $key => $text) {
            imagettftext($finalImage, $fontSize, 0, $text['x'], $text['y'], $white, $font, $text['text']);    
        }
    }
    imagepng($finalImage,$saveFileName.".png");
    imagedestroy($finalImage);

以前工作过,但之后就停止了,我不知道为什么。这是在我更改源图像(生成正常)之后,我没有触及代码。我已经尝试过改变颜色的各种各样的东西,但我不能让它显示在黑色以外的任何东西。

3 个答案:

答案 0 :(得分:3)

您是否检查过imagecolorallocate()函数是否返回布尔值假,如果分配失败,它会不会?如果$ finalImage .png是8位,并且您的纯白色不在源图像的调色板中,则此调用将失败。你说你改变了源图像,所以这很可能就是为什么它现在已经坏了。

$white = imagecolorallocate($finalImage, 255, 255, 255);
if ($white === FALSE) { // note the === -> strict type comparison
    die("Failed to allocate color 255/255/255")
}

该函数还将返回一个代表颜色三元组的数字,在这种情况下它将为0xFFFFFF。您可以尝试直接将其传递到imagegetttftext()来电,看看是否有帮助。

答案 1 :(得分:1)

通过将imagecolorallocate更改为imagecolorclosest来修复它,因为我已经在我复制的徽标上添加了一些白色文本。

答案 2 :(得分:0)

使用 imagecreatetruecolor()分配颜色:

$width = 500; //whatever width you need
$height = 200; //whatever height you need
$white = imagecolorallocate(imagecreatetruecolor($width, $height), 255, 255, 255);

这对我有用。

相关问题