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);