这个问题是由误解造成的. imagettfbbox的值也定义了你必须从哪里开始绘制 – 通常这些坐标甚至是负的.我总是假设你可以从[0,0]坐标开始.这不是真的,绘图坐标可以是负的.
此功能也在评论中提到并源自PHP.net用户贡献计算开始坐标以及宽度和高度(在相关代码中是正确的).
// Source: http://php.net/manual/en/function.imagettfbbox.php#75407
function imagettfbboxextended($size, $angle, $fontfile, $text) {
/*this function extends imagettfbbox and includes within the returned array
the actual text width and height as well as the x and y coordinates the
text should be drawn from to render correctly. This currently only works
for an angle of zero and corrects the issue of hanging letters e.g. jpqg*/
$bbox = imagettfbbox($size, $angle, $fontfile, $text);
//calculate x baseline
if($bbox[0] >= -1) {
$bbox['x'] = abs($bbox[0] + 1) * -1;
} else {
//$bbox['x'] = 0;
$bbox['x'] = abs($bbox[0] + 2);
}
//calculate actual text width
$bbox['width'] = abs($bbox[2] - $bbox[0]);
if($bbox[0] < -1) {
$bbox['width'] = abs($bbox[2]) + abs($bbox[0]) - 1;
}
//calculate y baseline
$bbox['y'] = abs($bbox[5] + 1);
//calculate actual text height
$bbox['height'] = abs($bbox[7]) - abs($bbox[1]);
if($bbox[3] > 0) {
$bbox['height'] = abs($bbox[7] - $bbox[1]) - 1;
}
return $bbox;
}
但是当绘制时,您必须使用此函数给出的x和y坐标:
imagettftext($im, $fontSize, 0, $bbox["x"], $bbox["y"], $text_color, $font, $text);