strlen() in pixels?!

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
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

strlen() in pixels?!

Post 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?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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..).
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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!
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post 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.
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

Have you treid to do a strlen($bodyOfText); then multiply the result by the scaling ratio of the font you are using?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

there is a GD library funvtion for this, but i can't remember which...
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post 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!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Heh, just do it in css (and ask for help in Client-Side forum :D).
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

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