PHP:包装的测试行长度

时间:2010-08-31 14:21:54

标签: php width textwrapping

如何测试块是否超出图像大小并将该文本包装到下一行。不确定我是否正在使用if语句正确执行此操作。

$text="Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem http://somelongurl.com/then-we-make-it-super-long-with-some-more/ Lorem Ipsum Lorem Ipsum Lorem Ipsum";

$string_chunks = explode(' ', $text);

foreach ($string_chunks as $chunk) {

    if($end_x + $chunk > $image_width){
        $start_x = 5;
        $start_y += 20;
    }

   $coords = imagettfbbox($fontsize, $angle, $font, $chunk);

   $end_x = $coords[0] + $coords[4] + 10;

   $color_to_draw = is_a_url($chunk) ? $linkcolor : $black; 

   imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk);

   $start_x += $end_x;
}

使用此代码我得到:

Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem 
http://somelongurl.com/then-we-make-it-super-long-with-some-more/
Lorem Ipsum Lorem Ipsum Lorem Ipsum

我想要发生的事情是:

Lorem Ipsum Lorem Ipsum Lorem Ipsum 
Lorem http://somelongurl.com/then-we
-make-it-super-long-with-some-more/
Lorem Ipsum Lorem Ipsum Lorem Ipsum

4 个答案:

答案 0 :(得分:2)

使用wordwrap并将第四个参数$cut作为true传递给强制网址。

Live example

答案 1 :(得分:1)

我想我知道你想要实现的目标。我没有测试下面的代码,所以它可能需要一些抛光和重新/测试。但它应该给你一个很好的起点

<?php
$text = "Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem http://somelongurl.com/then-we-make-it-super-long-with-some-more/ Lorem Ipsum Lorem Ipsum Lorem Ipsum";
$string_chunks = explode(' ', $text);

foreach ($string_chunks as $chunk) {
    $_start_bit = false;
    // before anything else check if chunk is url
    $color_to_draw = is_a_url($chunk) ? $linkcolor : $black; 
    // check if chunk is to long
    if(strlen($chunk) > $image_width) {
        // if there is allredy a word in the current line
        // make the first bit $imagewidth - current line width
        if ($start_x > 5) {
            $_start_bit = substr($chunk, 0, ($image_width - $start_x));
            $chunk = str_replace($_start_bit, "", $chunk);
        }
        $_chunkbits = wordwrap($chunk, $image_width, "\n", true);
        $_chunkbits = explode("\n", $_chunkbits);
        if($_start_bit) {
            array_unshift($_chunkbits, $_start_bit);
        }
        // loop bits and draw them
        foreach ($_chunkbits as $bit) {
            if($end_x + $bit > $image_width){
                $start_x = 5;
                $start_y += 20;
            }
            $coords = imagettfbbox($fontsize, $angle, $font, $bit);
            $end_x = $coords[0] + $coords[4] + 10;
            imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $bit);
            $start_x += $end_x;
        }
        unset($_chunkbits);
    } else {
        if($end_x + $chunk > $image_width){
            $start_x = 5;
            $start_y += 20;
        }
        $coords = imagettfbbox($fontsize, $angle, $font, $chunk);
        $end_x = $coords[0] + $coords[4] + 10;
        imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk);
        $start_x += $end_x;
    }
}

答案 2 :(得分:0)

if($end_x + $chunk > $image_width){ 
    $start_x = 5; 
    $start_y += 20; 
} 

一个主要错误,$ end_x +(strlen($ chunk)* $ charPXsize)&gt; $ IMAGE_WIDTH 另一个错误,如果一行超过$ image_width会发生什么?它会在下一行打印出来,但无论如何它都会太长。

答案 3 :(得分:0)

我所做的是迭代字符串,每次检查边界框是否过宽。如果是,请插入一个新行并继续,直到您消耗完所有文本为止。然后把它全部写成一个大字符串......

$chunks = explode(' ', $text);
$wrappedText = '';
foreach ($chunks as $chunk) {
    $coords = imagettfbbox($fontsize, $angle, $font, $wrappedText.' '.$chunk);
    $width = $coords[2] - $coords[0];
    if ($width > $myMaxWidth) {
        $wrappedText .= "\n" . $chunk;
    } else {
        $wrappedText .= ' ' . $chunk;
    }
}
imagettftext(
    $im, 
    $fontsize, 
    $angle, 
    $start_x, 
    $start_y, 
    $color_to_draw, 
    $font, 
    $wrappedText
);

现在,请注意,这不会对您的链接进行不同的着色...但是,您可以随时在其中添加检测方法以确定链接将被写入的确切位置,并用它的颜色覆盖它(如果它在那里)。 ..