php imagettfbbox和imagettftext 文字图片自动换行的方法
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 );