2

我正在使用 imagettftext 来渲染 PNG 文件。对 imagettftext() 的调用会返回渲染文本的边界框,但仔细观察,文本会稍微超出它自己的边界框!边界框是正确的(我检查了图像的像素坐标),但文本位置不正确,它输出这个,其中框是渲染文本后返回的边界框:

在此处输入图像描述

我的代码是:

// helper function for geting textbox bounds
function bounds($text,$fontFile,$fontSize,$fontAngle) { 
    $rect = imagettfbbox($fontSize,$fontAngle,$fontFile,$text); 
    $minX = min(array($rect[0],$rect[2],$rect[4],$rect[6])); 
    $maxX = max(array($rect[0],$rect[2],$rect[4],$rect[6])); 
    $minY = min(array($rect[1],$rect[3],$rect[5],$rect[7])); 
    $maxY = max(array($rect[1],$rect[3],$rect[5],$rect[7])); 

    return array( 
        "left"   => abs($minX) - 1, 
        "top"    => abs($minY) - 1, 
        "width"  => $maxX - $minX, 
        "height" => $maxY - $minY, 
        "box"    => $rect 
    ); 
}

$canvas = @imagecreate(640, 680)
    or die('Cannot Initialize new GD image stream');

$title_color = imagecolorallocate($canvas, 153, 153, 153);
$content_color = imagecolorallocate($canvas, 51, 51, 51);

$content_bounds = bounds("12", "Helvetica_Reg.ttf", 75, 0);
$test = imagettftext($canvas, 75, 0, 30, 200, $content_color, "Helvetica_Reg.ttf", "12");
imagerectangle($canvas, $test[0], $test[1], $test[4], $test[5], $title_color);
4

1 回答 1

1

可能是字体有问题,我用另一个测试你的代码,我得到了

在此处输入图像描述

于 2014-07-25T15:55:08.583 回答