2016-04-29 2 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、espを参照してください。 '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にアクセスして、推奨される回避策espを読みましたか? 8年前から「mike at mikeleighドットコム」のもの?もしあなたの提案された回避策/修正が回避できない/あなたが持っている問題を修正するいくつかの理由がありますか? –

+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私は私の質問にコメントなしでそれを理解していないでしょう。 –

関連する問題