How to determine the length (in pixels) of a string being rendered on a web page?

前端 未结 5 631
盖世英雄少女心
盖世英雄少女心 2020-12-14 17:19

If I know the font size (12) and the font family (calibri), is there a way to determine the length (in pixels) a given string will take after it will be rendered? I am absol

5条回答
  •  离开以前
    2020-12-14 17:37

    You could use PHP GD to render out the string as a image and get the size of the resulting image.

    If you use this, it should work:

       list($left,, $right) = imageftbbox( 12, 0, "arial.ttf", "Hello World");
    
       $width = $right - $left;
    

提交回复
热议问题