如何在多行(段落中)中写入文本?(How to write text in multiline(in a paragraph)?)

编程入门 行业动态 更新时间:2024-10-24 14:28:06
如何在多行(段落中)中写入文本?(How to write text in multiline(in a paragraph)?)

当我使用这段代码时,我可以用文字制作图像,但是在一行中,

function writetext($image_path,$imgdestpath,$x,$y,$angle,$text,$font,$fontsize) { $image=imagecreatefromjpeg("$image_path"); $height = imageSY($image); $width = imageSX($image); $color = imagecolorallocate($image,0,0,0); $textwidth = $width; imageTTFtext($image,$fontsize,$angle,$x,$y,$color,$font, $text ); ImageJPEG($image,$imgdestpath); }

请告诉我如何在多行段落中制作此图像?

When i use this code,I can make a image out of text,but in a single line,

function writetext($image_path,$imgdestpath,$x,$y,$angle,$text,$font,$fontsize) { $image=imagecreatefromjpeg("$image_path"); $height = imageSY($image); $width = imageSX($image); $color = imagecolorallocate($image,0,0,0); $textwidth = $width; imageTTFtext($image,$fontsize,$angle,$x,$y,$color,$font, $text ); ImageJPEG($image,$imgdestpath); }

Please tell how to make this image in a multiline paragraph??

最满意答案

对于每一行,您需要使用$ y的新值调用新的imageTTFtext函数,例如:

$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer non nunc lectus. Curabitur hendrerit bibendum enim dignissim tempus. Suspendisse non ipsum auctor metus consectetur eleifend. Fusce cursus ullamcorper sem nec ultricies. Aliquam erat volutpat. Vivamus massa justo, pharetra et sodales quis, rhoncus in ligula. Integer dolor velit, ultrices in iaculis nec, viverra ut nunc.'; // Break it up into pieces 125 characters long $lines = explode('|', wordwrap($text, 115, '|')); // Starting Y position $y = 513; // Loop through the lines and place them on the image foreach ($lines as $line) { imagettftext($image, $font_size, 0, 50, $y, $font_color, $font, $line); // Increment Y so the next line is below the previous line $y += 23; }

资源

For each line you need new imageTTFtext function to be called with new value for $y for example:

$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer non nunc lectus. Curabitur hendrerit bibendum enim dignissim tempus. Suspendisse non ipsum auctor metus consectetur eleifend. Fusce cursus ullamcorper sem nec ultricies. Aliquam erat volutpat. Vivamus massa justo, pharetra et sodales quis, rhoncus in ligula. Integer dolor velit, ultrices in iaculis nec, viverra ut nunc.'; // Break it up into pieces 125 characters long $lines = explode('|', wordwrap($text, 115, '|')); // Starting Y position $y = 513; // Loop through the lines and place them on the image foreach ($lines as $line) { imagettftext($image, $font_size, 0, 50, $y, $font_color, $font, $line); // Increment Y so the next line is below the previous line $y += 23; }

source

更多推荐

本文发布于:2023-08-06 21:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455000.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:段落   文本   如何在   write   multiline

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!