Jump to content

How to work with imageftbbox


GarretD

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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.