
php使用GD库绘制长文本,需要对文本进行换行处理
PHP
/**
 * @param $fontsize 字体大小
 * @param $angle 角度
 * @param $fontface 字体名称
 * @param $string 字符串
 * @param $width 预设宽度
 * 用于拆分大段文字换行
 * @return string
 */
protected function autowrap($fontsize, $angle, $fontface, $string, $width) {
    $content = "";
    preg_match_all("/./u", $string, $arr);
    $letter = $arr[0];
    foreach($letter as $l) {
        $teststr = $content.$l;
        $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
        if (($testbox[2] > $width) && ($content !== "")) {
            $content .= PHP_EOL;
        }
        $content .= $l;
    }
    return $content;
}处理好之后使用 imagettftext() 函数将文本段绘制在画布上
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。