PHP imagettftext返回边界框与渲染边界框不同
我正在使用imagettftext渲染PNG文件。对imagettftext()的调用将返回文本在其中呈现的边界框,但仔细检查后,文本将在其自己的边界框之外稍微呈现!边界框是正确的(我检查了图像的像素坐标),但是文本位置不正确,它会输出这个,其中框是渲染文本后返回的边界框: 我的代码是:PHP imagettftext返回边界框与渲染边界框不同,php,image,gd,imagettftext,Php,Image,Gd,Imagettftext,我正在使用imagettftext渲染PNG文件。对imagettftext()的调用将返回文本在其中呈现的边界框,但仔细检查后,文本将在其自己的边界框之外稍微呈现!边界框是正确的(我检查了图像的像素坐标),但是文本位置不正确,它会输出这个,其中框是渲染文本后返回的边界框: 我的代码是: // helper function for geting textbox bounds function bounds($text,$fontFile,$fontSize,$fontAngle) {
// 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);
可能是字体有问题,我用另一个测试了你的代码,我得到了