GarretD Posted April 29, 2018 Share Posted April 29, 2018 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 More sharing options...
requinix Posted April 29, 2018 Share Posted April 29, 2018 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 More sharing options...
Barand Posted April 29, 2018 Share Posted April 29, 2018 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 More sharing options...
requinix Posted April 29, 2018 Share Posted April 29, 2018 With Windows's constan.ttf I get 431 too in 7.0, 7.1, and 7.2. Using constantia.ttf from the site gives me 434. Link to comment Share on other sites More sharing options...
Barand Posted April 29, 2018 Share Posted April 29, 2018 Sounds like the font metrics in the constan.ttf file are a bit off. EDIT: Just downloaded a copy of "constantia.ttf". Now it gives 466. Go figure! Link to comment Share on other sites More sharing options...
GarretD Posted April 30, 2018 Author Share Posted April 30, 2018 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 Link to comment Share on other sites More sharing options...
kicken Posted April 30, 2018 Share Posted April 30, 2018 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 More sharing options...
GarretD Posted April 30, 2018 Author Share Posted April 30, 2018 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 More sharing options...
GarretD Posted April 30, 2018 Author Share Posted April 30, 2018 IE 11: 441pxSafari 5.1.7: 439pxOpera 12: 439px Seems like chrome is the odd one out Link to comment Share on other sites More sharing options...
requinix Posted April 30, 2018 Share Posted April 30, 2018 See if imagettfbbox gives you different results. Rendering is not an exact science. Results vary. If you want pixel-perfect precision with any internet technology then you're setting yourself up to fail. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.