views:

142

answers:

3

hello! I'm trying to output multiline text with GD+PHP but can't get it working. my php knowledge is really basic. here's the code, any idea on how to output 2 or 3 lines of text?

$theText = (isset($_GET['caption']))? stripslashes($_GET['caption']) :'';
imagettftext($baseImage, $textSize, $textAngle, $textXposition, $textYposition, $textColor, $fontName, $theText);
A: 
imagettftext($baseImage, $textSize, $textAngle, $textXposition, $textYposition, $textColor, $fontName, $theText);
imagettftext($baseImage, $textSize, $textAngle, $textXposition+(25), $textYposition, $textColor, $fontName, $theText);
imagettftext($baseImage, $textSize, $textAngle, $textXposition+(50), $textYposition, $textColor, $fontName, $theText);

you have to add x pixel to move it downwards to the X position. keep in mind that your whole image should be high and wide enough to fit the text.

Tobias
thank you!is there a way to split it automatically into lines? for instance counting the words or the width of the image?
alekone
well there is a problem because the width of every character within a ttf font differ a lot. a solution which is not as fancy as ttf fonts but mostly is good enough would be normal pixel fonts.i wrote a script for it a while ago which you can find here:http://gist.github.com/421165just read it over you will understand it - its not very hard - its also not very clean or nicely written so... ;)you just pass it text and the width of the image - it will do the rest. have fun! :)
Tobias
there are proper functions to get the "bounding box" of a "text", you can use those. To split, use `split`; to know the "bounding box", use `imagettfbbox`... if you'd have read the link I've given in my answer, you would see examples that suggests you how to do it.
ShinTakezou
A: 

It's not supported by API. Here is the code to do it "manually":

http://php.net/manual/en/function.imagettftext.php#75718

vartec
ok that's a start! I will try to work out the code in this link. thank you!
alekone
A: 

You can repeat one imagettftext per line; just split $theText into an array (separator is the NewLine) and loop for each element in the array, incrementing $textYposition by the height of the line (see $textSize, but indeed you'll get it better using imageftbbox. Read the page in the PHP manual

ShinTakezou