Search code examples
phpfontssizepixeltruetype

Get the width and height of a string in pixel (PHP)


Is there any good function to calculate the pixel width and height? Can imagettfbbox be used to do that? I mean which ttf file is needed for the different fonts used by different browsers?

Thx


Solution

  • As PHP run's on the server there is no way to do that with PHP alone. The size of the text depends on the operating system, the browser (and the browser settings like zoom) of the client. You could however use javascript to get the size of an element once its rendered.

    height = document.getElementById("elementId").style.height; // Will return 176px for example
    width = document.getElementById("elementId").style.width; // Will return 176px for example
    

    Hope this helps