我有文本框的宽度、高度和起点
$text=$content[$i];
$length=strlen($text);
$top=$y[$i]; //top position of string
$fontsize=$height; //height of the text box
if($width<$fontsize*$length/1.34) //using Roboto Font its character height is somewhere around 1.34 to 1.43 of its width
{
$fontsize=ceil(sqrt($width*$height/$length*1.4));
}
if($fontsize<20)
{
$fontsize=20; //min font size
}
if($fontsize>75)
{
$fontsize=75; //max font size
}
$char_per_line=floor($length/$height*$fontsize*1.4);
$string = wordwrap($text,$char_per_line,"|");
//create array of lines
$strings = explode("|",$string);
foreach($strings as $string){
$img->text($string, $x[$i], $top, function($font)use($fontsize) {
$font->file('assets/fonts/Roboto-Medium.ttf');
$font->size($fontsize);
$font->color(‘#000000’);
$font->align(‘left);
$font->valign('top');
});
$top=ceil($top+($fontsize*1.02)); //shift top postition down
}
}
使用这段代码,我得到了令人满意的结果,但它并不完美。我怎样才能得到完美的结果?