This tutorial show you on how to get the font width and height using GD library.
  1. Copy arial.ttf file to a folder accessible from the code.
  2. The following code will load the arial.ttf font file, and calculate the size using imagettfbbox function.
    //Load the font file
    $font = "arial.ttf";
    //Set the font size
    $font_size = 48;
    //Set the text;
    $text = "A";
    //Calculate the font size
    $dimension = imagettfbbox($font_size, 0, $font, $text);
    $text_width = $dimension[2] - $dimension[0];
    $text_height = $dimension[1] - $dimension[7];
    //print the font size
    echo "Font width:" . $text_width . "px<br/>";
    echo "Font height:" . $text_height . "px<br/>";
    Font width:0px
    Font height:0px