imageftbbox
(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)
imageftbbox — 通过 freetype2 使用字体给出文本的边界框
说明
   imageftbbox(
float
float
string
string
array
): array|false
  float
$size,float
$angle,string
$font_filename,string
$string,array
$options = []): array|false
此函数计算并返回 FreeType 文本的边界框(以像素为单位)。
注意:
在 PHP 8.0.0 之前,imageftbbox() 是 imagettfbbox() 的扩展变体,它还支持
options。自 PHP 8.0.0 起,imagettfbbox() 是 imageftbbox() 的别名。
参数
size- 
      
字体的尺寸,单位:点(磅)。
 angle- 
      
测量的
string的角度(以度为单位)。 font_filename- 
      
TrueType 字体文件的名称(可以是 URL)。根据 PHP 使用的 GD 库的版本,可能会尝试搜索没有以“/”开头并会在末尾追加“.ttf”的文件名,并沿着库定义的字体路径搜索。
 string- 
      
要测量的字符串。
 options- 
      
options可能的数组索引键 类型 含义 linespacingfloat 定义绘制行距  
返回值
imageftbbox() 返回包含 8 个元素的数组,代表构成文本边界框的四个点:
| 0 | 左下角,X 坐标 | 
| 1 | 左下角,Y 坐标 | 
| 2 | 右下角,X 坐标 | 
| 3 | 右下角,Y 坐标 | 
| 4 | 右上角,X 坐标 | 
| 5 | 右上角, Y 坐标 | 
| 6 | 左上角,X 坐标 | 
| 7 | 左上角,Y 坐标 | 
   无论 angle 如何,这些点都是相对于
   text,因此“左上角”意味着在左上角水平地看到文本。
  
   失败时,返回 false。
  
示例
示例 #1 imageftbbox() 示例
<?php
// 创建 300x150 图像
$im = imagecreatetruecolor(300, 150);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
// 将背景设置为白色
imagefilledrectangle($im, 0, 0, 299, 299, $white);
// 字体文件的路径
$font = './arial.ttf';
// 首先创建边界框
$bbox = imageftbbox(10, 0, $font, 'The PHP Documentation Group');
// 这是 X 和 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');
// 输出到浏览器
header('Content-Type: image/png');
imagepng($im);
?>注释
注意: 此函数仅在 PHP 编译时加入 freetype 支持时有效(--with-freetype-dir=DIR)。
参见
- imagefttext() - 使用 FreeType 2 字体将文本写入图像
 - imagettfbbox() - 取得使用 TrueType 字体的文本的边界框
 
  +添加备注
  
用户贡献的备注 8 notes
  
  
  fernando ¶
  
 
  19 years ago
  imagettfbbox() returns an array with 8 elements representing four points making the bounding box of the text: 
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 
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.  
  
  phpimageftbbox at juggernaut dot com dot au ¶
  
 
  22 years ago
  This function can be used to generate right-aligned text. Just work out how wide the text image is and position it accordingly. Example:
$i_width  = 200;
$i_height = 40;
$string = "Hello World!";
$pointsize = 10;
$fontfile = "/usr/local/lib/ttf/Helve.ttf";
$im = imagecreate($i_width, $i_height);
$black = imagecolorallocate ($im, 0, 0, 0);
$white = imagecolorallocate ($im, 255, 255, 255);
$string_size = ImageFtBbox($pointsize, 0, $fontfile, $string, array("linespacing" => 1));
$s_width  = $string_size[4];
$s_height = $string_size[5];
ImageFtText($im, $pointsize, 0, $i_width - $s_width - 1,  0 - $s_height, $white, $fontfile, $string, array("linespacing" => 1));
Header ("Content-type: image/png");
ImagePNG ($im);
ImageDestroy ($im);  
  
  sectionthirty1 at yahoo dot com ¶
  
 
  20 years ago
  Here is a handy example I used to center "dynamic text" onto an image.  
Ex. Say you want to center a clients IP Address onto a picture.  
$ip=$_SERVER['REMOTE_ADDR'];    
$details = imageftbbox($fontsize, 0, $font, $ip, array("linespacing" => 1));
$xcoord = ($imgwidth - $details[4]) / 2;  // this will return the x coordinate centered to your specific image.  Make sure  you set $imgwidth to the width of the image you are using.      
imagettftext($image, $fontsize, 0, $xcoord, $ycoord, $fontcolor, $font, $ip);  
  
  theo v e -2 ¶
  
 
  19 years ago
  ah... the problem between imageftbbox() and imagefttext() lies in the mirroring of the y-axes.
Below you see, for a font-size 16 the boudingboxes of "b", "p" and "bp":
< b: w=9 h=15
b(0,-1)
b(9,-1)
b(9,-16)
b(0,-16)
< p: w=9 h=16
p(0,4)
p(9,4)
p(9,-12)
p(0,-12)
< bp: w=20 h=20
bp(0,4)
bp(20,4)
bp(20,-16)
bp(0,-16)
If drawing "bp" using imagefttext() at y=0, the the top of "bp" indeed is at y=-16, and the bottom of "bp" at y=4. (Plus or minus a pixel here and there, because at y=0 there actually is a vissible pixel.)  
  
  pablocorezzola at gmail dot com ¶
  
 
  7 years ago
  //EXAMPLE - Center text
function newText($im, $size, $angle= 0, $x, $y, $color, $font, $text,$align = "left",$border=false,$width=0,$height=0){
    if($align == "center")
    {
        if ($border == true ){
           imagerectangle($im, $x, $y, $x +$width, $y + $height, $color);
        }
        $bbox = imageftbbox($size, 0, $font, $text);
        // Marcamos el ancho y alto
        $s_width  = $bbox[4];
        $s_height = $bbox[5];  
        $y = $y + ($height-$s_height)/2;
        $x = $x + ($width-$s_width)/2;
    }
    imagettftext($im, $size, $angle, $x, $y, $color, $font, $text);
}  
  
  Johan ¶
  
 
  17 years ago
  For alignment i used this method:
if($align == "center" || $align == "right")
    {
        $verticaltxtspace = $backwidth - (2 * $posx);        
        $spacepositions = imagettfbbox($size, $angle, "fonts/verdanaz.ttf", " ");         
        $spacepx = $spacepositions[4] - $spacepositions[0];        
        // Split text in lines
        $lines = split("[\r][\n]", $text);        
        for($count = 0; $count < count($lines); $count++)
        {
            $textpositions = imagettfbbox($size, $angle, "fonts/verdanaz.ttf", $lines[$count]);            
            $textpx = $textpositions[2] - $textpositions[0];
            if($align == "right")
            {
                $spaces = ($verticaltxtspace - $textpx) / $spacepx;
            }
            else if($align == "center")
            {
                $spaces = (($verticaltxtspace - $textpx)/2) / $spacepx;
            }
            // Add spaces
            $line = $lines[$count];
            for($i = 0; $i < $spaces; $i++)
            {
                $line = " " . $line;
            }
            $lines[$count] = $line;
        }
        // Create new text of lines
        $text = "";
        for($count = 0; $count < count($lines); $count++)
        {
            $text .= $lines[$count] . "\r\n";
        }        
    }
    //    Draw the shadow text on de shadow
    imagettftext($background, $size, $angle, $posx, $posy, $textcolor, "fonts/verdanaz.ttf",  $text);  
  
  ta at NOSPAM dot magicsquare dot info ¶
  
 
  22 years ago
  i've found a work around for this situation
it seems that height is directly proportional to line spacing so you just have to apply the same factor to image height
for example :
$spacing = 0.7;
$params = array("linespacing" => $spacing);
$box = imageftbbox ($size, 0, $font, $text, $params);
$tw=$box[4]-$box[0]; //image width
$th=($box[1]-$box[5])*$spacing; //image height  
  
  groomed at users dot sf dot net ¶
  
 
20 years ago
  ImageFTBBox returns a bounding box, not metrics, as some (most?) of the notes above seem to assume. The 8 values it returns specify the 4 corners of this bounding box. So to properly determine the width and height of a string you need to do:
$bbox = ImageFTBBox(...);
$width = abs($bbox[0]) + abs($bbox[2]); // distance from left to right
$height = abs($bbox[1]) + abs($bbox[5]); // distance from top to bottom