Current Location: Home> Latest Articles> Alternative Approach: How to Use imagettfbbox() Instead of imagefontwidth() for More Accurate Text Width Measurement

Alternative Approach: How to Use imagettfbbox() Instead of imagefontwidth() for More Accurate Text Width Measurement

M66 2025-06-11

When handling text in images with PHP, it's often necessary to obtain the width of the text for layout and positioning purposes. Traditionally, many developers use the imagefontwidth() function to get the width of characters in built-in fonts. However, since imagefontwidth() only supports built-in fonts (which have fixed sizes), its precision and flexibility are limited, especially when using TrueType fonts.

This article will introduce how to use the imagettfbbox() function as a replacement for imagefontwidth(), achieving more accurate text width measurement, especially suited for scenarios where TTF fonts are freely used.


1. Limitations of imagefontwidth()

The imagefontwidth() function only accepts built-in font sizes (from 1 to 5) and returns the width of a character in the specified font (in pixels). However, this function cannot measure the overall width of any string, nor does it support custom font sizes or font files.

<?php
// Traditional usage: get the width of a character for built-in font size 3
$charWidth = imagefontwidth(3);
echo "Character width: $charWidth";
?>

This method cannot accurately obtain the width of arbitrary strings or custom fonts.


2. Using imagettfbbox() to Obtain More Accurate Text Width

imagettfbbox() is a PHP function designed for TrueType fonts. It returns an array representing the bounding box coordinates of the given text on the image. By calculating these coordinates, you can get the width and height of the text.

Function Prototype:

array imagettfbbox(float $size, float $angle, string $fontfile, string $text)
  • $size: font size

  • $angle: angle (usually 0)

  • $fontfile: path to the font file (.ttf)

  • $text: the text to measure

Return Value:

Returns an array of 8 integers representing the four vertices of the text bounding box:

0 => lower left X
1 => lower left Y
2 => lower right X
3 => lower right Y
4 => upper right X
5 => upper right Y
6 => upper left X
7 => upper left Y

3. Example Code for Calculating Text Width

The following example demonstrates how to replace imagefontwidth() with imagettfbbox() to accurately measure the text width.

<?php
// Text content
$text = "Hello, 世界!";
<p>// Font size<br>
$fontSize = 16;</p>
<p>// Angle<br>
$angle = 0;</p>
<p>// Font file path (assuming the font file is on the server)<br>
$fontFile = 'm66.net/fonts/arial.ttf';</p>
<p>// Get the bounding box array for the text<br>
$bbox = imagettfbbox($fontSize, $angle, $fontFile, $text);</p>
<p>// Calculate width<br>
// Lower right X - lower left X<br>
$textWidth = abs($bbox[2] - $bbox[0]);</p>
<p>echo "Text width is: " . $textWidth . " pixels";<br>
?><br>


4. Example of Dynamic Centering Based on Text Width

Assuming we want to center text horizontally within a 300px wide canvas, we can first use imagettfbbox() to get the text width, then calculate the starting point:

<?php
// Create canvas
$imgWidth = 300;
$imgHeight = 50;
$image = imagecreatetruecolor($imgWidth, $imgHeight);
<p>// Define colors<br>
$bgColor = imagecolorallocate($image, 255, 255, 255);<br>
$textColor = imagecolorallocate($image, 0, 0, 0);</p>
<p>// Fill background<br>
imagefill($image, 0, 0, $bgColor);</p>
<p>// Text and font parameters<br>
$text = "Hello, 世界!";<br>
$fontSize = 20;<br>
$angle = 0;<br>
$fontFile = 'm66.net/fonts/arial.ttf';</p>
<p>// Get text bounding box<br>
$bbox = imagettfbbox($fontSize, $angle, $fontFile, $text);<br>
$textWidth = abs($bbox[2] - $bbox[0]);<br>
$textHeight = abs($bbox[5] - $bbox[1]);</p>
<p>// Calculate X-axis start for horizontal centering<br>
$x = ($imgWidth - $textWidth) / 2;</p>
<p>// Calculate Y-axis start for vertical centering (note imagettftext uses baseline)<br>
$y = ($imgHeight + $textHeight) / 2;</p>
<p>// Draw the text<br>
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, $fontFile, $text);</p>
<p>// Output image<br>
header('Content-Type: image/png');<br>
imagepng($image);<br>
imagedestroy($image);<br>
?><br>


5. Summary

  • imagefontwidth() is only suitable for built-in fonts and offers simple functionality;

  • imagettfbbox() can accurately obtain the bounding dimensions of any TTF font, providing more precise text width measurement;

  • Using imagettfbbox(), you can achieve more flexible text positioning and layout needs, especially for custom fonts and complex text.

With the examples introduced in this article, you can easily replace imagefontwidth() with imagettfbbox() to improve the accuracy and flexibility of image text processing.