php文本转图片自动换行的方法
˂?phpheader("Content-type:image/png");mb_internal_encoding("UTF-8");//设置编码functionautowrap($
<?php
header ("Content-type: image/png");
mb_internal_encoding("UTF-8"); // 设置编码
function autowrap($fontsize, $angle, $fontface, $string, $width) {
// 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
$content = "";
// 将字符串拆分成一个个单字 保存到数组 letter 中
for ($i=0;$i<mb_strlen($string);$i++) {
$letter[] = mb_substr($string, $i, 1);
}
foreach ($letter as $l) {
$teststr = $content." ".$l;
$testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
// 判断拼接后的字符串是否超过预设的宽度
if (($testbox[2] > $width) && ($content !== "")) {
$content .= "\n";
}
$content .= $l;
}
return $content;
}
$bg = imagecreatetruecolor(300, 290); // 创建画布
$white = imagecolorallocate($bg, 255, 255, 255); // 创建白色
$text = "内容内容内容。";
$text = autowrap(12, 0, "simsun.ttc", $text, 280); // 自动换行处理,simsun.ttc为字体文件路径
// 若文件编码为 GB2312 请将下行的注释去掉
// $text = iconv("GB2312", "UTF-8", $text);
imagettftext($bg, 12, 0, 10, 30, $white, "simsun.ttc", $text);
imagepng($bg);
imagedestroy($bg);
?>
相关文章
- okhttp、okhttp3使用post方式发送form-data数据
- JAVA字符串逗号分隔并对每个字符串添加引号
- [Docker系列] Install BaoTa with Docker
- [信创系列]银河麒麟安装nodejs18和npm2,并启动对应工程
- EasyExcel导出Excel并合并单元格
- Python3安装pip及pip安装whl包
- [Ubuntu系列]Ubuntu 安装 Harbor
- [Ubuntu系列]Ubuntu 安装 docker 及修改 docker 存储位置
- [Centos系列]CentOS 修改 DNS
- [Centos系列]source /etc/profile 无法永久生效问题
发表评论
评论列表
- 这篇文章还没有收到评论,赶紧来抢沙发吧~