[Resolved] GD Library - imagettfbbox() function help needed

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
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

[Resolved] GD Library - imagettfbbox() function help needed

Post by kaisellgren »

Hi,

I'm getting angry today I have spent too much time on this one for nothing :(

For some reason I get different $text_width for letters '9' and '8' :S

I've tried all these and actually all these code did the same - no difference:

Code: Select all

$image_data = imagettfbbox(8,0,$font,$highest.'0');
$text_width = max($text_data[2],$text_data[4])-min($text_data[0],$text_data[6]);

$image_data = imagettfbbox(8,0,$font,$highest.'0');
$text_width = abs($image_data[2]-$image_data[0]);

$image_data = imagettfbbox(8,0,$font,$highest.'0');
$text_width = abs($image_data[2])+abs($image_data[0]);
Does anyone have a solution for this? I need to get the width of the text to align my texts properly.
Now the texts are being offset by 0-3 pixels depending on the letters :S
Last edited by kaisellgren on Mon Apr 09, 2007 3:42 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Individual characters have varying widths, there's little you can do about that.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

Hmm.

When I have for instance numbers 900 and 800, it's shifting the 800 more left than the 900 :/

Do you have any ideas or let's say 'tricks' to get around this?

Hmm. Perhaps I could count the numbers with strlen() and then multiply it with static width of character 0 ? Yeah, I'll try that one :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you want all the characters to fall into a specific grid position them based on the width of the w/m (case may matter here) .. I can't recall offhand, but one of them is used as a general width (because they're the widest characters in most instances.)
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

Thanks for the tip.

I figured out the character widths.

upper case W is the widest character and lower case l (L) is the thickest character.

If someone else is (going to) have/having this same problem and found this thread, I better explain my solution:

1) Take the width of the widest character W:

Code: Select all

$image_data = imagettfbbox($font_size,$font_angle,$font,'W');
$text_width_base = abs($image_data[2]-$image_data[0]);
2) To get a width of string 'abcdefghjilkml' for instance, do this:

Code: Select all

$string = 'abcdefghijklm';
$text_width = strlen($string)*$text_width_base;
Post Reply