Re: Re: Review: Function that measures the width of a text string in pixels.
| From: | Jacob Friis | Date: | Fri, 29 Oct 2004 00:52:31 +0000 |
| Subject: | Re: Re: Review: Function that measures the width of a text string in pixels. | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-200596@lists.php.net to get a copy of this message | ||
M. Sokolewicz wrote:
Jacob Friis wrote:I know, I had thought about that I could multiply the result according to what I know about the font. It works ok so far for my purpose, which is to measure the width of menu elements in dhtmlcentral.com's coolmenus. /JacobI have created a function that will measure the width of a text string in pixels. It works ok. If you have optimizations, please let me know. Thanks, Jacob function txt_width ($txt) {the width depends on the font used (monospace or not), the kerning used, the size of whitespaces, etc. you can't say "the width of X will be Y" without knowing the font, font size, kerning, etc.$width = 0; $txt_len = strlen($txt); for ($n = 1; $n <= $txt_len; $n++) { switch (substr($txt, $n, 1)) { case 'l'; case '.'; case ' '; case '-'; case 't'; $width += 3; break; default : $width += 8; break; } } return $width;}