2010-06-01 21 views
0

我试图用GD + PHP输出多行文本,但无法让它工作。我的PHP知识是非常基本的。 这里是代码,关于如何输出2或3行文本的任何想法?我可以使用GD和PHP多行文本吗?

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

回答

3
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); 

必须添加X像素向下移动它的X位置。请记住,您的整个图像应该足够高并且足够宽以适应文本。

+0

谢谢! 有没有办法将它自动​​分割成几行?例如计算图像的文字或宽度? – alekone 2010-06-01 16:43:19

+0

好,因为ttf字体中每个字符的宽度差别很大,所以存在问题。一种不像ttf字体那么华丽的解决方案,但其中大部分是足够好的将是正常的像素字体。 我写了一个脚本,它前一段时间,你可以在这里找到: http://gist.github.com/421165 只是读它你就会明白它 - 它并不难 - 它也不是很干净或写得很好......;) 你只是传递它的文字和图像的宽度 - 它会完成剩下的工作。玩的开心! :) – Tobias 2010-06-03 01:44:44

+0

有适当的功能来获得“文本”的“边界框”,你可以使用这些。要分割,使用'split';要知道“边界框”,使用'imagettfbbox' ...如果你已经阅读了我在答案中给出的链接,你会看到一些示例,告诉你如何去做。 – ShinTakezou 2010-06-06 06:36:14

0

每行可以重复一个imagettftext;刚刚拆分$theText到一个数组(分隔符是换行)和循环数组中的每个元素,由线(见$textSize的高度递增$textYposition,但确确实实你会得到更好利用imageftbboxRead the page in the PHP manual

0

我有一个未知的字符串长度,但只有一定的宽度来处理,所以我想出了这个,基本上它把这个句子分解为字符,如果它碰到一个空白字符,它会检查这个单词是否可以添加到上一行如果不是,则开始一个新的行,对于超长的单词也有一个蹩脚的安全措施,那些正在被切碎的单词,以便不会从图像中跳出来

在我实际打印文字到图像,我检查是否该行小于允许的最大字符数,并添加前导+尾随空格,以模仿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; 

     } 
    } 

不要忘了在行中添加最后一个单词。您还需要进行检查,看看它是否应该换行。

添加行图像:

//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; 
    }