我使用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);
答案 0 :(得分:1)
可能是字体有问题,我用另一个测试你的代码,然后我得到了