GD Text Alignment
Moderator: General Moderators
-
bluesguitarman25
- Forum Newbie
- Posts: 8
- Joined: Tue Jul 03, 2007 9:22 am
- Location: PA
GD Text Alignment
I am having trouble aligning text specifically where I want it over a jpeg. Can anyone help?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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
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....
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:
Thanks
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);
?>- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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!!
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!