2012-07-10 19 views
1

簡單的問題,我在那裏搜索,沒有找到一種方法來做到這一點。Make imagettftext recognice HEX顏色

我有一個字符串(可變一個),它可以是這樣的:

#FF9900Hel#FFFFFFlo 

我需要一種方法來使recognice這些顏色的imagettftext功能和使用繪製這些顏色的文本。例如,在我之前提到的例子中,文本應該是:紅色:Hel和白色:Lo。我希望我解釋清楚我的意思。

在此先感謝

+1

有絕對沒有在'imagettftext'這將使它做到這一點。你必須自己做這件事,把它作爲兩個不同顏色的兩個單獨的文本部分放入圖像中,並將它們與一些數學對齊。 – deceze 2012-07-10 18:57:00

+0

嗯,我明白這一點。我在這個新的。有沒有什麼函數可以將這些HEX顏色轉換爲我可以用於imagettftext的實際變量,或者我應該爲此創建自己的函數? – Karevan 2012-07-10 18:58:33

+0

如果你明白這一點,那麼你的問題的措辭就會大不相同。最好描述你在實施上述過程中遇到的具體問題。 – deceze 2012-07-10 19:00:16

回答

3

您不僅需要識別顏色,還需要對齊的字符串。這不是自動的,因爲它在HTML中。

我會先分割字符串做爲其組分部分:

// Split the text using hex colors as strings: 
$hex  = '[0-9a-fA-F]'; // Capital hex should be supported 

$colorex = "(#{$hex}{$hex}{$hex}{$hex}{$hex}{$hex})"; 
// or also $colorex = "(#{$hex}{6})"; 

$parts = preg_split ("/{$colorex}/", $text, -1, PREG_SPLIT_DELIM_CAPTURE); 

// Then you would iterate through the parts: 

$color = imageColorAllocate($gd, 0, 0, 0); // Default is black 
foreach($parts as $part) 
{ 
    // Scan the hex value 
    if (preg_match("/^{$colorex}\$/", $part, $gregs)) 
    { 
     sscanf($gregs[1], "#%02x%02x%02x", &$r, &$g, &$b); 
     $color = imageColorAllocate($gd, $r, $g, $b); 
     continue; 
    } 
    // IMPROVEMENT: if count(explode("\n", $part)) > 1, $y += $height, $x = 0 
    // to indicate "go to next line". So this would appear on two lines: 
    //  #Hel#ff7700lo, 
    //  #443212World 
    // Next section will be repeated for each item 
    // in the exploded string. 
    //! $subparts = explode("\n", $part); 
    //! foreach($subparts as $part) 
    //! { // We have overwritten $part 

    // Here $part is printed as string at position $x, $y 
    // Ask GD to calculate string width 
    // with http://php.net/manual/en/function.imagettfbbox.php 
    // Calculations with $angle != 0 is a bit more difficult, entails trigonometric 
    // evaluation of $w and $h. 
    $bbox = imagettfbbox ($size, $angle, $fontfile, $part); 
    $w = $bbox[4] - $bbox[0]; // 0,1 is lower left corner 
    $h = $bbox[5] - $bbox[1]; // 4,5 is upper right 
    imagettftext ($gd, $size, $angle, $x, $y, $color, $fontfile, $part); 
    // Increment $x position by $w 
    $x += $w; 

    //!  if (count($subparts) > 1) 
    //!  { 
    //!   $x = 0; $y += $h; 
    //!  } 
    //! } 
} 
3

你將不得不解析出的顏色和相應的字符串,每個獨特的色彩調配色彩GD資源,並進行單獨的調用imagettftext調整你xy座標需要。

imagettxtext函數不能也不會爲你做這個。

查看imageftbbox,因爲您需要此功能才能計算每個文本切片的邊界框,這是正確放置下一個不同顏色的文本塊所必需的。

以下是將HTML顏色轉換爲十六進制三元組的函數,您可以將它們傳遞給imagecolorallocate

function htmlColorToHex($color) { 
    if (substr($color, 0, 1) == '#') { 
     $color = substr($color, 1); 
    } 

    if (strlen($color) == 3) { 
     $red = str_repeat(substr($color, 0, 1), 2); 
     $green = str_repeat(substr($color, 1, 1), 2); 
     $blue = str_repeat(substr($color, 2, 1), 2); 
    } else { 
     $red = substr($color, 0, 2); 
     $green = substr($color, 2, 2); 
     $blue = substr($color, 4, 2); 
    } 

    $hex = array('r' => hexdec($red), 
       'g' => hexdec($green), 
       'b' => hexdec($blue)); 

    return $hex; 
} 

想要做的事情最複雜的部分是要正確計算每個文本部分的座標。