php - 如何实现多行文本干预?

标签 php imagemagick gd imagick intervention

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

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

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

最佳答案

这是解决方案,

$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');
    });
}

关于php - 如何实现多行文本干预?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37234981/

相关文章:

php - jQuery AJAX 在表末尾添加表 tr

image - 如何将 PNG 转换为 1 位 BMP

php - 使用 gd 在图像边界内换行文本

php - mysqli 准备好的语句不转义哪些字符?

php - 如何 mysql join 在我的结果中起作用

php - 如何在一个查询中更新和插入数据

imagemagick - 使用 ImageMagick 命令行在现有图像之上添加图像

macos - 确定 os x 上 bmp 文件的位深度

php - 无法在 ubuntu 上安装 gd lib

php - 如何使用 PHP 将 "true-color"图像转换为 "black-and-white"图像?