我imagettftext()
用来在图像中写一封信。问题是我不知道图像中的字母位置是什么。
如您所见,我使用 绘制了图像和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);