Jump to content


This topic is now archived and is closed to further replies.


GD Text to Image

Recommended Posts

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


Share this post

Link to post
Share on other sites
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. 

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.