Calculating the vertical size of text?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kolleraa
Forum Newbie
Posts: 2
Joined: Thu Sep 23, 2004 3:21 pm

Calculating the vertical size of text?

Post 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]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

or the easy way: [php_man]imageftbbox[/php_man] and [php_man]imagettfbbox[/php_man]
kolleraa
Forum Newbie
Posts: 2
Joined: Thu Sep 23, 2004 3:21 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply