如何使字符串长度受字符串长度限制(PHP)

时间:2013-07-07 10:40:02

标签: image gd

我试图用GD签名图片。

我有一行文字,看起来像这样:“这是一长篇文字”

如果我使用第60个字体,它不适合我的图像,所以我需要一些wordwrap

现在我正在使用循环,这个循环正在按照符号数量进行自动换行,并逐行添加文本到图像中。

$words = explode(" ", $text);
if (strlen($words[0]) > 20)
    $output = substr($words[0], 0, 20);
else {
    $output = array_shift($words);
    while (strlen($output . " " . $words[0]) <= 20) {
        $output .= " " . array_shift($words);
    }
}
$text = $str2 = substr($text, strlen($output));

list($left,, $right) = imageftbbox($font_sizet, 0, $font_path, $output);
$width = $right - $left;
$leftx = (740 - $width) / 2;
$topy = (560 + $i * 30);
imagettftext($img, $font_sizet, 0, $leftx, $topy, $white, $font_path, $output);

这个脚本的问题是,如果第一个字母将是“WWWWWWWWWWW”,那么图像将只适合10个符号......如果第一个字母将是“llllllllllll”,则文本看起来很短。 / p>

使用此行我无法确定的文字宽度:

list($left,, $right) = imageftbbox( $font_sizet, 0, $font_path, $output);
$width = $right - $left;

&lt; - 我认为不知何故,我可以在我的身上使用这个$ width     循环与maxlength进行比较

如何通过符号数量改变限制以限制像素数(比方说,我希望线条不超过300px)?

0 个答案:

没有答案