ImageString的两个问题:粗体字和换行PHP


Two questions with ImageString: Bold words and Newlines PHP

我正在尝试在图像中写一个文本,我有这个:

header("Content-type: image/png");
$font = 4;
$width = ImageFontWidth($font) * strlen($string);
$height = ImageFontHeight($font);
$im = @imagecreate($width,$height);
$background_color = imagecolorallocatealpha($im, 255, 255, 255, 127);
$text_color = imagecolorallocate($im, 0, 0,0); 
imagestring($im, $font, 0, 0, utf8_decode($string), $text_color);
imagepng($im);
imagedestroy($im);

字符串:

$string = "Hello, My name is Ikillnukes and I'm trying to do a newline with PHP.";

好吧,我想用输出做这个:

正常输出:你好,我的名字是Ikillnukes,我正在尝试用PHP写一行换行符。

我想要的:

你好,我的名字是Ikilnukes和

我正在尝试用PHP做一个换行符。

我该怎么做?

还有一件事。。。我想在字符串中添加一些粗体文本,我该怎么做?

首先,imagestring不执行自动换行。你必须自己做。

这意味着你需要知道一个特定字符串的像素长度。这是你的朋友imagettfbox

imagettfbox的文档中,有一个很好的、全面的示例,说明如何自动换行以匹配图像宽度:PHP文档中的a2hansolo示例。

对于粗体书写的问题,请查看有关imagettftext的文档,尤其是mirza_aqueel_qau的示例。