我正在使用gd库在图像上生成段落。但我仍然没有找到任何答案。 这是我正在尝试的代码。谢谢
<?php
header ('Content-Type: image/jpeg');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
$text = "gfhgfhg
hgf hgf hf hj
hgfhgfhgf hhfh
ffgfhgf hgf hgf
gfhgfhfhfh";
imagestring($im, 1, 5, 5, $text, $text_color);
imagejpeg($im, 'palette.jpg');
imagedestroy($im);
?>
答案 0 :(得分:0)
尝试:
$text = "gfhgfhg\r\n
hgf hgf hf hj\r\n
hgfhgfhgf hhfh\r\n
ffgfhgf hgf hgf\r\n
gfhgfhfhfh";
答案 1 :(得分:0)
您应该根据文本中的行数调整图像的高度,并分别输出每一行:
$text = "gfhgfhg
hgf hgf hf hj
hgfhgfhgf hhfh
ffgfhgf hgf hgf
gfhgfhfhfh";
$lines = explode("\n", $text);
$letterHeight = 8;
$lineSpacing = 2;
$padding = 10;
$height = count($lines) * ($letterHeight + $lineSpacing) + $padding;
$im = @imagecreatetruecolor(120, $height)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
foreach ($lines as $i => $line) {
imagestring($im, 1, 5, 5 + $i * ($letterHeight + $lineSpacing), $line, $text_color);
}
imagejpeg($im, 'palette.jpg');
imagedestroy($im);
以下是使用上述代码生成的palette.jpg
:
请注意,在将图像输出到文件(而不是浏览器)时,无需发送Content-type
标题。
答案 2 :(得分:0)
使用wordwrap函数
尝试此操作<?php
header ('Content-Type: image/jpeg');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
$text = "gfhgfhg
hgf hgf hf hj
hgfhgfhgf hhfh
ffgfhgf hgf hgf
gfhgfhfhfh";
$text=wordwrap($text,25,"<br>\n");
imagestring($im, 1, 5, 5, $text, $text_color);
imagejpeg($im, 'palette.jpg');
imagedestroy($im);
?>