drsqpko5286 2013-06-26 23:19 采纳率: 0%
浏览 646
已采纳

php imagettftext字体位置

I'm using imagettftext() to write a letter into an image. The problem is that I don't know what's the letter position inside the image.

enter image description here

As you can see, I drawn border of the image and of the letter using imagettfbbox(), but the letter border is on the left also when the letter is on the right.

So the question is: How can I know where the letter starts (on the x axis)? Every letter looks like having a different "offset" and I just want to know it to draw the black border.

This is the code I used to create the images of the link above:

//$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);
  • 写回答

1条回答 默认 最新

  • dqusbxh44823 2013-06-27 12:31
    关注

    I just found the reason the in the Manual page of the function: http://www.php.net/manual/en/function.imagettfbbox.php#97357

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘