问题是,当使用imagettfbbox
计算文本尺寸时,输入文本以数字开头时会返回太小的矩形。这是我的代码:imagettfbbox在文本以数字开头时计算错误的矩形
$fontSize = 150;
$font = "font/courier_new.ttf";
$text = $_GET["text"];
//Determine font dimensions
$bbox = imagettfbbox($fontSize, 0, $font, $text);
$bbox["width"]= abs($bbox[4]-$bbox[0]);
$bbox["height"]= abs($bbox[5]-$bbox[1]);
$im = imagecreatetruecolor($bbox["width"], $bbox["height"]);
echo "<b>Image size:</b>\n";
print_r($bbox);
// This part makes transparent background
imagesavealpha($im, true);
$bg = imagecolorallocatealpha($im, 254, 254, 254,127);
$text_color= imagecolorallocate($im, 0, 0, 0);
imagealphablending($im, false);
imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), $bg);
imagealphablending($im, true);
header("X-Img-Size: ".$bbox["width"]."x".$bbox["height"]."");
imagettftext($im, $fontSize, 0, 0, $bbox["height"], $text_color, $font, $text);
// This is output
header("Content-Type: text/html");
ob_start();
imagepng($im);
$image_data = ob_get_contents();
ob_end_clean();
imagedestroy($im);
echo "<img src=\"data:image/png;base64,".base64_encode($image_data)."\" />";
这些结果我得到不同的输入文本:
我该如何解决这个问题?
这似乎是一个已知的问题;请参阅http://php.net/manual/en/function.imagettfbbox.php,尤其是。 'peterjwest3'回答了一个建议的修复。函数的作者必须采取一些捷径......它似乎使用['FT_BBox'](http://www.freetype.org/freetype2/docs/reference/ft2-basic_types.html#FT_BBox),除了这*会*“正确地赋予字形的下行”。 – usr2564301
@TomášZato - 你去过http://php.net/manual/en/function.imagettfbbox.php并阅读了建议的解决方法,特别是。从8年前的“mikeleigh dot com”迈克尔?如果是这样,有什么理由,他建议的解决方法/修复不会解决/解决你遇到的问题? –
@BobJarvis我不能发誓我在那里使用了每种解决方法,但是这段代码已经应用了一些建议的代码 - 那些宽度和高度的等式来自该页面。我会尝试你提到的确切的解决方法。 –