RadGH Posted September 16, 2008 Share Posted September 16, 2008 I want to truncate a string that will not fit into a table and append "..." to the end. Currently I just cut it off at 10 characters, but if you see the example below you'll see why that doesn't work: QXQXQXQXQX IJIJIJIJIJ Is there a way (presumably using GD) to get the length of a string in pixels? I thought about writing a string to an image as I would be able to change the font and size, but I don't see an option to have the size of the image automatically stretched to fit. So, is this possible, or is there an alternative solution? I've always been curious how applications were able to detect a font's actual width... Link to comment https://forums.phpfreaks.com/topic/124440-get-string-length-in-pixels/ Share on other sites More sharing options...
Garethp Posted September 16, 2008 Share Posted September 16, 2008 Use imagefontwidth() * strlen($Text); Link to comment https://forums.phpfreaks.com/topic/124440-get-string-length-in-pixels/#findComment-642620 Share on other sites More sharing options...
RadGH Posted September 16, 2008 Author Share Posted September 16, 2008 I looked up that function, is there a way to specify a font and a size? They have one example but it uses a font type 'gdf', the one I was using is ttf. Whats the next step? Link to comment https://forums.phpfreaks.com/topic/124440-get-string-length-in-pixels/#findComment-642629 Share on other sites More sharing options...
Garethp Posted September 16, 2008 Share Posted September 16, 2008 Look up imagefontwidth() on php.net. For changed sizes you do imagefontwidth(1) or imagefontwidth(2) all the way up to five. You can load your own font too, imagefontwidth(1, $Font) once GD already loaded it Link to comment https://forums.phpfreaks.com/topic/124440-get-string-length-in-pixels/#findComment-642640 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.