如何使用imagettftext函数实现多行文本?

8

我正在使用PHP创建透明文本->png图像,目前一切顺利。唯一的问题是,我希望能够在固定宽度下换行或在文本中插入断行符号。是否有人有相关经验?以下是我的代码...

<?php

$font = 'arial.ttf';
$text = 'Cool Stuff! this is nice LALALALALA LALA HEEH EHEHE';
$fontSize = 20;

$bounds = imagettfbbox($fontSize, 0, $font, $text); 

$width = abs($bounds[4]-$bounds[6]); 
$height = abs($bounds[7]-$bounds[1]); 



$im = imagecreatetruecolor($width, $height);
imagealphablending($im, false);
imagesavealpha($im, true);


$trans = imagecolorallocatealpha($im, 255, 255, 255, 127);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);


imagecolortransparent($im, $black);
imagefilledrectangle($im, 0, 0, $width, $height, $trans);


// Add the text
imagettftext($im, $fontSize, 0, 0, $fontSize-1, $grey, $font, $text);


imagepng($im, "image.png");
imagedestroy($im);


?>
4个回答

22

试一试:

$text = 'Cool Stuff! this is nice LALALALALA LALA HEEH EHEHE';
$text = wordwrap($_POST['title'], 15, "\n");

4
今天他/她帮助了我。两年后... :-) - Preben

6
只需将文本按空格拆分为单词数组,然后通过循环单词数组开始构建行,通过imagettfbbox测试每个新单词的添加是否会创建超过您设置的最大宽度的宽度。如果是,则在全新的一行上开始下一个单词。我发现更容易的方法是创建一个带有特殊换行符的新字符串,然后再次将该字符串拆分为行数组,您将分别将每个行写入最终图像中。类似以下内容:
$words = explode(" ",$text);
$wnum = count($words);
$line = '';
$text='';
for($i=0; $i<$wnum; $i++){
  $line .= $words[$i];
  $dimensions = imagettfbbox($font_size, 0, $font_file, $line);
  $lineWidth = $dimensions[2] - $dimensions[0];
  if ($lineWidth > $maxwidth) {
    $text.=($text != '' ? '|'.$words[$i].' ' : $words[$i].' ');
    $line = $words[$i].' ';
  }
  else {
    $text.=$words[$i].' ';
    $line.=' ';
  }
}

这里的管道符号是换行符。


这段代码运行得很棒,但我只有一个问题......如何将每一行分开? - Joshua Curci
2
不要迭代行,当行太长时只需添加PHP_EOL,如下所示:if ($lineWidth < $maxLineWidth) { $newText .= $value . ' '; } else { $newText .= PHP_EOL . $value; } - Yevgeniy

2

在所有发布的答案中,我最喜欢 Genius in trouble 's,但它只是在每15个字符后添加一个换行符,而不是像现代文字处理器一样让文本“流动”,具体取决于字体选择和使用哪些字符(例如,小写字母L占用的水平空间比大写字母W少- l vs. W)。

我想出了一个解决方案,并将其作为开源发布在https://github.com/andrewgjohnson/linebreaks4imagettftext

要使用,您只需更改:

$font = 'arial.ttf';
$text = 'Cool Stuff! this is nice LALALALALA LALA HEEH EHEHE';
$fontSize = 20;
$bounds = imagettfbbox($fontSize, 0, $font, $text); 
$width = abs($bounds[4]-$bounds[6]); 

至:

$font = 'arial.ttf';
$text = 'Cool Stuff! this is nice LALALALALA LALA HEEH EHEHE';
$fontSize = 20;
$bounds = imagettfbbox($fontSize, 0, $font, $text); 
$width = abs($bounds[4]-$bounds[6]);

// new code to add the "\n" line break characters to $text
require_once('linebreaks4imagettftext.php'); //https://raw.githubusercontent.com/andrewgjohnson/linebreaks4imagettftext/master/source/linebreaks4imagettftext.php
$text = \andrewgjohnson\linebreaks4imagettftext($fontSize, 0, $font, $text, $width);

这里是一个示例,展示了一段较长文本的前后对比:

Example


1
它可以工作,但如何将文本居中并更改行高? - Ishtiyaq Husain

-2
如果您的字符串中没有任何空格,您可以尝试这个方法:
 $text = 'Cool Stuff!thisisniceLALALALALALALAHEEHEHEHE';
 $text = wordwrap($_POST['title'], 15, "\n",true); //TRUE = Wrap

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接