1

我有文本框的宽度、高度和起点

$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
                }
            }

使用这段代码,我得到了令人满意的结果,但它并不完美。我怎样才能得到完美的结果?

4

1 回答 1

0

这是解决方案,

$lines = explode("\n", wordwrap($description, 120)); // break line after 120 charecters

for ($i = 0; $i < count($lines); $i++) {
    $offset = 820 + ($i * 50); // 50 is line height
    $cert->text($lines[$i], 110, $offset, function ($font) {
        $font->file('fonts/Roboto_Regular.ttf');
        $font->size(30);
        $font->color('#000000');
    });
}
于 2020-12-06T01:07:31.693 回答