简体   繁体   English

PHP:imageftbbox

[英]PHP: imageftbbox

What is the application of a bounding box of a text in PHP imageftbbox ? PHP imageftbbox中文本边界框的应用是什么?

In the example of the manual , it was used to determine the coordinates for the imagefttext function. 在本手册的示例中,它用于确定imagefttext函数的坐标。 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');

...

Flylib has extensive information about imagettfbox() . Flylib有关于imagettfbox()的广泛信息。

Here's some relevant information from the linked information: 以下是链接信息中的一些相关信息:

在此输入图像描述

(image copyright Flylib) (图片版权所有Flylib)

From image above, you can see that there are 4 point with 8 information (as the documentation already stated): 从上图中,您可以看到有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

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: 因此,如果将文本顺时针旋转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

Another resources that I believe will help you better grasp the basic idea around bounding box: 我相信的另一个资源将帮助您更好地掌握边界框周围的基本思想:

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) imageftbboximagettfbbox这两个函数允许您知道文本在图像上占用多少空间(第一个用于自由类型文本,第二个用于真实类型文本)

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) 这样,如果你有一个应用程序生成一些图像并在其上写入变量文本(用户输入或类似的东西),你可以决定使用像imagefttext或imagettftext这样的函数放置该文本的方式/位置(相同的区别 - 字体)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM