GD Text Alignment

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bluesguitarman25
Forum Newbie
Posts: 8
Joined: Tue Jul 03, 2007 9:22 am
Location: PA

GD Text Alignment

Post by bluesguitarman25 »

I am having trouble aligning text specifically where I want it over a jpeg. Can anyone help?
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

It's a little hard to help without some code to see where the problem may be.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Where do you want it?
Where are you putting it?
What code is doing it?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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
bluesguitarman25
Forum Newbie
Posts: 8
Joined: Tue Jul 03, 2007 9:22 am
Location: PA

Sorry....

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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. :P
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
bluesguitarman25
Forum Newbie
Posts: 8
Joined: Tue Jul 03, 2007 9:22 am
Location: PA

Rock On!!

Post 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!
Post Reply