Search code examples
phpfontsgd

Generated image with text always off-center


I have a simple PHP avatar generator that takes someone's username, and makes an image with their initials on it. This works well, but somehow the text is always off center and I cannot figure out have to always have the text properly centered.

This uses the Ubuntu font.

$im = imagecreatetruecolor(125, 125);

$str = 'dpoint';
if (str_word_count($str) > 1)
{
    $words = explode(' ', $str);
    $text = $words[0][0]. $words[1][0];
}
else
{
    $text = substr($str, 0, 2);
}

$font_size = '50';
$white = imagecolorallocate($im, 255, 255, 255);
$angle = 0;

// Get image dimensions
$width = imagesx($im);
$height = imagesy($im);
// Get center coordinates of image
$centerX = $width / 2;
$centerY = $height / 2;
// Get size of text
list($left, $bottom, $right, , , $top) = imageftbbox($font_size, $angle, './Ubuntu-L.ttf', $text);
// Determine offset of text
$left_offset = ($right - $left) / 2;
$top_offset = ($bottom - $top) / 2;
// Generate coordinates
$x = $centerX - $left_offset;
$y = $centerY + $top_offset;
// Add text to image
imagettftext($im, $font_size, $angle, $x, $y, $white, './Ubuntu-L.ttf', $text);

// Set the content type header
header('Content-Type: image/jpeg');

// Skip the file parameter using NULL, then set the quality
imagejpeg($im, NULL, 100);

// Free up memory
imagedestroy($im);

That for example ends up like this: enter image description here You can see it's much lower down that it should be. What positioning am I doing wrong here?

Ideally I just want it so no matter the text size or lower-case or upper-case, it just centers it vertically and horizontally properly inside the box.


Solution

  • As @Olivier has correctly answered the y coordinate is the baseline.

    But, as the $angle is 0 and the bounding-box at hand, we only need to correct by an upper y value, e.g. offsets 5 or 7:

            Corners of GD Bounding Boxes
                                                   x,y
         (4)<--(3)       (1) lower-left corner     0,1
          :     ^        (2) lower-right corner    2,3
          .     |        (3) upper-right corner    4,5
         (1)-->(2)       (4) upper-left corner     6,7
    

    The value is negative by the number of pixels above the baseline.

    $y = $centerY - $top - $top_offset;
    

    It can make sense to also do an $offsetX correction (upper-left.x, offset 6), but it is often minimal only:

    $x = $centerX - $offsetX + intdiv($offsetX, 2) - $left_offset;
    

    Images with these two corrections (X+Y), and the projected bounding box from the imageftbbox() function as a red rectangle on top:

    Image Image Image Image
    "dp" "DP" "pp" "dd"
    "gd \n lib" "lib \b gd" ""ngty \n sqd" "7y"