Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

imageftbbox

Description

The imageftbbox of Image for PHP give the bounding box of a text using fonts via FreeType 2.

Syntax

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

Parameters

size

The font size in points.

angle

The angle in degrees.

Higher values represent a counter-clockwise rotation. 0 degrees reads left-to-right. 90 degrees reads bottom-to-top.

font_filename

The path to the TrueType font.

Depending on which version of the GD library PHP is using, when font_filename does not begin with a leading / then .ttf will be appended to the filename and the library will attempt to search for that filename along a library-defined font path.

When using versions of the GD library lower than 2.0.18, a space character, rather than a semicolon, was used as the 'path separator' for different font files. Unintentional use of this feature will result in the warning message: Warning: Could not find/open font. For these affected versions, the only solution is moving the font to a path which does not contain spaces.

In many cases where a font resides in the same directory as the script using it the following trick will alleviate any include problems.

Note: Note that open_basedir does not apply to font_filename.

<?
// Set the environment variable for GD
putenv('GDFONTPATH=' . realpath('.'));

// Name the font to be used (note the lack of the .ttf extension)
$font = 'SomeFont';
?>

string

The string to be measured.

options

KeyTypeDescription
linespacingfloatDefines drawing linespacing

Return

Returns an array with 8 elements defining the four points of the bounding box of the text on success and false on error.

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.

KeyDescription
0lower left x
1lower left y
2lower right x
3lower right y
4upper right x
5upper right y
6upper left x
7upper left y

Examples

1 · return

<?

$size = 8;
$angle = 0;
$font_filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/ttf/arial.ttf';
$string = "string";
$options = array(
    0
);

$return = imageftbbox($size, $angle, $font_filename, $string, $options);

var_export($return);

?>
array (
  0 => 0,
  1 => 2,
  2 => 25,
  3 => 2,
  4 => 25,
  5 => -7,
  6 => 0,
  7 => -7,
)

2 · base64

<?

$width = 100;
$height = 100;
$image = imagecreatetruecolor($width, $height);

$red = 255;
$green = 255;
$blue = 255;
$color = imagecolorallocate($image, $red, $green, $blue);

$size = 8;
$angle = -45;
$font_filename = $_SERVER['DOCUMENT_ROOT'] . '/assets/ttf/arial.ttf';
$string = "string";
$options = array(
    0
);
$box = imageftbbox($size, $angle, $font_filename, $string, $options);

$x = $box[0] + ((imagesx($image) - $box[4]) / 2);
$y = $box[1] + ((imagesy($image) - $box[5]) / 2);
imagefttext($image, $size, $angle, $x, $y, $color, $font_filename, $string, $options);

ob_start();

    imagepng($image);

$output = ob_get_clean();

echo '<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,' . base64_encode($output) . '">
</body>
</html>';

?>
<!doctype html>
<html>
<body>
    <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAIAAAD/gAIDAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACgElEQVR4nO3YMUgjQRTG8bkoKURCEBGxCiIiS5BgOYgsuAQViyAqslhYptIUskg6xSqCtVhZSBCxCYYQLCxSiEXQICJbiMWQwiJIEJGY6oqVkFO5aw5nhf+vGraZj8e+tzMrBAAAAAAAAAAAAAAAAAAAAAAAwH/W39+vO8IPkc1mlVLlcnlgYEB3Fh/r7OwUQjSbzcHBwa2trZOTk0AgMDExsbKyojua/xweHhqGcXx8fHR0JKW8vr52XVcpFYvFdEfzn6urq7GxsUgk8vr6KqVcXV2t1WoPDw/hcFh3tH/o+P4t397ednZ2Li8vx8fH6/X62tra9PT06enp5ORktVp9eXn5/ki+Ztt2sVjc399vdZ/jOM/Pz5lMpvWJ9EYb3t3c3CwtLQkhHMdxXdeyrN3d3e3tbSFEPB7f29vTHfCjXxr3TqVSoVCo0WgsLi5allWv173nyWSyUCg8PT3Rkn+QUpbL5fbR7jhOpVJZX1/n/PWFYDDYWnuV2tzcbDQa6XQ6FAppDPZZQHcA0Ww2vYXjOLZt53K5hYWF5eXl3t7ei4uL9lLinZTSe6fau1IpZVmW3mA+ZZpme6USiYRSqqurS28q/2o13ezsrFLKMAy9eX4Ar1LRaFQI0dfXd3BwUK1Wi8Wi9l86+gf8Z0NDQzMzM7e3t93d3aVSSSllmmalUvHOq/jaxsZG6xwfDAZd1z07O9OYx9f3r5GRkfPzc289NTUVDofn5+f1RvIj27YNw0gkEq7rxuPxZDJ5f3/vTTF8FI1GlVKjo6Omaebz+cfHRyr1N3Nzc7VarVQq3d3dDQ8P647jez09PVJKbjwAAAAAAAAAAAAAAAAAAADwj9+G/ejGt8S0jAAAAABJRU5ErkJggg==">
</body>
</html>
HomeMenu