Page 1 of 1

Center variable text length on an image

Posted: Thu Apr 03, 2008 9:34 pm
by Beauford
I am completely new to GD and having a minor problem. I have been able to get text on an image and it looks great - problem is that the text will always be a different length and as such the text will not be centered everytime.

How do I set it up so the starting pixel is variable depending on the length of the string. The image is always going to be 800x600.

This is what I am using, but obviously this is not variable.

imagettftext($TheImage, 30, 0, 400, 260, $ColorText, "Arial.ttf", $name);

Also, as a side note. I have an arial.ttf file in the same folder as the script, but it doesn't seem to see it, is there a type of font specific to GD that needs to be used or can it be any font file.

Thanks

Re: Center variable text length on an image

Posted: Thu Apr 03, 2008 10:18 pm
by John Cartwright

Re: Center variable text length on an image

Posted: Fri Apr 04, 2008 3:08 am
by JayBird

Re: Center variable text length on an image

Posted: Fri Apr 04, 2008 3:13 am
by onion2k
JCart was nearly right. imagefontwidth() and imagefontheight() only work on GD's built in fonts though. You need to use imagettfbbox(). It'll return an array of coordinates ... if you calculate the difference between indexes 0 and 2 (assuming the text isn't angled) you can get the width of the rasterised string, from which you can work out where to put it in order to centre it.

Re: Center variable text length on an image

Posted: Fri Apr 04, 2008 5:51 pm
by Beauford
JayBird wrote:Moved this to PHP - Code

Please post in the correct forum in future
My apologies, but is this not general discussion? I see a wide variety of topics here and assumed mine would be appropriate here.

Re: Center variable text length on an image

Posted: Fri Apr 04, 2008 9:45 pm
by John Cartwright
Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.
Notice the bolded text in our the description of General Discussion ;)