php - PHP 中的 "imagettfbbox()"是如何工作的?

标签 php gd

请您解释一下 imagettfbbox() 的返回值到底是什么意思? The manual says :

imagettfbbox() returns an array with 8 elements representing four points making the bounding box of the text on success and FALSE on error. [...Table of points here...] The points are relative to the text regardless of the angle, so "upper left" means in the top left-hand corner seeing the text horizontally.

但是,我发现不是很清楚。例如返回值:

array(-1, 1, 61, 1, 61, -96, -1, -96)

表示以下几点:

(-1, -96) ------ (61, -96)
    |                |
    |                |
    |                |
    |                |
    |                |
    |                |
 (-1, 1) -------- (61, 1)              

我应该如何解释它们?

为什么会有负值?

最佳答案

你应该看看 comment by "marclaz" on the imagettfbbox manual page :

Please note that as imageTTFBbox and imageTTFText functions return an array of coordinates which could be negative numbers care must be taken with height and width calculations.

The rigth way to do that is to use the abs() function:

for an horizontal text:

$box = @imageTTFBbox($size,0,$font,$text); $width = abs($box[4] -
$box[0]); $height = abs($box[5] - $box[1]);

Then to center your text at ($x,$y) position the code should be like that:

$x -= $width/2; $y += $heigth/2;

imageTTFText($img,$size,0,$x,$y,$color,$font,$text);

this because (0,0) page origin is topleft page corner and (0,0) text origin is lower-left readable text corner.

关于php - PHP 中的 "imagettfbbox()"是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338072/

相关文章:

php - 如何调试图像创建

php - imagepng() 和 GD 库中的 PHP 透明度

php - 从 .jpeg/png 创建动画 .gif

php - 如何在使用 mysql php 查询别名列之间运行

php - 无法在 php 中创建新的 MySQL 数据库

PHP mySQL 显示好友列表中的帖子

hyperlink - PHP 链接到 session_destroy

php - fwrite() : SSL operation failed with code 1. OpenSSL 错误消息:\nerror:1409F07F:SSL 例程:SSL3_WRITE_PENDING:PHP 中的错误写入重试

php - move_uploaded_file 不起作用,没有错误

php - 在创建的 PNG 图像上设置 DPI 的干净解决方案