2010-08-31 52 views
4

如何测试块是否超过图像大小并将该文本包装到下一行。不知道我是否甚至用我的if语句正确地做到这一点。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) { 

    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 

回答

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; 
    } 
} 
0
if($end_x + $chunk > $image_width){ 
    $start_x = 5; 
    $start_y += 20; 
} 

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

+0

我改变了我的问题,以反映我想要的图像包装像。 – tim 2010-08-31 14:37:45

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 
); 

现在,请注意,这不会影响你对链接不同......但是,你总是可以在里面添加的检测方法,以确定确切位置的链接将被写入,并与你的颜色覆盖它,如果它的存在......

+0

这会产生阶梯式输出,而不是包装文字。 – tim 2010-08-31 14:51:26

+0

它生成包装文本。它只是不预先计算宽度(因为你不能,由于TrueType字体不是固定宽度)... – ircmaxell 2010-08-31 15:04:29

+0

啊,我明白你的意思了。如果你有一个“单词”(你的URL)比这个盒子长,它会把每个新单词添加到它自己的新行中。您需要在宽度检查之后添加另一个检查,以确定它本身是否过长(并且在那一点将其分开) – ircmaxell 2010-08-31 15:11:20

2

使用wordwrap,并通过它的第四个参数,$cut,作为true迫使URL被打破。

Live example

+0

这将是最简单的,但我已经尝试过,似乎无法使其工作。 – tim 2010-08-31 14:48:53

+0

@timg什么问题?检查我添加的链接。 – Artefacto 2010-08-31 15:19:33

+0

尝试使用imagettftext与爆炸文本做到这一点,它不起作用。 – tim 2010-08-31 15:31:08