imagecolorclosest
GET the index of the closest color to the specified color 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 of the specified color which is closest to the specified RGB value.
The distance between the desired color and each color in the palette is calculated as if the RGB values represented points in three-dimensional space.
Colors present only in the palette are not resolved.
If the image was created from a file, only colors used in the image are resolved.
<?php
int imagecolorclosest( 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
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$tst_img01 = "gif/GIF 009 01.gif";
$exact_col_img01 = "gif/GIF 009 01-exact color.gif";
$closest_col_img01 = "gif/GIF 009 01-closest color.gif";
$cs_red01 = 204;
$cs_green01 = 204;
$cs_blue01 = 0;
echo $tst_img01; ?>
<br><br><img src="<?php echo $tst_img01; ?>"
alt="<?php echo $tst_img01; ?>" width="400"><br><br>
<?php
$im01 = imagecreatefromgif($tst_img01);
$ndx_exc_pal01 =
imagecolorexact($im01, $cs_red01, $cs_green01, $cs_blue01);
if ($ndx_exc_pal01 == -1)
{
$im01s = imagecreate(imagesx($im01), 30);
imagecolorallocate($im01s, $cs_red01, $cs_green01, $cs_blue01);
imagegif($im01s, $exact_col_img01);
$en = 'The reported color is not part of the image!';
echo $exact_col_img01 . '<br><br>';
echo '<img src="' . $exact_col_img01
. '" alt="' . $exact_col_img01 . '"' . ' width="400">';
echo '<br><br>RGB = ( ' . $cs_red01 . ', ' . $cs_green01
. ', ' . $cs_blue01 . ' )<br>' . $$lang ;
$en = 'Index = ' . $ndx_exc_pal01;
echo '<br>' . $$lang;
$ndx_closest_pal01 =
imagecolorclosest($im01, $cs_red01, $cs_green01, $cs_blue01);
$color_ndx_closest_pal01 =
imagecolorsforindex( $im01, $ndx_closest_pal01);
$ct_red01 = $color_ndx_closest_pal01['red'];
$ct_green01 = $color_ndx_closest_pal01['green'];
$ct_blue01 = $color_ndx_closest_pal01['blue'];
$im01t = imagecreate(imagesx($im01), 30);
imagecolorallocate($im01t, $ct_red01, $ct_green01, $ct_blue01);
imagegif($im01t, $closest_col_img01);
$en = '<br>The closest color!';
echo '<br><br><br>' . $closest_col_img01;
echo '<br><br><img src="' . $closest_col_img01
. '" alt="' . $closest_col_img01 . '"' . ' width="400">';
echo '<br><br>RGB = ( ' . $ct_red01 . ', ' . $ct_green01
. ', ' . $ct_blue01 . ' ) ' . $$lang;
$en = 'Index = ' . $ndx_closest_pal01;
echo '<br>' . $$lang . '';
}
else
{
$im01s = imagecreate(imagesx($im01), 30);
imagecolorallocate($im01s,$cs_red01, $cs_green01, $cs_blue01);
imagegif($im01s, $exact_col_img01);
echo '* ' . $exact_col_img01 . ' ';
$en = '<br><br>
* The reported color is part of the image!
<br>';
echo '<br>* RGB = ( ' . $cs_red01 . ', ' . $cs_green01
. ', ' . $cs_blue01 . ' )' . $$lang;
echo '<br><img src="' . $exact_col_img01
. '" alt="' . $exact_col_img01 . '"' . ' width="400">';
$en = '<br>* Index = ' . $ndx_exc_pal01;
echo '<br>' . $$lang . '<br>';
}
?>
RESULT GIF 009 01.gifGIF 009 01-exact color.gifRGB = ( 204, 204, 0 )
The reported color is not part of the image palette!
Palette Index = -1GIF 009 01-closest color.gifRGB = ( 201, 212, 16 )
The closest color!
Palette Index = 184
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$tst_img02 = "gif/GIF 014 01 white-red.gif";
$exact_col_img02 = "gif/GIF 014 01 white-red-exact color.gif";
$closest_col_img02 = "gif/GIF 014 01 white-red-closest color.gif";
$cs_red02 = mt_rand(0, 255);
$cs_green02 = mt_rand(0, 255);
$cs_blue02 = mt_rand(0, 255);
/*
// Try this values for the color to see the difference
$cs_red02 = 0;
$cs_green02 = 0;
$cs_blue02 = 254;
*/
echo $tst_img02; ?>
<br><br><img src="<?php echo $tst_img02; ?>"
alt="<?php echo $tst_img02; ?>"><br><br>
<?php
$im02 = imagecreatefromgif($tst_img02);
$ndx_exc_pal02 =
imagecolorexact($im02,
$cs_red02, $cs_green02, $cs_blue02);
if ($ndx_exc_pal02 == -1)
{
$im02s = imagecreate(imagesx($im02), 30);
imagecolorallocate($im02s,
$cs_red02, $cs_green02, $cs_blue02);
imagegif($im02s, $exact_col_img02);
$en = 'The reported color is not part of the image!';
echo $exact_col_img02 . '<br><br>';
echo '<img src="' . $exact_col_img02
. '" alt="' . $exact_col_img02 . '"' . '>';
echo '<br><br>RGB = ( ' . $cs_red02 . ', ' . $cs_green02
. ', ' . $cs_blue02 . ' )<br>' . $$lang;
$en = 'Palette Index = ' . $ndx_exc_pal02;
echo '<br>' . $$lang;
$ndx_closest_pal02 =
imagecolorclosest($im02,
$cs_red02, $cs_green02, $cs_blue02);
$color_ndx_closest_pal02 =
imagecolorsforindex( $im02, $ndx_closest_pal02);
$ct_red02 = $color_ndx_closest_pal02['red'];
$ct_green02 = $color_ndx_closest_pal02['green'];
$ct_blue02 = $color_ndx_closest_pal02['blue'];
$im02t = imagecreate(imagesx($im02), 30);
imagecolorallocate($im02t,
$ct_red02, $ct_green02, $ct_blue02);
imagegif($im02t, $closest_col_img02);
$en = '<br>The closest color!';
echo '<br><br><br>' . $closest_col_img02;
echo '<br><br><img src="' . $closest_col_img02
. '" alt="' . $closest_col_img02 . '"' . '>';
echo '<br><br>RGB = ( ' . $ct_red02 . ', ' . $ct_green02
. ', ' . $ct_blue02 . ' ) ' . $$lang;
$en = 'Palette Index = ' . $ndx_closest_pal02;
echo '<br>' . $$lang . '';
}
else
{
$im02s = imagecreate(imagesx($im02), 30);
imagecolorallocate($im02s,
$cs_red02, $cs_green02, $cs_blue02);
imagegif($im02s, $exact_col_img02);
echo '* ' . $exact_col_img02 . ' ';
$en = '<br><br>
* The reported color is part of the image palette!
<br>';
echo '<br>* RGB = ( ' . $cs_red02 . ', ' . $cs_green02
. ', ' . $cs_blue02 . ' )' . $$lang;
echo '<br><img src="' . $exact_col_img02
. '" alt="' . $exact_col_img02 . '"' . '>';
$en = '<br>* Palette Index = ' . $ndx_exc_pal02;
echo '<br>' . $$lang . '<br>';
}
?>
RESULT GIF 014 01 white-red.gifGIF 014 01 white-red-exact color.gifTo be searched for in the image.RGB = ( 186, 60, 100 )
The reported color is not part of the image palette!
Palette Index = -1GIF 014 01 white-red-closest color.gifRGB = ( 226, 90, 67 )
The closest color!
Palette Index = 10
This is a particular result.
Every new execution a new result should be obtained.
DO THE TEST YOURSELF.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$tst_img03 = "jpeg/JPEG 019 03 white-red.jpg";
$exact_col_img03 = "jpeg/JPEG 019 03 white-red-exact color.jpg";
$closest_col_img03 = "jpeg/JPEG 019 03 white-red-closest color.jpg";
$cs_red03 = mt_rand(0, 255);
$cs_green03 = mt_rand(0, 255);
$cs_blue03 = mt_rand(0, 255);
/*
// Try this values for the color to see the difference
$cs_red03 = 0;
$cs_green03 = 0;
$cs_blue03 = 254;
*/
echo $tst_img03; ?>
<br><br><img src="<?php echo $tst_img03; ?>"
alt="<?php echo $tst_img03; ?>"><br><br>
<?php
$im03 = imagecreatefromjpeg($tst_img03);
$ndx_exc_pal03 =
imagecolorexact($im03, $cs_red03, $cs_green03, $cs_blue03);
if ($ndx_exc_pal03 == -1)
{
$im03s = imagecreate(imagesx($im03), 30);
imagecolorallocate($im03s, $cs_red03, $cs_green03, $cs_blue03);
imagegjpeg($im03s, $exact_col_img03);
$en = 'The reported color is not part of the image!';
echo 'RGB = ( ' . $cs_red03 . ', ' . $cs_green03
. ', ' . $cs_blue03 . ' )<br>' . $$lang . '<br><br>';
echo $exact_col_img03 . '<br><br>';
echo '<img src="' . $exact_col_img03
. '" alt="' . $exact_col_img03 . '"' . '>';
$en = 'Index = ' . $ndx_exc_pal03;
echo '<br><br>' . $$lang;
$ndx_closest_pal03 =
imagecolorclosest($im03, $cs_red03, $cs_green03, $cs_blue03);
$color_ndx_closest_pal03 =
imagecolorsforindex( $im03, $ndx_closest_pal03);
$ct_red03 = $color_ndx_closest_pal03['red'];
$ct_green03 = $color_ndx_closest_pal03['green'];
$ct_blue03 = $color_ndx_closest_pal03['blue'];
$im03t = imagecreate(imagesx($im03), 30);
imagecolorallocate($im03t, $ct_red03, $ct_green03, $ct_blue03);
imagejpeg($im03t, $closest_col_img03);
$en = '<br>The closest color!';
echo '<br><br><br><br><br>RGB = ( ' . $ct_red03 . ', ' . $ct_green03
. ', ' . $ct_blue03 . ' )' . $$lang . '<br><br>';
echo $closest_col_img03;
echo '<br><br><img src="' . $closest_col_img03
. '" alt="' . $closest_col_img03 . '"' . '>';
$en = 'Index = ' . $ndx_closest_pal03;
echo '<br><br>' . $$lang . '';
}
else
{
$im03s = imagecreate(imagesx($im03), 30);
imagecolorallocate($im03s, $cs_red03, $cs_green03, $cs_blue03);
imagejpeg($im03s, $exact_col_img03);
echo '<br>' . $exact_col_img03 . '<br><br>';
$en = '<br>The reported color is part of the image!<br>';
echo '<img src="' . $exact_col_img03
. '" alt="' . $exact_col_img03 . '"' . '>';
echo '<br><br>RGB = ( ' . $cs_red03 . ', ' . $cs_green03
. ', ' . $cs_blue03 . ' )' . $$lang;
$en = 'Index = ' . $ndx_exc_pal03;
echo $$lang . '<br>';
}
?>
RESULT JPEG 019 03 white-red.jpgJPEG 019 03 white-red-exact color.jpgRGB = ( 35, 190, 134 )
The reported color is part of the image palette!
Palette Index = 2342534
Every new execution a new result should be obtained.
DO THE TEST YOURSELF.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$tst_img04 = "png/PNG 033 04-test image.png";
$exact_col_img04 = "png/PNG 033 04-exact color.png";
$closest_col_img04 = "png/PNG 033 04-closest color.png";
$cs_red04 = mt_rand(0, 255);
$cs_green04 = mt_rand(0, 255);
$cs_blue04 = mt_rand(0, 255);
$xf04 = 20;
$yf04 = 20;
$im04 = imagecreate(384, 288);
$blue04 = imagecolorallocate($im04, 0, 0, 255);
imagefill($im04, $xf04, $yf04, $blue04);
imagepng($im04, $tst_img04);
echo $tst_img04; ?><br><br>
<img src="<?php echo $tst_img04; ?>"
alt="<?php echo $tst_img04; ?>"
title="<?php echo $tst_img04; ?>">
<?php
$ndx_exc_pal04 =
imagecolorexact($im04, $cs_red04, $cs_green04, $cs_blue04);
if ($ndx_exc_pal04 == -1)
{
$im04s = imagecreate(imagesx($im04), 30);
imagecolorallocate($im04s, $cs_red04, $cs_green04, $cs_blue04);
imagepng($im04s, $exact_col_img04);
$en = 'The reported color is not part of the image!';
echo '<br><br>' . $exact_col_img04 . '<br><br>';
echo '<img src="' . $exact_col_img04
. '" alt="' . $exact_col_img04 . '"' . '>';
echo '<br><br>RGB = ( ' . $cs_red04 . ', ' . $cs_green04
. ', ' . $cs_blue04 . ' )<br>' . $$lang;
$en = '<br>Index = ' . $ndx_exc_pal04;
echo $$lang;
$ndx_closest_pal04 =
imagecolorclosest($im04, $cs_red04, $cs_green04, $cs_blue04);
$color_ndx_closest_pal04 =
imagecolorsforindex( $im04, $ndx_closest_pal04);
$ct_red04 = $color_ndx_closest_pal04['red'];
$ct_green04 = $color_ndx_closest_pal04['green'];
$ct_blue04 = $color_ndx_closest_pal04['blue'];
$im04t = imagecreate(imagesx($im04), 30);
imagecolorallocate($im04t, $ct_red04, $ct_green04, $ct_blue04);
imagepng($im04t, $closest_col_img04);
$en = '<br>The closest color!';
echo '<br><br>' . $closest_col_img04;
echo '<br><br><img src="' . $closest_col_img04
. '" alt="' . $closest_col_img04 . '"' . '>';
echo '<br><br>RGB = ( ' . $ct_red04 . ', ' . $ct_green04
. ', ' . $ct_blue04 . ' )' . $$lang;
$en = '<br>Index = ' . $ndx_closest_pal04;
echo $$lang . '';
}
else
{
$im04s = imagecreate(imagesx($im04), 30);
imagecolorallocate($im04s, $cs_red04, $cs_green04, $cs_blue04);
imagepng($im04s, $exact_col_img04);
echo '<br><br>* ' . $exact_col_img04 . ' ';
$en = '<br>* The reported color is part of the image!
<br>';
echo '<br>* RGB = ( ' . $cs_red04 . ', ' . $cs_green04
. ', ' . $cs_blue04 . ' )' . $$lang;
echo '<br><img src="' . $exact_col_img04
. '" alt="' . $exact_col_img04 . '"' . '>';
$en = '<br>* Index = ' . $ndx_exc_pal04;
echo '<br>' . $$lang . '<br>';
}
?>
RESULT PNG 033 04-test image.png
PNG 033 04-exact color.pngRGB = ( 55, 137, 91 )
The reported color is not part of the image palette!
Palette Index = -1PNG 033 04-closest color.pngRGB = ( 0, 0, 255 )
The closest color!
Palette Index = 0
Every new execution a new result should be obtained.
Just because the test image has only one color, the closest color will always have the same color, ie blue.
DO THE TEST YOURSELF.