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 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)