Home  >  Article  >  Backend Development  >  How to automatically wrap text and images in imagettfbbox and imagettftext in PHP

How to automatically wrap text and images in imagettfbbox and imagettftext in PHP

不言
不言Original
2018-07-05 13:51:059393browse

This article mainly introduces the method of automatic line wrapping of text and pictures in imagettfbbox and imagettftext in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

mb_internal_encoding("UTF-8"); // 设置编码
   // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
   $content = "";
// 将字符串拆分成一个个单字 保存到数组 letter 中
   for ($i=0;$i<mb_strlen($text3);$i++) {
       $letter[] = mb_substr($text3, $i, 1);
   }
foreach ($letter as $l) {
       $teststr = $content." ".$l;
       $fontBox = imagettfbbox($fontSize, 0, $font, $teststr);
//        $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
       // 判断拼接后的字符串是否超过预设的宽度
       if (($fontBox[2] > $width) && ($content !== "")) {
           $content .= "\n";
       }
       $content .= $l;
   }

//    $fontBox = imagettfbbox($fontSize, 0, $font, $text3);
   imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 400, $fontColor_black, $font, $content );

The above is this article The entire content, I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHPstorm configures PHPunit to unit test the PHP code introduced by composer

The above is the detailed content of How to automatically wrap text and images in imagettfbbox and imagettftext in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn