donglinxia1541 2019-08-18 18:22 采纳率: 0%
浏览 183

当我试图找到文本长度和字符长度之和时,PHP imagettfbbox给出了不同的结果

I have a string with font and i need to find it's width, and then width of each char in this string, I'm using GD lib imagettfbbox for this, but it always gives me different result in the end.

$text="some random text";
$info=imagettfbbox(18,0,'/var/www/html/ARIAL.TTF',$text);
$text_width=$info[2]-$info[0];

$text_width_by_chars=0;
for($i=0;$i<strlen($text); $i++){
    $char=$text[$i];
    $info=imagettfbbox(18,0,'/var/www/html/ARIAL.TTF',$char);
    $char_width=$info[2]-$info[0];
    $text_width_by_chars+=$char_width;
}
echo $text_width.' '.$text_width_by_chars;

It happens to all fonts doesnt metter if $info[0] posetive or negative or 0, the differance is always around strlen($text) but not exactly, i've tried to find out if i can mutiply char width to some multiplier but it doesn't work, it's always around 0.91. Why does it happen? And how can i prevent this so in the end $text_width and $text_width_by_chars become equal?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料