2010-08-31 44 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