Page 1 of 1
Calculating the vertical size of text?
Posted: Thu Sep 23, 2004 3:35 pm
by kolleraa
What's the best way to calculate the vertical space (in lines of text) taken up by an arbitrary string $string (which may contain newline characters) that is placed into a td cell of width x pixels?
Say using Verdana 12px, 1024x768, and default wrapping behavior.
thanks[/mail_search]
Posted: Thu Sep 23, 2004 4:27 pm
by Christopher
Depends if you want quick or accurate. For speed you can just count spaces and multply by by an average word length. Most accurate would be to loop through the string, adding up widths and wrapping lines.
Posted: Thu Sep 23, 2004 4:42 pm
by feyd
or the easy way: [php_man]imageftbbox[/php_man] and [php_man]imagettfbbox[/php_man]
Posted: Thu Sep 23, 2004 5:17 pm
by kolleraa
feyd, thanks for pointing out those functions. However, I don't understand how these functions could be used to determine the height of text that is constrained by the bounds of a table cell... seems like they just multiply the height of the text font by the number of lines in the text, and they seem to ignore html markup. Is there some way around this?
thanks
Posted: Thu Sep 23, 2004 9:46 pm
by feyd
you can use javascript to determine the pixel heights. However, if your design needs that kind of precision, it's time to rethink the design.