今天我想做一个函数,将文本解析成行,由imageTTFBbox()
. 我创建了这段代码,但仅限于 2 行。我想做同样的事情,但是用无限的线条。谢谢你的帮助!:D
function printe($image, $image_width, $string, $font_size, $y, $color, $font){
$font = "fonts/" . $font . ".ttf";
$limit = $image_width - 20;
$tsize = @imageTTFBbox($font_size,0, $font, $string);
$twidth = abs($tsize[4] - $tsize[0]);
$words = explode(" ", $string);
$text = ''; $text1 = '';$a = 0;$o = 0;
for($i = 0; $i < count($words); $i++){
$tsize = @imageTTFBbox($font_size,0, $font, $words[$i]." ");
$twidth = abs($tsize[4] - $tsize[0]);
$nw = $twidth + $a;
if($nw > $limit OR $o > 0){
$o++;
$text1 .= $words[$i]." ";
}else{
$text .= $words[$i]. " ";
$a =$a+$twidth;
}
}
$txtcolor = processColor($color);
$t1size = @imageTTFBbox($font_size,0, $font, $text);
$t1width = abs($t1size[4] - $t1size[0]);
$t2size = @imageTTFBbox($font_size, 0, $font, $text1);
$t2width = abs($t2size[4] - $t2size[0]);
$center = ceil($image_width / 2);
$y1 = $y; $y2 = $y;
$xcord1 = ($image_width/2)-($t1width/2)+3;
$xcord2 = ($image_width/2)-($t2width/2)+3;
$y2 = $y + ($font_size * 1.5);
imagettftext($image, $font_size, 0, $xcord1, $y1, $txtcolor, $font, $text);
imagettftext($image, $font_size, 0, $xcord2, $y2, $txtcolor, $font, $text1);
}