2010-06-02 21 views
5

मेरे पास एक PHP चर है जिसमें रंग के बारे में जानकारी है। उदाहरण के लिए $text_color = "ff90f3"। अब मैं यह रंग imagecolorallocate पर देना चाहता हूं। ऐसे ही imagecolorallocate काम करता है:मैं imagecolorallocate को रंग कैसे दे सकता हूं?

imagecolorallocate($im, 0xFF, 0xFF, 0xFF);

तो, मैं निम्न कार्य करने की कोशिश कर रहा हूँ:

$r_bg = bin2hex("0x".substr($text_color,0,2)); 
$g_bg = bin2hex("0x".substr($text_color,2,2)); 
$b_bg = bin2hex("0x".substr($text_color,4,2)); 
$bg_col = imagecolorallocate($image, $r_bg, $g_bg, $b_bg); 

यह काम नहीं करता। क्यूं कर? मैं bin2hex के बिना भी कोशिश करता हूं, यह भी काम नहीं करता है। क्या कोई मेरी मदद कर सकता है?

+0

bin2hex फ़ंक्शन क्या करता है? –

+0

मैंने स्ट्रिंग को हेक्साडेसिमल संख्या में बदलने के लिए bin2hex रखा है जिसे imagecolorallocate को दिया जाना चाहिए। – Roman

+0

"स्ट्रिंग" और "हेक्साडेसिमल नंबर" के बीच क्या अंतर है? और मैं पूछ रहा था कि यह कार्य क्या करता है, आपने इसका उपयोग क्यों नहीं किया। कम से कम यह क्या लौटता है? इस मामले में मेरा मतलब है –

उत्तर

5

http://forums.devshed.com/php-development-5/gd-hex-resource-imagecolorallocate-265852.html से

function hexColorAllocate($im,$hex){ 
    $hex = ltrim($hex,'#'); 
    $a = hexdec(substr($hex,0,2)); 
    $b = hexdec(substr($hex,2,2)); 
    $c = hexdec(substr($hex,4,2)); 
    return imagecolorallocate($im, $a, $b, $c); 
} 

प्रयोग

$img = imagecreatetruecolor(300, 100); 
$color = hexColorAllocate($img, 'ffff00'); 
imagefill($img, 0, 0, $color); 

रंग हेक्सके रूप में पारित किया जा सकताया #ffffff

0
function hex2RGB($hexStr, $returnAsString = false, $seperator = ',') { 
    $hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string 
    $rgbArray = array(); 
    if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster 
     $colorVal = hexdec($hexStr); 
     $rgbArray['red'] = 0xFF & ($colorVal >> 0x10); 
     $rgbArray['green'] = 0xFF & ($colorVal >> 0x8); 
     $rgbArray['blue'] = 0xFF & $colorVal; 
    } elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations 
     $rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2)); 
     $rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2)); 
     $rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2)); 
    } else { 
     return false; //Invalid hex color code 
    } 
    return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array 
} 
संबंधित मुद्दे