2016-04-29 24 views
15

问题是,当使用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)."\" />"; 

Online test here

这些结果我得到不同的输入文本:

image description

image description

image description

我该如何解决这个问题?

+2

这似乎是一个已知的问题;请参阅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

+1

@TomášZato - 你去过http://php.net/manual/en/function.imagettfbbox.php并阅读了建议的解决方法,特别是。从8年前的“mikeleigh dot com”迈克尔?如果是这样,有什么理由,他建议的解决方法/修复不会解决/解决你遇到的问题? –

+0

@BobJarvis我不能发誓我在那里使用了每种解决方法,但是这段代码已经应用了一些建议的代码 - 那些宽度和高度的等式来自该页面。我会尝试你提到的确切的解决方法。 –

回答

6

这个问题是由误解造成的。 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); 
+0

伟大的你自我回答似乎是一个专家水平的问题。谢谢! –

+0

@StijndeWitt尽管我没有对我的问题发表评论,但我不会想到它。 –