imageftbbox

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

imageftbbox β€” ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΎΠ³ΠΎ обрамлСния для тСкста, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° freetype2 Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ Π½Π° экран ΠΏΡƒΡ‚Ρ‘ΠΌ Ρ€Π΅Π½Π΄Π΅Ρ€ΠΈΠ½Π³Π° ΡˆΡ€ΠΈΡ„Ρ‚Π°

ОписаниС

function imageftbbox(
Β Β Β Β float $size,
Β Β Β Β float $angle,
Β Β Β Β string $font_filename,
Β Β Β Β string $string,
Β Β Β Β array $options = []
): array|false

Ѐункция рассчитываСт ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΠΎΠ±Ρ€Π°ΠΌΠ»ΡΡŽΡ‰ΡƒΡŽ Ρ€Π°ΠΌΠΊΡƒ Π² пиксСлях для тСкста, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ выводится Π½Π° экран Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΎΠΉ FreeType.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅:

Π”ΠΎ PHP 8.0.0 функция imageftbbox() Π±Ρ‹Π»Π° Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½Π½ΠΎΠΉ вСрсиСй Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ imagettfbbox(), которая Π΄ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π»Π° ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ options. Начиная с 8.0.0 функция imagettfbbox() стала псСвдонимом Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ imageftbbox().

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

size

Π Π°Π·ΠΌΠ΅Ρ€ ΡˆΡ€ΠΈΡ„Ρ‚Π° Π² типографских ΠΏΡƒΠ½ΠΊΡ‚Π°Ρ….

angle

Π£Π³ΠΎΠ» Π² градусах, ΠΏΠΎΠ΄ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ функция ΠΈΠ·ΠΌΠ΅Ρ€ΠΈΡ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Π°Ρ€Π³ΡƒΠΌΠ΅Π½Ρ‚Π° string.

font_filename

НазваниС Ρ„Π°ΠΉΠ»Π° TrueType-ΡˆΡ€ΠΈΡ„Ρ‚Π° Π² Π²ΠΈΠ΄Π΅ локального ΠΏΡƒΡ‚ΠΈ ΠΈΠ»ΠΈ URL-адрСса. Π’ зависимости ΠΎΡ‚ вСрсии Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ GD, с ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚ PHP, функция попытаСтся Π½Π°ΠΉΡ‚ΠΈ Ρ„Π°ΠΉΠ»Ρ‹, названия ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… Π½Π΅ Π½Π°Ρ‡ΠΈΠ½Π°ΡŽΡ‚ΡΡ с символа '/', ΠΏΡƒΡ‚Ρ‘ΠΌ добавлСния ΠΊ названию Ρ„Π°ΠΉΠ»Π° Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΡ '.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

// Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ изобраТСния Ρ€Π°Π·ΠΌΠ΅Ρ€ΠΎΠΌ 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, 'Π“Ρ€ΡƒΠΏΠΏΠ° докумСнтирования языка PHP');

// Установка ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ для осСй 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, 'Π“Ρ€ΡƒΠΏΠΏΠ° докумСнтирования языка PHP');

// Π’Ρ‹Π²ΠΎΠ΄ Π² Π±Ρ€Π°ΡƒΠ·Π΅Ρ€
header('Content-Type: image/png');

imagepng($im);

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Π­Ρ‚Π° функция доступна Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π² случаС, Ссли PHP Π±Ρ‹Π» скомпилирован с ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΎΠΉ freetype (--with-freetype-dir=DIR)

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imagefttext() - Наносит тСкст Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ ΠΏΡƒΡ‚Ρ‘ΠΌ Ρ€Π΅Π½Π΄Π΅Ρ€ΠΈΠ½Π³Π° ΡˆΡ€ΠΈΡ„Ρ‚Π° Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΎΠΉ FreeType 2
  • imagettfbbox() - ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚ Π³Ρ€Π°Π½ΠΈΡ†Ρ‹ ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΎΠ³ΠΎ обрамлСния для тСкста, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠ° freetype2 Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ Π½Π° экран ΠΏΡƒΡ‚Ρ‘ΠΌ Ρ€Π΅Π½Π΄Π΅Ρ€ΠΈΠ½Π³Π° ΡˆΡ€ΠΈΡ„Ρ‚Π°
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 8 notes

up
2
fernando ΒΆ
20 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.
up
1
phpimageftbbox at juggernaut dot com dot au ΒΆ
24 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);
up
1
sectionthirty1 at yahoo dot com ΒΆ
22 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);
up
0
theo v e -2 ΒΆ
20 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.)
up
-1
pablocorezzola at gmail dot com ΒΆ
8 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);
}
up
-1
Johan ΒΆ
18 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);
up
-2
ta at NOSPAM dot magicsquare dot info ΒΆ
23 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
up
-3
groomed at users dot sf dot net ΒΆ
22 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