PHP Conference Japan 2024

imageftbbox

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

imageftbbox使用 FreeType 2 取得文字的邊界框

描述

imageftbbox(
    浮點數 $size,
    浮點數 $angle,
    字串 $font_filename,
    字串 $string,
    陣列 $options = []
): 陣列|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 可能的陣列索引
鍵名 類型 意義
linespacing float 定義繪製的行距

返回值

imageftbbox() 返回一個包含 8 個元素的陣列,表示組成文字邊界框的四個點

0 左下角,X 座標
1 左下角,Y 座標
2 右下角,X 座標
3 右下角,Y 座標
4 右上角,X 座標
5 右上角,Y 座標
6 左上角,X 座標
7 左上角,Y 座標

這些點是相對於 *文字* 的,與 angle 無關,因此「左上角」是指在水平方向看文字時,文字的左上角。

失敗時,返回 false

範例

範例 #1 imageftbbox() 範例

<?php
// Create a 300x150 image
$im = imagecreatetruecolor(300, 150);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);

// Set the background to be white
imagefilledrectangle($im, 0, 0, 299, 299, $white);

// Path to our font file
$font = './arial.ttf';

// 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');

// Output to browser
header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
?>

備註

注意只有在 PHP 編譯時啟用 freetype 支援 (--with-freetype-dir=DIR) 才可以使用此函數。

參見

新增註釋

使用者貢獻的註釋 8 則註釋

fernando
19 年前
imagettfbbox() 返回一個包含 8 個元素的陣列,表示組成文字邊界框的四個點

0 左下角,X 座標
1 左下角,Y 座標
2 右下角,X 座標
3 右下角,Y 座標
4 右上角,X 座標
5 右上角,Y 座標
6 左上角,X 座標
7 左上角,Y 座標

這些點是相對於文字的,與角度無關,因此「左上角」是指在水平方向看文字時,文字的左上角。
phpimageftbbox at juggernaut dot com dot au
22 年前
此函數可用於產生靠右對齊的文字。只需計算出文字影像的寬度,然後據此定位即可。範例

$i_width = 200;
$i_height = 40;

$string = "Hello World!"; // $string = "哈囉,世界!";
$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"); // 設定 HTTP 標頭
ImagePNG ($im); // 輸出 PNG 圖像
ImageDestroy ($im); // 銷毀圖像資源
sectionthirty1 at yahoo dot com
20 年前
這是一個我用來將「動態文字」置中的簡便範例。

例如,假設您想將客戶的 IP 位址置中於圖片上。

$ip=$_SERVER['REMOTE_ADDR']; // 取得客戶端 IP 位址

$details = imageftbbox($fontsize, 0, $font, $ip, array("linespacing" => 1)); // 取得 IP 位址文字方框大小

$xcoord = ($imgwidth - $details[4]) / 2; // 計算置中的 x 座標,請確保 $imgwidth 已設定為您使用的圖像寬度。

imagettftext($image, $fontsize, 0, $xcoord, $ycoord, $fontcolor, $font, $ip); // 將 IP 位址寫入圖像
theo v e -2
18 年前
啊... imageftbbox() 和 imagefttext() 之間的問題在於 y 軸的鏡像。
以下您可以看到字體大小為 16 時,「b」、「p」和「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)
如果使用 imagefttext() 在 y=0 處繪製「bp」,則「bp」的頂部確實位於 y=-16 處,底部位於 y=4 處。(加減一個像素,因為在 y=0 處實際上有一個可見像素。)
pablocorezzola at gmail dot com
7 年前
//範例 - 置中文字

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); //取得文字方框大小

// 標記寬度和高度
$s_width = $bbox[4]; //文字寬度
$s_height = $bbox[5]; //文字高度

$y = $y + ($height-$s_height)/2; //計算垂直置中 y 座標
$x = $x + ($width-$s_width)/2; //計算水平置中 x 座標

}

imagettftext($im, $size, $angle, $x, $y, $color, $font, $text); //將文字寫入圖像
}
Johan
17 年前
我使用這個方法進行對齊

if($align == "center" || $align == "right") //如果對齊方式為置中或靠右
{
$verticaltxtspace = $backwidth - (2 * $posx); //計算可用的垂直空間
$spacepositions = imagettfbbox($size, $angle, "fonts/verdanaz.ttf", " "); //取得空格的方框大小
$spacepx = $spacepositions[4] - $spacepositions[0]; //計算空格的像素寬度

// 將文字分割成行
$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; //計算需要添加的空格數量
}

// 添加空格
$line = $lines[$count]; //取得當前行
for($i = 0; $i < $spaces; $i++) //迴圈添加空格
{
$line = " " . $line; //在行首添加空格
}
$lines[$count] = $line; //更新當前行
}

// 建立新的文字行
$text = ""; //清空文字
for($count = 0; $count < count($lines); $count++) //迴圈處理每一行
{
$text .= $lines[$count] . "\r\n"; //將處理後的行添加到文字中
}
}


// 在陰影上繪製陰影文字
imagettftext($background, $size, $angle, $posx, $posy, $textcolor, "fonts/verdanaz.ttf", $text);
ta at NOSPAM dot magicsquare dot info
22 年前
我找到了這個情況的解決方法

看起來高度與行距成正比,所以您只需將相同的係數應用於圖片高度

例如

$spacing = 0.7;
$params = array("linespacing" => $spacing);

$box = imageftbbox ($size, 0, $font, $text, $params);
$tw=$box[4]-$box[0]; //圖片寬度
$th=($box[1]-$box[5])*$spacing; //圖片高度
groomed at users dot sf dot net
20 年前
ImageFTBBox 返回的是邊界框,而不是度量值,就像上面一些(大多數?)註釋似乎假設的那樣。它返回的 8 個值指定了這個邊界框的 4 個角。因此,要正確確定字串的寬度和高度,您需要這樣做

$bbox = ImageFTBBox(...);
$width = abs($bbox[0]) + abs($bbox[2]); // 從左到右的距離
$height = abs($bbox[1]) + abs($bbox[5]); // 從上到下的距離
To Top