2013-06-26 74 views
0

我正在使用imagettftext()在圖像中寫入字母。問題是我不知道圖像中的字母位置是什麼。php imagettftext字體位置

enter image description here

正如你可以看到,我繪製的圖像,並使用imagettfbbox()信的邊界,但信的邊界是在左邊也當字母右邊。

所以問題是:我怎麼知道字母在哪裏開始(在x軸上)?每封信看起來都有不同的「偏移」,我只是想知道它畫的是黑色邊框。

這是我用來創建上面的鏈接的圖像的代碼:

//$w,$h,$bl(baseline) and $s taken from a text in an html page 

$string = "a"; 
$font = "font/times.ttf"; 
$h = 247; 
$w = 94; //106 for letter "b" 
$bl = 54; 

header('Content-type: image/png'); 
$im = imagecreatetruecolor($w, $h); 

$back = imagecolorallocate($im, 200, 200, 200); 
$asd = imagecolorallocate($im, 0, 255, 255); 

imagefilledrectangle($im, 0, 0, $w, $h, $back); 

$x = 0; 
$y = $h-$bl; 
$s = 212 * 0.75; 

imagettftext($im,$s,0,$x,$y,$asd,$font,$string); 

$prova = imagettfbbox($s, 0, 'font/times.ttf', $string); 
imageline($im,$prova[0],$h - $bl + $prova[1],$prova[2],$h - $bl + $prova[3],imagecolorallocate($im, 0, 0, 0)); 
imageline($im,$prova[4],$h - $bl + $prova[5],$prova[2],$h - $bl + $prova[3],imagecolorallocate($im, 0, 0, 0)); 
imageline($im,$prova[4],$h - $bl + $prova[5],$prova[6],$h - $bl + $prova[7],imagecolorallocate($im, 0, 0, 0)); 
imageline($im,$prova[0],$h - $bl + $prova[1],$prova[6],$h - $bl + $prova[7],imagecolorallocate($im, 0, 0, 0)); 
imageline($im,$prova[0],$h - $bl + $prova[1],$prova[4],$h - $bl + $prova[5],imagecolorallocate($im, 0, 0, 0)); 
imageline($im,$prova[2],$h - $bl + $prova[3],$prova[6],$h - $bl + $prova[7],imagecolorallocate($im, 0, 0, 0)); 

imageline($im,0,0,$w,$h,imagecolorallocate($im, 0, 255, 0)); 
imageline($im,0,$h,$w,0,imagecolorallocate($im, 0, 255, 0)); 

imagepng($im); 
imagedestroy($im); 

回答