PHP imagettfbbox和imagettftext为同一字符提供不同的宽度
在我把一封信写到一张图片上之前,我想知道它的宽度。我正在为此使用PHP imagettfbbox和imagettftext为同一字符提供不同的宽度,php,imagettftext,Php,Imagettftext,在我把一封信写到一张图片上之前,我想知道它的宽度。我正在为此使用imagettfbbox。然而,我从这个函数得到的宽度似乎与我画完字母后得到的宽度不一样。我用来画画的字体是 我为测试这一点而编写的代码如下: /* Create the temp image. */ $tempImage = imagecreatetruecolor(100, 100); $fontSize = 24; $font = __DIR__ . '/Fonts/FreeSans.ttf'; $text = 'e';
imagettfbbox
。然而,我从这个函数得到的宽度似乎与我画完字母后得到的宽度不一样。我用来画画的字体是
我为测试这一点而编写的代码如下:
/* Create the temp image. */
$tempImage = imagecreatetruecolor(100, 100);
$fontSize = 24;
$font = __DIR__ . '/Fonts/FreeSans.ttf';
$text = 'e';
/* Get letter size before writing. */
$letterBoxSize = imagettfbbox($fontSize, 0, $font, $text);
$letterBoxWidth = max([$letterBoxSize[2], $letterBoxSize[4]]) - min([$letterBoxSize[0], $letterBoxSize[6]]);
/* Write the letter at (50, 50). */
$bbox = imagettftext($tempImage, $fontSize, 0, 50, 50, 0, $font, $text);
$bboxWidth = max([$bbox[2], $bbox[4]]) - min([$bbox[0], $bbox[6]]);
echo 'Width of letter from imagettfbbox: ' . PHP_EOL;
var_dump($letterBoxWidth);
echo 'Width of letter from imagettftext: ' . PHP_EOL;
var_dump($bboxWidth);
结果输出:
Width of letter from imagettfbbox:
int(19)
Width of letter from imagettftext:
int(20)
如何在写信前后获得相同的宽度
我的PHP版本是7.4.3,根据
GD_info()
我的GD库版本是2.2.5这似乎是一个与设备相关的问题。使用相同字体运行提供的测试代码,显示ttfbox:int(16)
和ttftext:int(16)
您的PHP和GD版本@0stone0?PHP7.4.3--gf_info()
显示:捆绑(2.1.0兼容)