使用多行的 imagettftext 函数?

Posted

技术标签:

【中文标题】使用多行的 imagettftext 函数?【英文标题】:Using the imagettftext function with multiple lines? 【发布时间】:2011-05-29 06:17:26 【问题描述】:

我正在使用 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);


?>

【问题讨论】:

【参考方案1】:

试试这个:

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

【讨论】:

今天帮助了我。 2年后... :-)【参考方案2】:

只需在空格上分解文本以获得单词数组,然后通过遍历单词数组开始构建行,通过 imagettfbbox 测试每个新单词的添加,看看它是否创建了超过您设置的 maxwidth 的宽度。如果是这样,请在新的一行开始下一个单词。我发现简单地创建一个添加了特殊换行符的新字符串会更容易,然后再次分解该字符串以创建一个行数组,您将分别将每一行写入最终图像。

类似这样的:

$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.=' ';
  

管道字符是换行符。

【讨论】:

这段代码工作得非常好,但我只有一个问题......然后你如何让每一行分开? 而不是迭代行,只需在行太长时添加 PHP_EOL,如下所示:if ($lineWidth &lt; $maxLineWidth) $newText .= $value . ' '; else $newText .= PHP_EOL . $value; 【参考方案3】:

在所有发布的答案中,我最喜欢 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);

下面是一个带有较长文本的前后示例:

【讨论】:

可以,但是要居中对齐文本并更改行高?【参考方案4】:

如果你的字符串没有空格,你可以试试这个:

 $text = 'Cool Stuff!thisisniceLALALALALALALAHEEHEHEHE';
 $text = wordwrap($_POST['title'], 15, "\n",true); //TRUE = Wrap

【讨论】:

以上是关于使用多行的 imagettftext 函数?的主要内容,如果未能解决你的问题,请参考以下文章

C++/Java/Python 中的 PHP imagettftext() 等效函数

文本的原点:imagettftext

imagettftext

php在linux下call to undefined function imagettftext()

我可以通过 imagettftext() 使用可变字体的功能吗?

imagettftext 中的像素字体大小而不是磅值