Page 1 of 1

strlen() in pixels?!

Posted: Thu Feb 26, 2004 9:27 pm
by Illusionist
Is there a way i can return the len of a string in pixels? Or somehow convert the length to pixels? Im doing this because i want to resize the background of the image according to the length of the text.... Anyoen got any ideas?

Posted: Thu Feb 26, 2004 9:32 pm
by d3ad1ysp0rk

Code: Select all

$baseh = 20;
$basew = 10;
$length = strlen($text);
$basew = $basew * $length;
wouldn't that work? just makes the image bigger for each character?
also it'd work best with a fixed width font (all characters take up one block, unlike ariel, comic sans, etc..).

Posted: Thu Feb 26, 2004 9:36 pm
by Illusionist
whats how i was doing it to ebgin with, but sometimes it would be perfect, but then others with smaller strings it'd be too wide! And ya i thought about using a fixed font, but... i dunno... If there's no other way i will... But surely there is something!

Posted: Thu Feb 26, 2004 11:31 pm
by Bill H
Can you do something with a "nowrap" table cell and let the text determine the cell width?

Just a thought, probably worth 2 cents.

Posted: Fri Feb 27, 2004 3:16 am
by Wayne
you could either create the image with GD libraries then you could check the size of the string and resize, or you could put the image as a background for a table cell <TD> and make it 100% and put the text in the cell .... just a couple of ideas

Posted: Fri Feb 27, 2004 8:41 am
by evilMind
Have you treid to do a strlen($bodyOfText); then multiply the result by the scaling ratio of the font you are using?

Posted: Fri Feb 27, 2004 9:33 am
by vigge89
there is a GD library funvtion for this, but i can't remember which...

Posted: Fri Feb 27, 2004 10:04 am
by Wayne
ImageTTFBBox() but it only works if you are writing the text on/in an image.

Be careful using a scaling ratio, remember that people can override your font sizes in the browser!

Posted: Fri Feb 27, 2004 11:18 am
by Weirdan
Heh, just do it in css (and ask for help in Client-Side forum :D).

Posted: Fri Feb 27, 2004 11:38 am
by Illusionist
evilMind i said i have already tried that. And yes im writing on an image, so ill try the ImageTTFBBox() function when i get home.
Thanks