• 欢迎访问小杰博客网站
  • 欢迎访问小杰博客网站哦

PHP GD库生成图片自动换行函数,自动分页函数

程序 小杰 5年前 (2018-01-16) 1581次浏览 已收录 0个评论

/* 文字自动换行
* @param $card 画板
* @param $pos 数组,top距离画板顶端的距离,fontsize文字的大小,width宽度,left距离左边的距离,hang_size行高
* @param $str 要写的字符串
* @param $iswrite  是否输出,ture,  花出文字,false只计算占用的高度
* @param $nowHeight  已写入行数;
* @param $second  数组 left  记录换行后据x坐标 ,width 记录换行后最大宽; , maxline 记录最大允许最大行数
* @return 数组 tp:本次写入行数  nowHeight:一共写入行数  residueStr:截取未写完的字符串 height:最后一行据顶部的高度
*/
function textalign($card, $pos, $str, $iswrite,$fontpath,$nowHeight,$second){
$_str_h = $pos[“top”];//文字在整个图片距离顶端的位置,也就是y轴的像素距离
$fontsize = $pos[“fontsize”];//文字的大小
$width = $pos[“width”];//设置文字换行的宽带,也就是多宽的距离,自动换行
$margin_lift = $pos[“left”];//文字在整个图片距离左边的位置,也就是X轴的像素距离
$hang_size = $pos[“hang_size”];// 这个是行高
$temp_string = “”;
$secondCk = “”; //换号的标示,已换行true ,未换行false;
$font_file =$fontpath;//字体文件,在我的同级目录的Fonts文件夹下面
$tp = 0;
$font_color = imagecolorallocate($card, $pos[“color”][0], $pos[“color”][1], $pos[“color”][2]);
for ($i = 0; $i < mb_strlen($str,’utf8′); $i++) {
$box = imagettfbbox($fontsize, 0, $font_file, $temp_string);
$_string_length = $box[2] – $box[0];
$temptext = mb_substr($str, $i, 1,’utf-8′);//拆分字符串
$temp = imagettfbbox($fontsize, 0, $font_file, $temptext);//用来测量每个字的大小
if($secondCk){//如果换行,进入判断赋值
if(is_array($second)){//如果传入换行后参数,则使用.
$width = $second[‘width’];
$margin_lift = $second[‘left’];
}
}
if($second[‘maxline’]){
//如果已经写入最大行数
if($nowHeight == $second[‘maxline’]){
//获取原字符串长度
$strlong = mb_strlen($str,’utf8′);
//抓取剩余字符串
$residueStr =”;
$residueStr .= mb_substr($str, $i, $strlong – $i,’utf-8′);
$cc = $strlong – $i;
break;
}
}
if ($_string_length + $temp[2] – $temp[0] < $width) {
$temp_string .= mb_substr($str, $i, 1,’utf-8′);
if ($i == mb_strlen($str,’utf8′) – 1) {
$_str_h += $hang_size;
$tp++;//用来记录有多少行
$nowHeight++;//记录一共写入多少行
if ($iswrite) {//如果传入的参数是false,只做测量,不进行绘制输出
imagettftext($card, $fontsize, 0, $margin_lift, $_str_h, $font_color, $font_file, $temp_string);
}
}
} else {
$texts = mb_substr($str, $i, 1,’utf-8′);
$isfuhao = preg_match(“/[\\pP]/u”, $texts) ? true : false;//用来判断最后一个字是不是符合,
if ($isfuhao) {//如果是符号,我们就不换行,把符合添加到最后一个位置去
$temp_string .= $texts;
} else {
$i–;
}
$_str_h += $hang_size;
$tp++;
$nowHeight++;//记录一共写入多少行
if($iswrite){
imagettftext($card, $fontsize, 0, $margin_lift, $_str_h, $font_color, $font_file, $temp_string);
}
$temp_string = “”;
$secondCk = true;//作为是否已换行的标志

}

}

$strdata[‘tp’] = $tp ;
$strdata[‘residueStr’] = $residueStr ;
$strdata[‘nowHeight’] = $nowHeight ;
$strdata[‘height’] = $_str_h;
return $strdata;
}


小杰博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP GD库生成图片自动换行函数,自动分页函数
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址