imagettfbbox

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

imagettfbbox

Post by dreamline »

Hi guyz,
I was wondering if you also had problems with the function imagettfbbox(). I'm trying to calculate the width of a given text, so i can determine wether i need more lines / tablerows to put the text in.

However i can't seem to find the proper code snippet for this purpose. It seems that the imagettfbbox isn't very accurate (or is it me?) in determining the widht of a given text. For instance: when i imagettfbbox this: 29-11-2005 (font: Trebuchet MS 12 px) then the width of the bounding box returned is 74 ($bbox[2]+$bbox[0], however when i watch the same text in adobe then the width is about 58 pixels. That last one seems about correct since my table with is set to 65.

Is there any way to determine exactly how many pixels my text is?

I've been doing a lot of searching however didn't come up with anything.. :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Are you sure you're using 12px in Adobe rather than 12pt? There is a difference. Note: FreeType renders fonts slightly differently from Adobe.

I believe the width is determined by subtracting a left x from a right x, not adding.. although it may be zero based, I'm not sure.



Moved to Graphics.
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

I'm adding the right x value since it is starting at -1, so basiccally if i subtract it then my total value goes up.. I'll have a go and see if i'm using 12 px or 12pt, hope i can get it right since it'll save me a lot of time if i have to come up with a routine myself.. LOL..

Thanks feyd i'll have a look in the px and pt values.. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I know there's a reason why the left starts at -1.. I can't quite remember what though. It had something to do with the paint routine..
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

As far as I can tell, imagettfbbox() is perfectly correct for what imagettftext() draws.. but thats not the same as you'll be drawing in Photoshop. Photoshop has a much more advanced font renderer than the Freetype library, it can mess about with the kerning, hinting, letter spacing and more.

Trying to get PHP to draw text in the same way Photoshop does is a bit of a nightmare.
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

Heheheh... What i did was alt-prtscrn and paste it into photoshop, however since my td width is about 65 pixels and imagettfbbox calculates a position of right: -1 and left: 77, i cant use the imagttfbbox routine to calculate the max nr. of pixels.

I did do some more reading and saw that an earlier gd library worked with pixels and that the upgrade (i believe 2.x) is working with points, so i think feyd is right and that my problem is lying in the point/pixel difference...

So now i'm trying to figure out if there is a general rule for points to pixel conversion.. :) Hopefully i find it, cause i think that's where my problem is and the gdlib i have is working with points while my css stylesheet is in pixels..

:D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

from what I remember, 72pt = 1" on a given display. Since most monitors are 72dpi, then 1pt should be about 1px. In practice it is not however, due to the rendering engine of the browser among other things. Most OS's don't have very good font rendering, so kerning and other things may be wonky on some machines.

I'd just suggest redesigning the page to allow for varying font sizes..
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

Thanks for the info feyd.. I haven't found the right routine yet, so i basically designed a preview screen to view the text and then approve it by setting the number of lines the message is.

It's not a very nifty solution, but it does work.. :) I'll keep looking though... :)
Post Reply