LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   variable length string using GD (word wrap, carriage return, word/character count)? (http://www.linuxquestions.org/questions/programming-9/variable-length-string-using-gd-word-wrap-carriage-return-word-character-count-704683/)

frieza 02-14-2009 03:21 PM

variable length string using GD (word wrap, carriage return, word/character count)?
 
here's the problem
i'm trying to take elements of an array (each element is a separate string) and trying to render the strings with imagettftext

the problem is of course gd only renders so many pixels based on the width and height of the initial image so if i try to output a string that exceeds that width it gets cut off

in theory i could use strpos to cut it pieces for output
or i could use str_word_count() to break it into words and output so many
words per line by concatinating them with a separating " "

however both options only work if the text is static and always will have the same amount of total characters and the same sized words or one iteration might look fine and another might have words chopped off in the middle

perhaps somehow a combination of both? or does gdlib have a way of emulating word wrap?
edit: p.s. i have 40 columns to play with

frieza 02-14-2009 05:21 PM

nvm i seem to have figured it out, if anyone can suggest how to improve uppon this method i would appreciate it ;)
Code:

$pos=0;
foreach($array as $key => $value) {
if ($pos == 0) {
$linepos=180;
} else {
$linepos = $linepos + 20;
}
while (strlen($string[$key]) <=38) {
$string[$key] .=$array[$pos]." ";
$pos++;
}
imagettftext($bigger,15  , 0  , 20  , $linepos  , $grey  , "./arial.ttf"  , $string[$key]);
}



All times are GMT -5. The time now is 10:53 AM.