當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP imageftbbox()用法及代碼示例

imageftbbox()函數是PHP中的內置函數,用於通過freetype2使用字體來計算文本的邊界框。

用法:

array imageftbbox( float $size, array $angle,
          string $fontfile, string $text, array $extrainfo )

參數:該函數接受上述和以下所述的五個參數:


  • $size:它以磅為單位指定字體大小。
  • $angle:它指定以度為單位的角度,在該角度下將測量文本。
  • $fontfile:它指定字體文件名。
  • $text:它指定要測量的字符串。
  • $extrainfo (Optional):它指定了額外的信息。

返回值:該函數成功返回一個數組。

下麵給出的程序說明了PHP中的imageftbbox()函數:

程序1:

<?php 
  
// Create bounding box with local font file 
$bbox = imageftbbox(100, 100, './Pacifico.ttf', 'GeeksforGeeks'); 
  
// Print the boundbox data 
print("<pre>".print_r($bbox, true)."</pre>"); 
?>

輸出:

Array
(
    [0] => 47
    [1] => -13
    [2] => -91
    [3] => -806
    [4] => -264
    [5] => -776
    [6] => -124
    [7] => 17
)

程序2:

<?php 
  
// Create an image 
$im = imagecreatetruecolor(800, 250); 
  
// Set the background to be light blue 
imagefilledrectangle($im, 0, 0, 299, 299, 
       imagecolorallocate($im, 0, 0, 100)); 
  
// Create bounding box with local font file 
$bbox = imageftbbox(10, 0, './Pacifico.ttf', 
            'GeeksforGeeks'); 
  
// Calculate coordinates using bounding box 
$x = $bbox[0] + 130; 
$y = $bbox[1] + 130; 
  
// Add text 
imagefttext($im, 50, 0, $x, $y, imagecolorallocate($im, 
         0, 150, 0), './Pacifico.ttf', 'GeeksforGeeks'); 
  
// Output to browser 
header('Content-Type:image/png'); 
imagepng($im); 
imagedestroy($im); 
?>

輸出:

參考: https://www.php.net/manual/en/function.imageftbbox.php



相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | imageftbbox() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。