2010-11-17 118 views
0

該腳本需要文本,尋找URL,顏色和製作一個PNG圖像。我遇到的問題是使用wordwrap。由於我不得不分開文本並將它們放在一起以着色鏈接,所以我很難弄清楚如何包裝文本。這是腳本和結果的圖像。PHP:包裝文字爆炸imagettftext

// Matches urls 
function is_a_url($text) { 
    $isurl = preg_match("((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)", $text) || preg_match("/@(\w+)/", $text) || preg_match("/#(\w+)/", $text); 
    return $isurl; 
} 

// Set defaults 
$image_width = 500; 
$image_height = 110; 
$start_x = 5; 
$start_y = 20; // initial x/y coords of text 
$fontsize = 14; 
$font = './font.ttf'; 
$angle = 0; 

$im = imagecreatetruecolor($image_width, $image_height); 

// Create some colors 
$white = imagecolorallocate($im, 255, 255, 255); 
$black = imagecolorallocate($im, 51, 51, 51); 
$linkcolor = imagecolorallocate($im, 51, 210, 208); 
$red = imagecolorallocate($im, 200, 0, 0); 

// Create the box 
imagefilledrectangle($im, 0, 0, $image_width, $image_height, $red); 

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

//split the text on spaces so we can eval each "word" 
$string_chunks = explode(' ', $text); 

// Takes the split and recombine 
foreach ($string_chunks as $chunk) { 
    //wrap based on image size 
    if($start_x > $image_width){ 
     $start_x = 5; 
     $start_y += 20; 
    } 
    else { 
     $image_width = $image_width - 10; 
     $output .= ($counter % 15 < 1) ? '\r' : ''; 
    } 
    // get coordinates of bounding box containing text 
    $coords = imagettfbbox($fontsize, $angle, $font, $chunk); 

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

    // figure out which color to draw in 
    $color_to_draw = is_a_url($chunk) ? $linkcolor : $black; 

    // draw the text chunk 
    imagettftext($im, $fontsize, $angle, $start_x, $start_y, $color_to_draw, $font, $chunk); 


    // adjust starting coordinates to the END of the just-drawn text 
    $start_x += $end_x; 
} 

// Save the image 
header('Content-type: image/png'); 
imagepng($im); 
imagedestroy($im); 

alt text http://dl.dropbox.com/u/19973/script.png

+1

空格分隔的單詞很容易分解成多行。但是你首先需要弄清楚你想如何打破一個長URL。 – stillstanding 2010-11-17 16:34:13

+0

是的,打破網址是主要問題。我會怎麼做呢? – tim 2010-11-17 16:39:24

+0

你對'/'分離很滿意嗎?這並不總是奏效。假設你有'http:// supercalifragilisticexpialidocious.example.com/a-very-long-non-break-space-uri-segment'?這也不符合'/'分隔規則。 – stillstanding 2010-11-17 16:46:27

回答

0

我曾與文本換行類似的問題,所以我寫了一個小功能,可以幫助我,也許它會幫助你。所以這裏的功能是:

function breakTextCentered($str,$num) { 
     $strs = explode(' ',$str); 
     $strlenMax = $num; 

     foreach($strs as $item) { 
      $strlenTemp += strlen(utf8_decode($item)); 
      $tempStr .= $item." "; 
      if($strlenTemp>$strlenMax) { 
       $ret .= $tempStr."\n"; 
       $strlenTemp =0; 
       $tempStr = ''; 
      } 
     } 
     if($strlenTemp<=$strlenMax) { 
      $ret .= $tempStr; 
     } 
     return $ret; 
    } 

只是傳遞你想要在一行上顯示的文字和字符數。它只會在空格上包裝文字。

+0

問題是我需要包裝現在有空格的網址。 – tim 2010-11-17 17:06:42