Page 1 of 1
GD Text Alignment
Posted: Tue Jul 03, 2007 9:25 am
by bluesguitarman25
I am having trouble aligning text specifically where I want it over a jpeg. Can anyone help?
Posted: Tue Jul 03, 2007 9:27 am
by TheMoose
It's a little hard to help without some code to see where the problem may be.
Posted: Tue Jul 03, 2007 9:27 am
by superdezign
Where do you want it?
Where are you putting it?
What code is doing it?
Posted: Tue Jul 03, 2007 10:12 am
by onion2k
If you're using imagestring(), use imagefontwidth() to figure out the size and use that to calculate the placement.
If you're using a TTF font, use imagettfbbox() to get the size. I wrote a bit about the basics off TTF bounding boxes here:
http://www.phpgd.com/articles.php?article=7
Sorry....
Posted: Tue Jul 03, 2007 3:59 pm
by bluesguitarman25
ok, this partially does what I need but the font is too small and I need to be able to place several bits of text from a form in several places on the image. So what I'm asking is someone to explain imagesy() and imagesx(). Here's what I have:
Code: Select all
<?php
header("Content-type: image/png");
$string = $_POST['text'];
$im = imagecreatefrompng("image.png");
$orange = imagecolorallocate($im, 220, 210, 60);
$px = (imagesx($im) - 7.5 * strlen($string) / 2);
imagestring($im, 3, $px, 9, $string, $orange);
imagepng($im);
imagedestroy($im);
?>
Thanks
Posted: Tue Jul 03, 2007 4:41 pm
by superdezign
imagesx and imagesy give you with width and height, respectively. Take half of that, and you have the center of the image. Take half of the width of the font, and you have the center of the font. Put them together, and you have the center.

Posted: Tue Jul 03, 2007 5:24 pm
by onion2k
The '3' in imagestring($im, 3, $px, 9, $string, $orange); refers to the font. There's 6 of them built in (numbered 1,2,3,4,5,6 obviously). 6 is the biggest one. If you need to use a bigger font than that your best option is imagettftext(), but that needs the freetype library to be installed. If it isn't available then things get tricky.
Rock On!!
Posted: Tue Jul 03, 2007 8:39 pm
by bluesguitarman25
Thanks to several hours of hacking away and some help from ya'll, I did what I needed to do. I will post the code when I am finished. Should be quite helpful to several people/businesses. Aloha!