PHP: 图像ftbbox

发布于 2024-12-22 04:11:24 字数 563 浏览 0 评论 0原文

PHP imageftbbox 中文本边框的应用是什么?

手册的示例中,它用于确定imagefttext 函数。有必要吗?也许我在这里遗漏了一些东西。

...

// First we create our bounding box
$bbox = imageftbbox(10, 0, $font, 'The PHP Documentation Group');

// This is our cordinates for X and Y
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) - 5;
$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;

imagefttext($im, 10, 0, $x, $y, $black, $font, 'The PHP Documentation Group');

...

What is the application of a bounding box of a text in PHP imageftbbox?

In the example of the manual, it was used to determine the coordinates for the imagefttext function. Is it necessary? Maybe I am missing something here.

...

// First we create our bounding box
$bbox = imageftbbox(10, 0, $font, 'The PHP Documentation Group');

// This is our cordinates for X and Y
$x = $bbox[0] + (imagesx($im) / 2) - ($bbox[4] / 2) - 5;
$y = $bbox[1] + (imagesy($im) / 2) - ($bbox[5] / 2) - 5;

imagefttext($im, 10, 0, $x, $y, $black, $font, 'The PHP Documentation Group');

...

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

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

发布评论

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

评论(2

起风了 2024-12-29 04:11:25

一对函数 imageftbboximagettfbbox 可以让您知道文本在图像上占用多少空间(第一个用于自由类型文本,第二个用于真正的文本) -

这样,如果您有一个应用程序生成一些图像并在其上写入可变文本(用户输入或类似的内容),您可以使用 imagefttext 或 imagettftext 等函数来决定如何/在何处放置该文本(相同的区别 字体)

所以你可以这样做:

$bbox = imagettfbbox(34, 0, 'myriadb.otf', strtoupper($name)); //font size 34, text in a horizontal line, use myriadb.otf as font, the user name as variable text
$text_width = $bbox[2]; // this is how much space the name will take
$margin = (CARD_WIDTH-($text_width))/2; // a constant knowing the width of the resulting card.. this way we will center the name..
imagettftext($image, 34, 0, $margin, $y, $white, 'myriadb.otf', strtoupper($name));// $image resource, 34-same size, same angle, the margin is the the x coordinate, fixed $y, color, font and the same text

the pair of functions imageftbbox and imagettfbbox allow you to know how much space will your text take on an image (the first one is used for free type text and the second for true type text)

this way, if you have an application that generates some images and writes on them variable text (user input or something like this) you can decide how/where to place that text using functions like imagefttext or imagettftext (same difference - the font)

so you can do something like:

$bbox = imagettfbbox(34, 0, 'myriadb.otf', strtoupper($name)); //font size 34, text in a horizontal line, use myriadb.otf as font, the user name as variable text
$text_width = $bbox[2]; // this is how much space the name will take
$margin = (CARD_WIDTH-($text_width))/2; // a constant knowing the width of the resulting card.. this way we will center the name..
imagettftext($image, 34, 0, $margin, $y, $white, 'myriadb.otf', strtoupper($name));// $image resource, 34-same size, same angle, the margin is the the x coordinate, fixed $y, color, font and the same text
无法回应 2024-12-29 04:11:24

Flylib 拥有有关 imagettfbox().

以下是链接信息中的一些相关信息:

在此处输入图像描述

(图片版权所有 Flylib)

从上图中,您可以看到有 4 个点和 8 个信息(如文档中所述):

Array information     Bounding box coordinate
===================================================================
    0                 X coordinate of the lower left hand corner
    1                 Y coordinate of the lower left hand corner
    2                 X coordinate of the lower right hand corner
    3                 Y coordinate of the lower right hand corner
    4                 X coordinate of the upper right hand corner
    5                 Y coordinate of the upper right hand corner
    6                 X coordinate of the upper left hand corner
    7                 Y coordinate of the upper left hand corner

同样,如文档中所述,该信息与文本相关,无论角度如何。因此,如果您顺时针旋转文本 90 度,边界框将变为:

Array information     Bounding box coordinate
===================================================================
    0                 X coordinate of the upper left hand corner
    1                 Y coordinate of the upper left hand corner
    2                 X coordinate of the lower left hand corner
    3                 Y coordinate of the lower left hand corner
    4                 X coordinate of the lower right hand corner
    5                 Y coordinate of the lower right hand corner
    6                 X coordinate of the upper right hand corner
    7                 Y coordinate of the upper right hand corner

我相信将帮助您更好地掌握边界框基本概念的另一个资源:

Flylib has extensive information about imagettfbox().

Here's some relevant information from the linked information:

enter image description here

(image copyright Flylib)

From image above, you can see that there are 4 point with 8 information (as the documentation already stated):

Array information     Bounding box coordinate
===================================================================
    0                 X coordinate of the lower left hand corner
    1                 Y coordinate of the lower left hand corner
    2                 X coordinate of the lower right hand corner
    3                 Y coordinate of the lower right hand corner
    4                 X coordinate of the upper right hand corner
    5                 Y coordinate of the upper right hand corner
    6                 X coordinate of the upper left hand corner
    7                 Y coordinate of the upper left hand corner

Again, as stated by the documentation, this information is relative to text regardless of the angle. Therefore, if you rotate the text to 90 degree clockwise, the bounding box become:

Array information     Bounding box coordinate
===================================================================
    0                 X coordinate of the upper left hand corner
    1                 Y coordinate of the upper left hand corner
    2                 X coordinate of the lower left hand corner
    3                 Y coordinate of the lower left hand corner
    4                 X coordinate of the lower right hand corner
    5                 Y coordinate of the lower right hand corner
    6                 X coordinate of the upper right hand corner
    7                 Y coordinate of the upper right hand corner

Another resources that I believe will help you better grasp the basic idea around bounding box:

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