Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
String Width in pixels (Im sure it aint possible :( )
Old 10-29-2005, 12:49 PM String Width in pixels (Im sure it aint possible :( )
AxE
AxE's Avatar
Skilled Talker

Posts: 62
Trades: 0
Im nearly 100% sure that this isnt possible, but I've been proved wrong by you dudes before so I'll ask anyway.

Is there any function that can calculate the width of a string in pixels, with a specific font. I have a function which uses the GD Image Library to create text headers with a grunge font, which I can use on my site without having to create a new image for each header and upload it etc. its done at runtime. But, the problem I have is that if I want to center an image its slightly off center, because the width of the image is strlen($text)*11.5 But, that isnt precise enough, it leaves about a 50/60 pixel gap (or bigger, if the image is bigger) at the edge of the image. I would make the *11.5 smaller, but for smaller headers it cuts some off that way. 11.5 is the lowest I can go without destroying smaller headers.

I have created my own function by measuring each character in the font and inputting it into an array, but that doesnt work any better due to the grunge effect, some letters merge into the last, some are wider etc., which would be fine, except that it makes some characters space out more or less, depending on the previous character, making the function obselete in that sometimes its too short, mostly its too wide still.

So, again in a shortened form, is there a string_width type of function?
AxE is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-29-2005, 02:55 PM
Extreme Talker

Posts: 169
Trades: 0
Well I think you have to write the function yourself.
__________________

Please login or register to view this content. Registration is FREE

TOP 10 WEB HOSTS COMPARED

BEST RESELLER HOSTING
john551 is offline
Reply With Quote
View Public Profile
 
Old 10-29-2005, 06:06 PM
AxE
AxE's Avatar
Skilled Talker

Posts: 62
Trades: 0
As I've said I already tried that.. I was hoping for a prebuilt function which would allow me to specify the font etc. but it doesnt seem like there is one.
AxE is offline
Reply With Quote
View Public Profile
 
Old 10-29-2005, 06:10 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
There is a function (imagefontwidth and imagefontheight), but they only return the size of one character using the built-in fonts (which are all fixed-width). You'll get the width/height of the entire string by multiplying the number returned by the number of characters.

I don't know of any way that lets you get the width or height of a string using a custom font, but I'm interested in this too.
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 10-29-2005, 06:19 PM
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
Ah.. If you are using TrueType fonts, then the function ImageTTFBox will return the coordinates of the corners for you.

If you're using built in fonts, then they're easy to calculate (as I said above).
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Old 10-29-2005, 07:15 PM
AxE
AxE's Avatar
Skilled Talker

Posts: 62
Trades: 0
TTF Indeed!

Again, proven wrong :P


Thank you ALOT! You just made my life alot easier :P

EDIT: Bugger. Due to the grunge (bits poking out etc.) of the font it reports the letters wider than they are, because when theyre actually written the bits that poke out merge with the previous character, for some reason.... this makes the image wider than it needs to be =(

Oh well

Last edited by AxE; 10-29-2005 at 07:39 PM..
AxE is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to String Width in pixels (Im sure it aint possible :( )
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.95054 seconds with 12 queries