php生成图片表格自动换行_PHP GD库生成图片自动换行函数,自动分页函数

/* 文字自动换行

* @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;

}

做一个心中有料的人.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值