加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 如何在多行(段落中)中编写文本?

发布时间:2020-12-13 17:43:45 所属栏目:PHP教程 来源:网络整理
导读:当我使用这段代码时,我可以用文字制作图像,但是在一行中, function writetext($image_path,$imgdestpath,$x,$y,$angle,$text,$font,$fontsize) { $image=imagecreatefromjpeg("$image_path"); $height = imageSY($image); $width = imageSX($image); $color
当我使用这段代码时,我可以用文字制作图像,但是在一行中,

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);
      $textwidth = $width;
      imageTTFtext($image,$fontsize,$color,$text );
      ImageJPEG($image,$imgdestpath);
}

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

解决方法

对于每一行,您需要使用$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,50,$font_color,$line);

// Increment Y so the next line is below the previous line
$y += 23;
}

source

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读