4

基本上,当我绘制文本时,它会变成这样的黑色: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);

它以前可以工作,但后来就停止了,我不知道为什么。那是在我更改了源图像(生成良好)并且我没有触及代码之后。我已经尝试了各种改变颜色的方法,但我无法让它以黑色以外的任何方式显示。

4

3 回答 3

3

您是否检查过imagecolorallocate()函数是否返回布尔值 false,就像分配失败时一样?如果 $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()调用中,看看是否有帮助。

于 2010-12-20T14:11:05.153 回答
2

imagecolorallocate通过更改为修复它,imagecolorclosest因为我在复制的徽标上已经有一些白色文本:

//       imagecolorallocate....
$white = imagecolorclosest($im, 255, 255, 255); 
于 2010-12-20T14:20:48.757 回答
0

使用imagecreatetruecolor()分配颜色:

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

这对我有用。

于 2016-09-13T11:59:54.977 回答