8
votes

Comment puis-je donner une couleur à l'imagecolorallocate?

J'ai une variable PHP qui contient des informations sur la couleur. Par exemple $ text_color = "ff90f3" code>. Maintenant, je veux donner cette couleur à imagecolorallocate code>. Le imagecolorallocate code> fonctionne comme celui-là:

Imagecolorallocate ($ im, 0xff, 0xff, 0xff); code> p>

donc, j'essaie de faire le Suivant: P>

$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);


5 commentaires

Que fait la fonction bin2hex?


Je mets à bin2hex là-bas pour transformer des chaînes en nombre hexadécimal qui devrait être donné à l'imagecolorallocate.


Quelle est la différence entre "chaîne" et "numéro hexadécimal"? Et je demandais quelle cette fonction ne le fait, pas pourquoi l'avez-vous utilisée? Qu'est-ce qu'il retourne au moins? Dans ce cas même, je veux dire


Si je l'utilise "pour transformer des chaînes en hexadécimal" que "transforme les cordes en hexadécimal". Il faut une chaîne et retourne hexadécimal. C'est ce que ça fait.


Pensez à quelle chaîne vous nourrissez-vous à cette fonction.


3 Réponses :


6
votes

Utiliser heexdec () (Exemple: heexdec ("A0") )

http://fr2.php.net/manual/fr/function. hexdec.php


0 commentaires

13
votes

de http: // forums. devshed.com/php-development-5/gd-hex-resource-imagerecolorallocacate-265852.html xxx

utilisation xxx < p> La couleur peut être transmise en tant qu'hex FFFFFF ou comme #ffffff


0 commentaires

1
votes
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
}

0 commentaires