PHP:imageftbbox

时间:2011-12-21 09:05:51

标签: php image gd

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

manual的示例中,它用于确定 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');

...

2 个答案:

答案 0 :(得分:5)

Flylib提供了有关imagettfbox()的广泛信息。

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

enter image description here

(图片版权所有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

我认为另一种资源可以帮助您更好地掌握有关边界框的基本概念:

答案 1 :(得分:1)

这两个函数 imageftbbox imagettfbbox 可让您知道文本在图像上占用多少空间(第一个用于自由类型文本和第二个是真正的类型文本)

这样,如果你有一个应用程序生成一些图像并在其上写入变量文本(用户输入或类似的东西),你可以决定使用像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