2010-01-22 14 views
0

Je travaille actuellement sur une application qui doit analyser un certain nombre d'images et déterminer de quelle couleur elles sont les plus proches.Analyse d'image couleur PHP avec transparence

Par conséquent, je trouve un extrait de code qui fait exactement cela:

function analyzeImageColors($im, $xCount =3, $yCount =3) 
    { 
    //get dimensions for image 
    $imWidth =imagesx($im); 
    $imHeight =imagesy($im); 
    //find out the dimensions of the blocks we're going to make 
    $blockWidth =round($imWidth/$xCount); 
    $blockHeight =round($imHeight/$yCount); 
    //now get the image colors... 
    for($x =0; $x<$xCount; $x++) { //cycle through the x-axis 
     for ($y =0; $y<$yCount; $y++) { //cycle through the y-axis 
     //this is the start x and y points to make the block from 
     $blockStartX =($x*$blockWidth); 
     $blockStartY =($y*$blockHeight); 
     //create the image we'll use for the block 
     $block =imagecreatetruecolor(1, 1); 
     //We'll put the section of the image we want to get a color for into the block 
     imagecopyresampled($block, $im, 0, 0, $blockStartX, $blockStartY, 1, 1, $blockWidth, $blockHeight); 
     //the palette is where I'll get my color from for this block 
     imagetruecolortopalette($block, true, 1); 
     //I create a variable called eyeDropper to get the color information 
     $eyeDropper =imagecolorat($block, 0, 0); 
     $palette =imagecolorsforindex($block, $eyeDropper); 
     $colorArray[$x][$y]['r'] =$palette['red']; 
     $colorArray[$x][$y]['g'] =$palette['green']; 
     $colorArray[$x][$y]['b'] =$palette['blue']; 
     //get the rgb value too 
     $hex =sprintf("%02X%02X%02X", $colorArray[$x][$y]['r'], $colorArray[$x][$y]['g'], $colorArray[$x][$y]['b']); 
     $colorArray[$x][$y]['rgbHex'] =$hex; 
     //destroy the block 
     imagedestroy($block); 
     } 
    } 
    //destroy the source image 
    imagedestroy($im); 
    return $colorArray; 
    } 

Le problème est que chaque fois que je donne une image avec transparence, GDLib consinders la transparence est noir, produisant ainsi un mauvais (beaucoup plus sombre) sortie que ce n'est vraiment le cas.

Par exemple cette icône où la zone blanche autour de la flèche est en fait transparente:

example http://img651.imageshack.us/img651/995/screenshot20100122at113.png

Quelqu'un peut-il me dire comment contourner cela?

Répondre

1

Vous avez besoin de imageColorTransparent(). http://www.php.net/imagecolortransparent

La transparence est une propriété de l'image, pas d'une couleur. Donc, utilisez quelque chose comme $transparent = imagecolortransparent($im) pour voir s'il y a de la transparence sur votre image, alors ignorez simplement cette couleur dans votre $ colorArray ou avez une autre façon d'identifier la couleur transparente dans le retour de votre fonction. Tout dépend de la façon dont vous utilisez les données renvoyées.

--M