从php中的ttf-font获取单个字符的宽度?

发布于 2024-08-25 15:24:05 字数 147 浏览 5 评论 0原文

我正在创建一个动态图像,它使用 PHP GD 库在我的页面上创建标题。问题是,我需要一个换行系统。这本身不是问题,但首先我需要获取当前字符的宽度(以像素为单位)。

我很好奇这个问题,有什么办法吗?或者我需要手动指定每个 abc 的宽度?

马蒂·莱恩

I'm creating an dynamic image, which creates headers on my page using PHPs GD-library. The problem is, that I need a line-wrapping system. It's not a problem itself, but first I need to get the width (in pixels) of current character.

I'm pretty curious about this, is there any way? Or do I need to manually specify width of every abc?

Martti Laine

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念 2024-09-01 15:24:05

您必须对每个字符执行 imagettfbbox()

未经测试但应该有效:

$string = "Lorem Ipsum";
$size = 20;
$angle = 0;
$fontfile = "ARIAL.TTF";

$strlen = strlen($string);
for ($i = 0; $i < $strlen; $i++)
 {
    $dimensions = imagettfbbox($size, $angle, $fontfile, $string[$i]);
    echo "Width of ".$string[$i]." is ".$dimensions[2]."<br>";

 }

You would have to do a imagettfbbox() on each single character.

Untested but should work:

$string = "Lorem Ipsum";
$size = 20;
$angle = 0;
$fontfile = "ARIAL.TTF";

$strlen = strlen($string);
for ($i = 0; $i < $strlen; $i++)
 {
    $dimensions = imagettfbbox($size, $angle, $fontfile, $string[$i]);
    echo "Width of ".$string[$i]." is ".$dimensions[2]."<br>";

 }
东北女汉子 2024-09-01 15:24:05

如果你打印结果,

$string = "Lorem Ipsum";
$size = 20;
$angle = 0;
$fontfile = "./fonts/arial.ttf";

$dimensions = imagettfbbox($size, $angle, $fontfile, $string); 
print_r($dimensions);

你可能会得到类似这样的结果:

Array
(
    [0] => -1
    [1] => 5
    [2] => 152
    [3] => 5
    [4] => 152
    [5] => -20
    [6] => -1
    [7] => -20
)

每个索引的位置是:

0   lower left corner, X position
1   lower left corner, Y position
2   lower right corner, X position
3   lower right corner, Y position
4   upper right corner, X position
5   upper right corner, Y position
6   upper left corner, X position
7   upper left corner, Y position

所以宽度应该是索引 2 - 索引 0。我不太明白索引 0 的负 1。

如果你将字符串中每个字符的总结果是 130 而不是 153。

If you print the results of

$string = "Lorem Ipsum";
$size = 20;
$angle = 0;
$fontfile = "./fonts/arial.ttf";

$dimensions = imagettfbbox($size, $angle, $fontfile, $string); 
print_r($dimensions);

you might get something` like:

Array
(
    [0] => -1
    [1] => 5
    [2] => 152
    [3] => 5
    [4] => 152
    [5] => -20
    [6] => -1
    [7] => -20
)

where each index is:

0   lower left corner, X position
1   lower left corner, Y position
2   lower right corner, X position
3   lower right corner, Y position
4   upper right corner, X position
5   upper right corner, Y position
6   upper left corner, X position
7   upper left corner, Y position

So the width should be index 2 - index 0. I dont quite get the minus 1 for index 0.

It is a bit odd that if you sum the total of each char in the string the results is 130 not 153.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文