我想用 php 将包装的文本写入图像。这是一个演示。如果您复制、粘贴下面的文本并将其发送到演示,您会注意到有些行被扭曲,并且有些段落空格不能存在。我正在寻找代码几个小时。我不明白问题出在哪里。有人帮我吗?
示例文本:
Lorem ipsum dolor sit amet, consectetur adipiscing elit。Sed ut justo consectetur, cursus lorem vitae, accumsan lorem。Cras eu odio vulputate、rhoncus dui vel、aliquam lorem。Fusce scelerisque facilisis lacus, quis malesuada justo placerat nec。Curabitur elementum mattis nisl、sed sodales mauris congue 等。Nunc velit mauris, accumsan a dictum vitae, pellentesque luctus leo。venenatis orci 中的 Maecenas。Suspendisse sed neque magna。Praesent vitae sapien porttitor diam aliquam eleifend。Maecenas lacinia elit non velit vehicula rutrum。Sed ultricies mauris vitae dapibus tempor。Praesent eros metus,euismod ut est eget,rhoncus iaculis lorem。Ut aliquet dictum ligula lobortis cursus。Sed mattis ante ut odio tincidunt venenatis。Donec euismod quam sit amet velit tincidunt, ut convallis ligula tempor。Quisque tincidunt elit sem,
$mx = imagesx($im); // Width of the created image
$my = imagesy($im); // Height of the image
//TEXT VARS/////////
$main_text = $text; // $text variable comes directly from the form
$main_text_size = 20; // $text font size
$words = explode(' ', $main_text);
$lines = array($words[0]);
$currentLine = 0;
for($i = 1; $i < count($words); $i++)
{
$lineSize = imagettfbbox($main_text_size, 0, $font, $lines[$currentLine] . ' ' . $words[$i]);
if($lineSize[2] - $lineSize[0] < ($mx-20))
{
$lines[$currentLine] .= ' ' . $words[$i];
}
else
{
$currentLine++;
$lines[$currentLine] = $words[$i];
}
}
$line_count = 1;
// Loop through the lines and place them on the image
foreach ($lines as $line)
{
$line_box = imagettfbbox($main_text_size, 0, $font, "$line");
$line_height = $line_box[1]-$line_box[7];
$line_y = (($line_height+4) * $line_count);
imagettftext($im, $main_text_size, 0, 5, $line_y, $black, $font, $line);
// Increment Y so the next line is below the previous line
$line_count ++;
}
imagepng($im, 'image.png');
imagedestroy($im);