Jump to content


GD Text to Image

  • Please log in to reply
3 replies to this topic

#1 CanMan2004

  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 03 September 2006 - 05:11 PM

Hi all

I have a simple text to image GD script

header ("Content-type: image/png"); 
$handle = ImageCreate (275, 386) or die ("Cannot Create image"); 
$bg_color = ImageColorAllocate ($handle, 255, 255, 255); 
$txt_color = ImageColorAllocate ($handle, 0, 0, 0); 
ImageTTFText ($handle, 8, 180, 270, 360, $txt_color, "arial.ttf", "".$_GET['txt'].""); 
ImagePng ($handle); 

It works when passing the URL

www.mysite.com/website/image.php?txt=this is a test

The script works fine, my question is, how can I get the text to word wrap, as it currently keeps all text onto the same line.

Secondly, how can I get GD to understand line breaks in the url, for example, if I pass the text

this is a text<br>this is another test<br>this is the last test

then it shows all that on one line, and prints in the text <br>

that is the same if you replace <br> with \n

Any help would be great

Thanks in advance


#2 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 03 September 2006 - 05:18 PM

i only have simple knowledge of the gd library, just enough to do a little customization of captcha type scripts, but i don't think the gd library specifically supports that. I think you are going to have to write your own function with conditions and some regexing and then create the image with the text using specific x,y coordinates, based on # of lines, etc... but hey, i could be wrong. there might be some gd function already made that does all that for you. 

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 AndyB

  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 September 2006 - 06:05 PM

gd alone can't do what you want.

Pass the multi-line text using an obvious delimiter between lines.
At the start of your gd script, use the explode() function to separate that multi-line text into single lines.
The size of the exploded array will let you define the vertical co-ordinate for each separate line
Loop thru' theImageTTFtext as many times as you have separate lines
Legend has it that reading the manual never killed anyone.
My site

#4 CanMan2004

  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 03 September 2006 - 07:13 PM


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users