imagecolorresolve
GET index of the specified color or its closest possible alternative in an image file.
This function get the index of an specific pixel color $image.
$image symbolized by a identifier returned by one of the image creation functions, such as imagecreate or imagecreatetruecolor.
This function returns the index for a requested color, either the exact color or the closest possible alternative.
If you created the image from a file, only colors used in the image are resolved.
Colors present only in the palette are not resolved.
<?php
int imagecolorresolve( GdImage $image,
int $red,
int $green,
int $blue )
where,
$image = The image identifier
$red = The value of red component color
$green = The value of green component color
$blue = The value of blue component color
?>
$image
The image identifier.
$red
The value of red component of color.
$green
The value of green component of color.
$blue
The value of blue component of color.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$base_img01 = "gif/GIF 015 01.gif";
$im01 = imagecreatefromgif($base_img01);
$nbr_c = imagecolorstotal($im01);
$pal_ref01 = "gif/GIF 015 01-searched color.gif";
$pal_res01 = "gif/GIF 015 01-resolved color.gif";
if($nbr_c == FALSE)
{
$en = '<br><br>TRUECOLOR IMAGE ';
}
else
{
$en = '<br><br>PALETTE IMAGE<br>'. $nbr_c .' colors';
}
echo $base_img01; ?><?php echo $$lang; ?><br><br>
<img src="<?php echo $base_img01; ?>"
alt="<?php echo $base_img01; ?>"
title="<?php echo $base_img01; ?>" width="400"><br><br>
<?php
$red_ref01 = mt_rand(0, 255);
$green_ref01 = mt_rand(0, 255);
$blue_ref = mt_rand(0, 255);
$en = "<br>" . $pal_ref01 . "<br><br>Referenced Color: ";
echo $$lang . ' RGB = ( ' . $red_ref01 . ', '
. $green_ref01 . ', ' . $blue_ref . ' )';
$imref01 = imagecreate(imagesx($im01), 60);
$ref_color01 =
imagecolorallocate($imref01, $red_ref01, $green_ref01, $blue_ref);
imagegif($imref01, $pal_ref01);
?>
<br><br><img src="<?php echo $pal_ref01; ?>"
alt="<?php echo $pal_ref01; ?>"
title="<?php echo $pal_ref01; ?>" width="400" height="40"><br><br>
<?php
$ndx_res01 =
imagecolorresolve($im01, $red_ref01, $green_ref01, $blue_ref);
$arr_res01 =
imagecolorsforindex($im01, $ndx_res01);
$red_res01 = $arr_res01['red'];
$green_res01 = $arr_res01['green'];
$blue_res01 = $arr_res01['blue'];
$en = "<br>" . $pal_res01 . "<br><br>Resolved Color: ";
echo $$lang . ' RGB = ( ' . $red_res01 . ', '
. $green_res01 . ', ' . $blue_res01 . ' )';
$imres01 = imagecreate(imagesx($im01), 60);
$res_color =
imagecolorallocate($imres01, $red_res01, $green_res01, $blue_res01);
imagegif($imres01, $pal_res01);
?>
<br><br><img src="<?php echo $pal_res01; ?>"
alt="<?php echo $pal_res01; ?>"
title="<?php echo $pal_res01; ?>" width="400" height="40"><br><br>
RESULT GIF 015 01.gif
PALETTE IMAGE
256 COLORS ( BASE IMAGE )
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$base_img02 = "png/PNG 034 02.png";
$im02 = imagecreatefrompng($base_img02);
$nbr_c = imagecolorstotal($im02);
$pal_ref02 = "png/PNG 034 02-searched color.png";
$pal_res02 = "png/PNG 034 02-resolved color.png";
if($nbr_c == FALSE)
{
$en = '<br><br>TRUECOLOR IMAGE ';
}
else
{
$en = '<br><br>PALETTE IMAGE '. $nbr_c .' colors';
}
echo $base_img02; ?><?php echo $$lang; ?><br><br>
<img src="<?php echo $base_img02; ?>"
alt="<?php echo $base_img02; ?>"
title="<?php echo $base_img02; ?>" width="400"><br><br>
<?php
$red_ref02 = mt_rand(0, 255);
$green_ref02 = mt_rand(0, 255);
$blue_ref = mt_rand(0, 255);;
$en = "<br>" . $pal_ref02 . "<br><br>Referenced Color: ";
echo $$lang . ' RGB = ( ' . $red_ref02 . ', '
. $green_ref02 . ', ' . $blue_ref . ' )';
$rsrref02 = imagecreate(imagesx($im02), 60);
$ref_color02 =
imagecolorallocate($rsrref02, $red_ref02, $green_ref02, $blue_ref);
imagepng($rsrref02, $pal_ref02);
?>
<br><br><img src="<?php echo $pal_ref02; ?>"
alt="<?php echo $pal_ref02; ?>"
title="<?php echo $pal_ref02; ?>" width="400" height="40"><br><br>
<?php
$ndx_res02 =
imagecolorresolve($im02, $red_ref02, $green_ref02, $blue_ref);
$arr_res02 =
imagecolorsforindex($im02, $ndx_res02);
$red_res02 = $arr_res02['red'];
$green_res02 = $arr_res02['green'];
$blue_res02 = $arr_res02['blue'];
$en = "<br>" . $pal_res02 . "<br><br>Resolved Color: ";
echo $$lang . ' RGB = ( ' . $red_res02 . ', '
. $green_res02 . ', ' . $blue_res02 . ' )';
$rsrres02 = imagecreate(imagesx($im02), 60);
$res_color =
imagecolorallocate($rsrres02, $red_res02, $green_res02, $blue_res02);
imagepng($rsrres02, $pal_res02);
?>
<br><br><img src="<?php echo $pal_res02; ?>"
alt="<?php echo $pal_res02; ?>"
title="<?php echo $pal_res02; ?>" width="400" height="40"><br><br>
RESULT PNG 034 02.png
TRUECOLOR IMAGE ( BASE IMAGE )