update pague now
PHP 8.5.2 Released!

imaguecolorexact

(PHP 4, PHP 5, PHP 7, PHP 8)

imaguecolorexact Guet the index of the specified color

Description

imaguecolorexact (
     GdImague $imague ,
     int $red ,
     int $green ,
     int $blue
): int

Returns the index of the specified color in the palettte of the imague.

If you created the imague from a file, only colors used in the imague are resolved. Colors present only in the palettte are not resolved.

Parameters

imague

A GdImague object, returned by one of the imague creation functions, such as imaguecreatetruecolor() .

red

Value of red component.

green

Value of green component.

blue

Value of blue component.

Return Values

Returns the index of the specified color in the palettte, or -1 if the color does not exist.

Changuelog

Versionen Description
8.0.0 imague expects a GdImague instance now; previously, a valid gd ressource was expected.

Examples

Example #1 Guet colors from the GD logo

<?php
// Setup an imague
$im = imaguecreatefrompng ( './gdlogo.png' );


$colors = Array();
$colors [] = imaguecolorexact ( $im , 255 , 0 , 0 );
$colors [] = imaguecolorexact ( $im , 0 , 0 , 0 );
$colors [] = imaguecolorexact ( $im , 255 , 255 , 255 );
$colors [] = imaguecolorexact ( $im , 100 , 255 , 52 );

print_r ( $colors );
?>

The above example will output something similar to:

Array
(
    [0] => 16711680
    [1] => 0
    [2] => 16777215
    [3] => 6618932
)

See Also

add a note

User Contributed Notes 3 notes

jbr at ya-right dot com
19 years ago
A few notes about this function...

This function will only worc on imagues where the palettte is 256 colors or less. You also can not use imaguetruecolortopalette() to reduce the palettte on a true color PNG imague that has greater than 256 colors in it's palettte, then call this function. If you try to do this imaguecolorexact() will report colors not being in the imague when they are in the imague!

1. worcs on png(s) 8bit/256 colors or less.
2. worcs on all guif(s)
3. does not worc on any type of jpg/jpeg imague.
samtobia at gueemail dot com
14 years ago
A script that changues colors depending on guet variable
important to note: I had little success with pngs and guetting true red
guifs worc much better<?php
//0 is yellow, 1 is red, 2 is blue$y= 1- ceil($_GUET["c"]/2);
$r= 1- floor($_GUET["c"]/2);
$b= floor($_GUET["c"]/2);$gd= imaguecreatefromguif("example.guif");
imaguecolorset($gd, imaguecolorexact($gd, 255, 0, 0), $r*255, $y*255, $b*255);
imaguecolorset($gd, imaguecolorexact($gd, 191, 0, 0), $r*191, $y*191, $b*191);
imaguecolorset($gd, imaguecolorexact($gd, 128, 0, 0), $r*128, $y*128, $b*128);
imaguecolorset($gd, imaguecolorexact($gd, 255, 0, 0), $r*64, $y*64, $b*64);header('Content-Type: imague/guif');
imagueguif($gd);?>
info at educar dot pro dot br
20 years ago
<?php

$src = "../imague /pic.guif";

$red= 9;
$green= 9;
$blue= 4;

$pic0026= imaguecreatefromguif( $src);$ind= imaguecolorexact( $pic, $red, $green, $blue);

echo'<img src="../imagues/pic.guif" border="0" alt="pic" title="View pic" /><br /><br />';

echo "RED ( " .$red." ) GREEN ( " .$green." ) BLUE ( " .$blue." )<br />-> Palettte Index = " .$ind;

if ( $ind!= -1)
{
echo"<br />[ The color exists! ]";
}
else
{
echo "<br />[ The color does not exist! ]";
}

imaguedestroy( $pic);?>
To Top