2009-11-02 17 views
0

基本上我正在尋找一種在GIF上寫動態文本的方法[最好通過PHP GD。]但我似乎無法讓這兩個函數發揮出色。可能使用PHP GD的imagecreatefromgif()和imagettftext()在一起?

參考:imagecreatefromgif & imagettftext

function load_gif($imgname) 
{ 
    $im = @imagecreatefromgif($imgname); 

    if(!$im) 
    { 
     $im = imagecreatetruecolor(150,30); 
     $bgc = imagecolorallocate($im,255,255,255); 
     $tc = imagecolorallocate($im,0,0,0); 

     imagefilledrectangle($im,0,0,150,30,$bgc); 

     imagestring($im,1,5,5,'Error loading ' . $imgname,$tc); 
    } 

    return $im; 
} 

if ($_GET['color'] == 'red') 
{ 
    header('Content-Type:image/gif'); 

    //$img = imagecreatetruecolor(51,32); // THIS IS NEEDED FOR TEXT TO SHOW 
    $img = load_gif('map-bubble-' . $_GET['color'] . '.gif'); 

    $black = imagecolorallocate($img,0,0,0); 
    $white = imagecolorallocate($img,255,255,255); 

    imagefilledrectangle($img,0,0,51,32,$black); 
    imagettftext($img,14,0,0,0,$white,'../arial.ttf','test'); 

    imagegif($img); 

    imagedestroy($img); 
} 
+1

讓我們看看你的代碼。告訴我們您收到的錯誤消息。 –

+1

你是什麼意思,「我似乎無法讓這兩個功能玩得好」? – Residuum

+0

如果你看看我剛剛添加的代碼示例,當我使用imagecreatetruecolor()定義$ img時,我可以使用文本,但它是純色的背景色,但是如果不使用文本,則使用imagecreatefromgif()。這意味着它是一個或另一個,因此不玩很好 –

回答

2

如果你創建一個新的truecolour圖像,導入的GIF成,爲GIF再次添加文本,然後將結果輸出 - 你可能有更多的運氣那樣。

+0

你可以更深入的步驟1和2,特別是如何將GIF導入真彩圖像。 –

+0

我想我應該指出:我認爲這是我需要做的,問題是如何 –

0

只需將imagecreatefromgif存儲到變量中,運行imagettftext,然後使用內容類型標頭輸出圖像。看看參數,並使用imagecreatefromgif中的outptu填充$ image參數。

+0

是不是我在我的代碼示例中做什麼? –

+0

我在發佈代碼之前編寫了答案。抱歉。 – CodeJoust

0

這裏是我落得這樣做:

if ($_GET['color'] == 'red' && strlen($_GET['number']) > 0 && is_numeric($_GET['number'])) 
{ 
    $image = imagecreatefromgif('map-bubble-' . $_GET['color'] . '.gif'); 

    $image_width = imagesx($image); 

    $text_size = @imagettfbbox(10,0,'../arial.ttf','$' . $_GET['number']); 
    $text_width = abs($text_size[2]-$text_size[0]); 

    imagettftext($image,10,0,($image_width/2) - ($text_width/2),17,imagecolorallocate($image,255,255,255),'../arial.ttf','$' . $_GET['number']); 

    header('Content-type:image/gif'); 
    imagegif($image); 

    imagedestroy($image); 
}