|
Im nearly 100% sure that this isnt possible, but I've been proved wrong by you dudes before so I'll ask anyway.
Is there any function that can calculate the width of a string in pixels, with a specific font. I have a function which uses the GD Image Library to create text headers with a grunge font, which I can use on my site without having to create a new image for each header and upload it etc. its done at runtime. But, the problem I have is that if I want to center an image its slightly off center, because the width of the image is strlen($text)*11.5 But, that isnt precise enough, it leaves about a 50/60 pixel gap (or bigger, if the image is bigger) at the edge of the image. I would make the *11.5 smaller, but for smaller headers it cuts some off that way. 11.5 is the lowest I can go without destroying smaller headers.
I have created my own function by measuring each character in the font and inputting it into an array, but that doesnt work any better due to the grunge effect, some letters merge into the last, some are wider etc., which would be fine, except that it makes some characters space out more or less, depending on the previous character, making the function obselete in that sometimes its too short, mostly its too wide still.
So, again in a shortened form, is there a string_width type of function?
|