пара функций 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