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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化