文字超出最大宽度分行
作者:互联网
/**
* @param $fontsize 字体大小
* @param $ttfpath 字体文件
* @param $str 字符串
* @param $width 最大宽度
* @param int $fontangle 将被度量的角度大小
* @param string $charset 编码
* @return string
*/
public static function autoWrap2($fontsize, $ttfpath, $str, $width, $fontangle = 0, $charset = 'utf-8')
{
$len = mb_strlen($str);
$arr = array();
$tmpArr = array();
$tmpStr = '';
for ($i = 1; $i <= $len; $i++) {
$tmpStr = mb_substr($str, 0, $i);
$size = imagettfbbox($fontsize, $fontangle, public_path() . '/static/fonts/msyh.ttf', $tmpStr);
$w = $size[2];
if ($w < $width) {
if ($i < $len) {
continue;
} else {
return $tmpStr;
}
} else {
$arr[] = $tmpStr;
$str2 = mb_substr($str, $i);
$tmpStr .= PHP_EOL . self::autoWrap2($fontsize, $ttfpath, $str2, $width);
}
break;
}
return trim($tmpStr, PHP_EOL);
}
标签:fontangle,charset,param,宽度,ttfpath,str,超出,array,分行 来源: https://blog.csdn.net/weixin_37367492/article/details/90608436