PHP: imageftbbox - PullRequest
       10

PHP: imageftbbox

3 голосов
/ 21 декабря 2011

Что такое применение ограничительной рамки текста в PHP imageftbbox ?

В примере руководства оно использовалось для определения координат для функции imagefttext . Это необходимо? Может быть, я что-то здесь упускаю.

...

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

...

Ответы [ 2 ]

5 голосов
/ 21 декабря 2011

Flylib содержит обширную информацию о imagettfbox () .

Вот некоторая соответствующая информация из связанной информации:

enter image description here

(авторское право на изображение Flylib)

Из изображения выше вы можете видеть, что есть 4 пункта с 8 информацией (как уже говорилось в документации):

Array information     Bounding box coordinate
===================================================================
    0                 X coordinate of the lower left hand corner
    1                 Y coordinate of the lower left hand corner
    2                 X coordinate of the lower right hand corner
    3                 Y coordinate of the lower right hand corner
    4                 X coordinate of the upper right hand corner
    5                 Y coordinate of the upper right hand corner
    6                 X coordinate of the upper left hand corner
    7                 Y coordinate of the upper left hand corner

Опять же, как указано в документации, эта информация относится к тексту независимо от угла. Поэтому, если вы поверните текст на 90 градусов по часовой стрелке , ограничивающий прямоугольник станет:

Array information     Bounding box coordinate
===================================================================
    0                 X coordinate of the upper left hand corner
    1                 Y coordinate of the upper left hand corner
    2                 X coordinate of the lower left hand corner
    3                 Y coordinate of the lower left hand corner
    4                 X coordinate of the lower right hand corner
    5                 Y coordinate of the lower right hand corner
    6                 X coordinate of the upper right hand corner
    7                 Y coordinate of the upper right hand corner

Еще один ресурс, который, как я считаю, поможет вам лучше понять основную идею вокруг ограничительной рамки:

1 голос
/ 21 декабря 2011

пара функций imageftbbox и imagettfbbox позволяют узнать, сколько места займет ваш текст на изображении (первая используется для текста свободного текста, а вторая дляtrue type text)

таким образом, если у вас есть приложение, которое генерирует некоторые изображения и записывает на них переменный текст (пользовательский ввод или что-то подобное), вы можете решить, как / где разместить этот текст, используя такие функции, как imagefttextили imagettftext (та же разница - шрифт)

, чтобы вы могли сделать что-то вроде:

$bbox = imagettfbbox(34, 0, 'myriadb.otf', strtoupper($name)); //font size 34, text in a horizontal line, use myriadb.otf as font, the user name as variable text
$text_width = $bbox[2]; // this is how much space the name will take
$margin = (CARD_WIDTH-($text_width))/2; // a constant knowing the width of the resulting card.. this way we will center the name..
imagettftext($image, 34, 0, $margin, $y, $white, 'myriadb.otf', strtoupper($name));// $image resource, 34-same size, same angle, the margin is the the x coordinate, fixed $y, color, font and the same text
...