PHP 计算字符串根据字体字号组成的长度

发布于 2024-01-12  701 次阅读


https://www.php.net/manual/zh/function.imagettfbbox.php

imagettfbbox(
    float $size,
    float $angle,
    string $font_filename,
    string $string,
    array $options = []
): array|false

脚本中使用

需要设置 GDFONTPATH

$fontpath = realpath('.'); //replace . with a different directory if needed 
putenv('GDFONTPATH='.$fontpath); 
print_r(ImageTTFBBox(20,0,'MiSans-Regular.ttf','Future'));

框架中使用(tp6为例)

ImageTTFBBox(20,0,app()->getRootPath().'public/MiSans-Regular.ttf',“测试文字”);