问题
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);
回答1:
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.
回答2:
It's not supported by API. Here is the code to do it "manually":
http://php.net/manual/en/function.imagettftext.php#75718
回答3:
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
回答4:
I had an unknown length of string, but only a certain width to work with. So I came up with this. Basically it splits the sentence in chars. If it bumps on a whitespace, it checks if the word can be added to the previous line, if not, it starts a new line. There is also a lame safety for extra long words, who are just being chopped of, so as not to go out of the image.
In the stage where I actually print the text to the image, I check if the line is smaller than the maximum allowed characters and add leading + trailing whitespaces, to mimic text-align: center.
# Split up the lines
$arrMessage = str_split(stripcslashes($strMessage));
$arrTemp = array();
$line = 0;
$word = array();
$arrTemp[$line] = array();
foreach($arrMessage as $char){
//if we hit a space, see if we should continue line, or make a new line
if($char == " ")
{
//calculate numbers of chars currently on line + number of chars in word
$numTotalChars = (int) count($word) + (int) count($arrTemp[$line]);
//if total > 14 chars on a line, create new line
if($numTotalChars > 14)
{
$line++;
$arrTemp[$line] = array();
}
$word[] = $char;
//push word-array onto line + empty word array
$arrTemp[$line] = array_merge($arrTemp[$line], $word);
$word = array();
}
else
{
//if word is too long for a line, split it
if( count($word) > 16)
{
$numTotalChars = (int) count($word) + (int) count($arrTemp[$line]);
if($numTotalChars > 16)
{
$line++;
$arrTemp[$line] = array();
}
$arrTemp[$line] = array_merge($arrTemp[$line], $word);
$word = array();
}
$word[] = $char;
}
}
Don't forget to add the last word to a line as well. You also need to the the check to see if it should be on a new line or not.
Add lines to image:
//add some px to x and y for every new line
$pos_x = $font->position[0];
$pos_y = $font->position[1];
$numLineHeight = 20;
$addToX = 0;
if($font->angle > 5)
{
$addToX = 2;
}
else if($font->angle < 0)
{
$addToX = -2;
}
# ADD MESSAGE
foreach($arrTemp as $arrLine){
//leading/trailing whitespace (==center text)
$numCharsOnThisLine = count($arrLine);
$extraWhiteSpace = 14 - $numCharsOnThisLine;
$frontBackSpace = floor($extraWhiteSpace / 2);
for($i = 0; $i < $frontBackSpace; $i++){
array_unshift($arrLine, " ");
$arrLine[] = " ";
}
//make string from char array
$strLine = implode("", $arrLine);
imagettftext ($image, $font->size, $font->angle, $pos_x, $pos_y, $tlt, $font->family, $strLine);
$pos_x = $pos_x + $addToX;
$pos_y = $pos_y + $numLineHeight;
}
来源:https://stackoverflow.com/questions/2951452/can-i-have-multiline-text-with-gd-and-php