Jump to content

Recommended Posts

Hi,

I've been trying to work with imageftbbox to calculate the length of a string in pixels. Have a look at this page: https://www.baobab.nl/fonts/test.php.

In the test directory I've placed the constantia.tff file that I use to calculate the length of the string. According to imageftbbox the length of string 'How long is this string in pixels' is 442. However, when I grab the text string displayed through css and see what the length is in an image editor I get 358 pixels. 

This is the code I'm using:

 

<style>

.kop {

background-color: #a29e25;

line-height: 30px;

font-family: Constantia;

font-size: 24px;

font-style: normal;

font-variant: normal;

color: #fff;

}

</style>

<?php

$text="How long is this string in pixels";

 

echo "<div class='kop'>".$text."</div>";

list($left,, $right) = imageftbbox( 24, 0, "constantia.ttf", $text);

 

echo $width = $right - $left;

?>

 

What am I doing wrong here?

Any help appreciated,

Garret

Link to comment
https://forums.phpfreaks.com/topic/307225-how-to-work-with-imageftbbox/
Share on other sites

The font size to imageftbbox is points, not pixels.

font-size: 24pt;
Which gives me 435px wide. Not the same but much closer.

 

If that's still not good enough, render the string as an image and compare it to what your browser shows.

When I ran it the length was returned as 431

$str = 'How long is this string in pixels';
list ($left,,$right) = imagettfbbox(24,0,'c:/windows/fonts/constan.ttf',$str);

echo $len = $right - $left;            //--> 431

Yet it will not display without wrapping the text if the div width is less than 435px (which was requinix's result).

 

Looks like my version (7.0.26) of imagettfbbox() is unreliable.

Thanks for the help guys.
When I use pt for the style I still get 442px compared to 478px in the image software. 
Tried with arial as well (https://www.baobab.nl/fonts/arial.php) and get 431 vs 481 so not useable either.
Apparently imageftbbox is not used a lot as I can't find anybody else describing this problem.

cheers

ImageTTFBBox had a long-standing bug where it wasn't very reliable for giving pixel perfect measurements. I remember running into it years ago and ended up doing some of my own measurement by drawing some text on an image then checking for filled vs blank pixels.

 

The bug has been fixed apparently (wasn't last time I checked) so make sure you're on a new enough version of PHP.

 

I just tried on my end by downloading the font file from your site and copying your code. After adjusting from px to pt as pointed out, I get a more or less matching number on PHP 5.6.30. Firefox say the div with the text is 434.95px wide, PHP says the string is 434px wide.

 

edit:

Arial seems fine too for me. Firefox: 435.767; PHP: 435

Impact is slightly different. Firefox: 405.333; PHP: 409

Edited by kicken

Thanks kicken, looks like firefox is doing a way better job at it than chrome. Just tested it in FF and come to 439px but probably have cut of the string quite sharp so 442 measurement of imageftbbox is pretty close. But if the difference between browsers is so big than of course it's not usable.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.