Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 858
SteMa Avatar asked Nov 04 '22 19:11

SteMa


1 Answers

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

like image 148
daniel.herken Avatar answered Nov 13 '22 23:11

daniel.herken