2011-01-09 37 views
6

我一直在使用這個簡單的腳本來從文字圖像的圖像格式的文本:使用PHP GD創建具有不同的字體

<?php 
header('Content-type: image/png'); 

$color = RgbfromHex($_GET['color']); 

$text = urldecode($_GET['text']); 

$font = 'arial.ttf'; 

$im = imagecreatetruecolor(400, 30); 

$bg_color = imagecolorallocate($im, 255, 255, 255); 

$font_color = imagecolorallocate($im, $color[0], $color[1], $color[2]); 

imagefilledrectangle($im, 0, 0, 399, 29, $bg_color); 

imagettftext($im, 20, 0, 10, 20, $font_color, $font, $text); 

imagepng($im); 

imagedestroy($im); 

function RgbfromHex($hexValue) { 
    if(strlen(trim($hexValue))==6) { 
     return array(
        hexdec(substr($hexValue,0,2)), // R 
        hexdec(substr($hexValue,2,2)), // G 
        hexdec(substr($hexValue,4,2)) // B 
        ); 
    } 
    else return array(0, 0, 0); 
} 

?> 

我打電話與file.php文本=測試腳本&顏色腳本= 000000

現在我想知道我怎麼能生成相同的圖像在混合正常和加粗字體的文本,像file.php?text=testing <b>script</b>&color=000000


謝謝到dqhendricks幫我解決這個問題。

下面是一個簡單的腳本我寫的,還需要改進很多,但對於基本功能,這似乎是做工精細:

<?php 
header('Content-type: image/png'); 

$color = RgbfromHex($_GET['color']); 

$im = imagecreatetruecolor(400, 30); 

$white = imagecolorallocate($im, 255, 255, 255); 

imagefilledrectangle($im, 0, 0, 399, 29, $white); 

$tmp = $_GET['text']; 

$words = explode(" ", $tmp); 

$x = array(0,0,10); // DUMMY ARRAY WITH X POSITION FOR FIRST WORD 

$addSpace = 0; 

foreach($words as $word) 
{ 
    if($addSpace) $word = " ".$word; // IF WORD IS NOT THE FIRST ONE, THEN ADD SPACE 

    if(stristr($word, "<b>")) 
    { 
     $font = 'arialbd.ttf'; // BOLD FONT 
     $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, str_replace(array("<b>","</b>"), "", $word)); 
    } 
    else 
    { 
     $font = 'arial.ttf'; // NORMAL FONT 
     $x = imagettftext($im, 20, 0, $x[2], 20, imagecolorallocate($im, $color[0], $color[1], $color[2]), $font, $word); 
    } 

    $addSpace = 1; 
} 

imagepng($im); 

imagedestroy($im); 

function RgbfromHex($hexValue) { 
    if(strlen(trim($hexValue))==6) { 
     return array(
        hexdec(substr($hexValue,0,2)), // R 
        hexdec(substr($hexValue,2,2)), // G 
        hexdec(substr($hexValue,4,2)) // B 
        ); 
    } 
    else return array(0, 0, 0); 
} 

?> 

注:這隻會爲「加粗」用空格分隔單個單詞工作而不是拼寫單詞的一部分。

呼叫與file.php?text=testing+<b>script</b>&color=000000

+0

嘗試http://www.roseindia.net/tutorial/php/phpgd/About-boldtext.html它似乎你想要做什麼,它的黑色字體,是在灰色字體的頂部,你可以改變灰色的位置,讓它們被放置在你喜歡的位置。該URL將類似於`file.php?text = testing&bold = script&color = 000000`。 – Daniel 2011-01-09 01:09:01

+0

這看起來不像大膽,但更像是一個影子,如果我把它放在彼此的前面,我不認爲它們會出現任何不同,因爲只有一個imagettftext()。 – Meredith 2011-01-09 02:45:21

回答

0

的腳本,你將需要加載的宋體,加粗字體文件,並做兩個獨立的imagettftext()調用,一個與您要使用的每個字體。至於解析字符串以找出你想要粗體的部分,以及你應該在哪裏打印文本的每個部分,這聽起來像它將變成非常複雜的代碼。你甚至用這個做什麼?完成同樣的事情可能會有更好的解決方案。

加成

從imagettftext()函數使用返回值來確定下一個文本打印應該開始。

從文檔:

返回與代表四個點製作的文本的邊界框8個元素的數組。點的順序是左下,右下,右上,左上。無論角度如何,這些點都是相對於文本的,所以「左上角」表示在左上角水平看到文本時。錯誤時返回FALSE。對於

-1

響應:

解析將不會是一個問題,我沒有明確的是如何將我找到了在該示例文本=測試腳本第二wordr X位置?我的意思是我怎麼知道第一個單詞的結束位置,因此我可以將第二個單詞與另一個imagettftext()和一個粗體字體放在一起。 - 梅雷迪思1月9日在'11 2:43


有趣的人怎麼花時間說「看編輯爲這個問題的答案」的時候本應該只是說在空間爆炸字符串他們,那麼每個字是在一個陣列..

<?PHP 
$sentence = "I LOVE giving retarded answers that don't amount to jack!"; 
$sentence = explode(" ",$sentence); 

for($s=0;$s<count($sentence);$s++){ 

#THERES YOUR INDIVIDUAL WORDS! 
$word = $sentence[$s]; 

} 
?> 

你的X位置將只是你的邏輯$ s。要獲得第二個單詞,你可以這樣做:

<?PHP 
$word1 = $sentence[0]; 
$word2 = $sentence[1]; 
$word3 = $sentence[2]; 
$word4 = $sentence[3]; 
?> 

是的,我將$字命名爲一種精神視覺效果。

$句子[1]是字2