gd-text PHP 库不在框中包装文本
gd-text PHP library doesn't wrap text within box
我正在使用 gd-text PHP 库在图像上绘制文本
它可以工作,但如果我不提供空格,文本不会环绕在我设置的边界框内,这是我的代码:
$textbox = new Box($img);
$textbox->setFontSize(20);
$textbox->setFontFace('arial.ttf');
$textbox->setFontColor(new Color(255,0,0));
$textbox->setBox(
10, // distance from left edge
10, // distance from top edge
10, // textbox width
10 // textbox height
);
$textbox->setTextAlign('center', 'top');
$textbox->draw("my text my text my text my text my text my text");
文本正确换行,但如果我不插入任何空格,则不会。文本一直在盒子外面。该文档没有解释如何在没有空格的情况下强制文本换行
该库仅在空格处中断,但是它非常简单,我做了一个更改,如果您的输入字符串没有空格,只要您超出框的宽度就会中断。此更改仅在整行没有空格时适用。理想情况下,会有一种模式不会溢出。那将很容易添加。您可能还想添加一个字符以在单词中间出现中断时插入。
将 Box class 中的 wrapTextWithOverflow 方法替换为:
protected function wrapTextWithOverflow($text)
{
$lines = array();
// Split text explicitly into lines by \n, \r\n and \r
$explicitLines = preg_split('/\n|\r\n?/', $text);
foreach ($explicitLines as $line) {
// Check every line if it needs to be wrapped
if((strpos($line, ' ')))
{
$words = explode(" ", $line);
$line = $words[0];
for ($i = 1; $i < count($words); $i++) {
$box = $this->calculateBox($line." ".$words[$i]);
if (($box[4]-$box[6]) >= $this->box['width']) {
$lines[] = $line;
$line = $words[$i];
} else {
$line .= " ".$words[$i];
}
}
}
else
{
//If there are no spaces, append each character and create a new line when an overrun occurs
$string = $line;
$line = $string[0];
for ($i = 1; $i < strlen($string); $i++) {
$box = $this->calculateBox($line.$string[$i]);
if (($box[4]-$box[6]) >= $this->box['width']) {
$lines[] = $line;
$line = $string[$i];
} else {
$line .= $string[$i];
}
}
}
$lines[] = $line;
}
return $lines;
}
我正在使用 gd-text PHP 库在图像上绘制文本
它可以工作,但如果我不提供空格,文本不会环绕在我设置的边界框内,这是我的代码:
$textbox = new Box($img);
$textbox->setFontSize(20);
$textbox->setFontFace('arial.ttf');
$textbox->setFontColor(new Color(255,0,0));
$textbox->setBox(
10, // distance from left edge
10, // distance from top edge
10, // textbox width
10 // textbox height
);
$textbox->setTextAlign('center', 'top');
$textbox->draw("my text my text my text my text my text my text");
文本正确换行,但如果我不插入任何空格,则不会。文本一直在盒子外面。该文档没有解释如何在没有空格的情况下强制文本换行
该库仅在空格处中断,但是它非常简单,我做了一个更改,如果您的输入字符串没有空格,只要您超出框的宽度就会中断。此更改仅在整行没有空格时适用。理想情况下,会有一种模式不会溢出。那将很容易添加。您可能还想添加一个字符以在单词中间出现中断时插入。
将 Box class 中的 wrapTextWithOverflow 方法替换为:
protected function wrapTextWithOverflow($text)
{
$lines = array();
// Split text explicitly into lines by \n, \r\n and \r
$explicitLines = preg_split('/\n|\r\n?/', $text);
foreach ($explicitLines as $line) {
// Check every line if it needs to be wrapped
if((strpos($line, ' ')))
{
$words = explode(" ", $line);
$line = $words[0];
for ($i = 1; $i < count($words); $i++) {
$box = $this->calculateBox($line." ".$words[$i]);
if (($box[4]-$box[6]) >= $this->box['width']) {
$lines[] = $line;
$line = $words[$i];
} else {
$line .= " ".$words[$i];
}
}
}
else
{
//If there are no spaces, append each character and create a new line when an overrun occurs
$string = $line;
$line = $string[0];
for ($i = 1; $i < strlen($string); $i++) {
$box = $this->calculateBox($line.$string[$i]);
if (($box[4]-$box[6]) >= $this->box['width']) {
$lines[] = $line;
$line = $string[$i];
} else {
$line .= $string[$i];
}
}
}
$lines[] = $line;
}
return $lines;
}