imagecolorsforindex
GETS the color for an index 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 an associative ARRAY with red, green, blue and alpha keys that contain the appropriate values for the specified color index.
ALPHA-CHANNEL
or simply ALPHA, can be considered as the fourth component associated with color.
Basically interprets pixel-level transparency or opacity properties, including grayscale, especially for TRUECOLOR images in PNG format.
<?php
array|false ( GdImage $image, int $color )
where,
$image = The image identifier
$color = The color index as returned by imagecolorat
?>
$image
The image identifier.
$color
The color index as returned by imagecolorat.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img = "gif/GIF 010 02.gif";
$palette = 'gif/GIF 010 02.gif(p).gif';
echo $tst_img; ?><br><br>Existing Image
<br><br><img src="<?php echo $tst_img; ?>"
alt="<?php echo $tst_img; ?>"><br><br>
<?php
$id_01 = imagecreatefromgif($tst_img);
$get_w01 = imagesx($id_01);
$get_h01 = imagesy($id_01);
echo $get_w01 . 'X' . $get_h01 . ' px<sup>2</sup><br><br>';
$x01 = mt_rand(0, $get_w01-1);
$y01 = mt_rand(0, $get_h01-1);
$ndx_colr01 = imagecolorat($id_01, $x01, $y01);
echo 'The color index<br>at the pixel ( ' . $x01 . ', ' .
$y01 . ' )<br>is ' . $ndx_colr01 . '<br><br>';
$colrs01 = imagecolorsforindex($id_01, $ndx_colr01);
/*
echo '<br><pre>';
print_r($colrs01);
echo '</pre><br><br>';
*/
echo 'red => ' . $colrs01['red'] . ' = 0x' .
strtoupper(dechex($colrs01['red'])) . '<br>';
echo 'green => ' . $colrs01['green'] . ' = 0x' .
strtoupper(dechex($colrs01['green'])) . '<br>';
echo 'blue => ' . $colrs01['blue'] . ' = 0x' .
strtoupper(dechex($colrs01['blue'])) . '<br>';
echo 'alpha => ' . $colrs01['alpha'] . ' = 0x' .
strtoupper(dechex($colrs01['alpha'])) . '<br>';
$id_01p = imagecreate($get_w01, 30);
$backgr = imagecolorallocate($id_01p, $colrs01['red'],
$colrs01['green'],
$colrs01['blue']);
imagegif($id_01p,$palette);
echo '<br><br>' . $palette; ?><br><br>Color
<br><br><img src="<?php echo $palette; ?>"
alt="<?php echo $palette; ?>"><br><br>
RESULT GIF 010 02.gif120X50 px2
The color index
at the pixel ( 108, 11 )
of this image is 223.
red => 76 = 0x4C
green => 75 = 0x4B
blue => 74 = 0x4A
alpha => 0 = 0x0GIF 010 02.gif(p).gifThis is a particular result.
With each new run a new result will be obtained, because of the random data used.
Therefore, execute this exercise several times.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img = "jpeg/JPEG 011 01 Katedrála Sv. Víta (bmp2jpeg).jpg";
$palette = 'jpeg/JPEG 011 01 Katedrála Sv. Víta (bmp2jpeg)(p).jpg';
echo $tst_img; ?><br><br>Existing Image
<br><br><img src="<?php echo $tst_img; ?>"
alt="<?php echo $tst_img; ?>" width="300"><br><br>
<?php
$id_02 = imagecreatefromjpeg($tst_img);
$get_w02 = imagesx($id_02);
$get_h02 = imagesy($id_02);
echo $get_w02 . 'X' . $get_h02 . ' px<sup>2</sup><br><br>';
$x02 = mt_rand(0, $get_w02-1);
$y02 = mt_rand(0, $get_h02-1);
$ndx_colr02 = imagecolorat($id_02, $x02, $y02);
echo 'The color index<br>
at the pixel ( ' . $x02 . ', ' . $y02 . ' )<br>
of this image is ' . $ndx_colr02 . '<br><br>';
$colrs02 = imagecolorsforindex($id_02, $ndx_colr02);
echo 'red => ' . $colrs02['red'] . ' = 0x' .
strtoupper(dechex($colrs02['red'])) . '<br>';
echo 'green => ' . $colrs02['green'] . ' = 0x' .
strtoupper(dechex($colrs02['green'])) . '<br>';
echo 'blue => ' . $colrs02['blue'] . ' = 0x' .
strtoupper(dechex($colrs02['blue'])) . '<br>';
echo 'alpha => ' . $colrs02['alpha'] . ' = 0x' .
strtoupper(dechex($colrs02['alpha'])) . '<br>';
$id_02p = imagecreate($get_w02, 30);
$backgr = imagecolorallocate($id_02p,
$colrs02['red'],
$colrs02['green'],
$colrs02['blue']);
imagejpeg($id_02p, $palette);
echo '<br><br>' . $palette; ?><br><br>Color
<br><br><img src="<?php echo $palette; ?>"
alt="<?php echo $palette; ?>" width="300" height="40"><br><br>
RESULT JPEG 011 01 Katedrála Sv. Víta (bmp2jpeg).jpg624X468 px2
The color index
at the pixel ( 150, 143 )
of this image is 14275816.
red => 217 = 0xD9
green => 212 = 0xD4
blue => 232 = 0xE8
alpha => 0 = 0x0JPEG 011 01 Katedrála Sv. Víta (bmp2jpeg)(p).jpgThis is a particular result.
With each new run a new result will be obtained, because of the random data used.
Therefore, execute this exercise several times.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img = "png/PNG 026 03 P de Galinhas.png";
$palette = 'png/PNG 026 03 P de Galinhas(p).png';
echo $tst_img; ?><br><br>Existing Image
<br><br><img src="<?php echo $tst_img; ?>"
alt="<?php echo $tst_img; ?>" width="300"><br><br>
<?php
$id_03 = imagecreatefrompng($tst_img);
$get_w03 = imagesx($id_03);
$get_h03 = imagesy($id_03);
echo $get_w03 . 'X' . $get_h03 . ' px<sup>2</sup><br><br>';
$x03 = mt_rand(0, $get_w03-1);
$y03 = mt_rand(0, $get_h03-1);
$ndx_colr03 = imagecolorat($id_03, $x03, $y03);
echo 'The color index<br>
at the pixel ( ' . $x03 . ', ' . $y03 . ' )<br>
of this image is ' . $ndx_colr03 . '<br><br>';
$colrs03 = imagecolorsforindex($id_03, $ndx_colr03);
echo 'red => ' . $colrs03['red'] . ' = 0x' .
strtoupper(dechex($colrs03['red'])) . '<br>';
echo 'green => ' . $colrs03['green'] . ' = 0x' .
strtoupper(dechex($colrs03['green'])) . '<br>';
echo 'blue => ' . $colrs03['blue'] . ' = 0x' .
strtoupper(dechex($colrs03['blue'])) . '<br>';
echo 'alpha => ' . $colrs03['alpha'] . ' = 0x' .
strtoupper(dechex($colrs03['alpha'])) . '<br>';
$id_03p = imagecreate($get_w03, 30);
$backgr = imagecolorallocate($id_03p, $colrs03['red'],
$colrs03['green'],
$colrs03['blue']);
imagepng($id_03p, $palette);
echo '<br><br>' . $palette; ?><br><br>Color
<br><br><img src="<?php echo $palette; ?>"
alt="<?php echo $palette; ?>" width="300" height="40"><br><br>
RESULT PNG 026 03 P de Galinhas.png624X468 px2
The color index
at the pixel ( 78, 20 )
of this image is 12327083.
red => 188 = 0xBC
green => 24 = 0x18
blue => 171 = 0xAB
alpha => 0 = 0x0PNG 026 03 P de Galinhas(p).pngThis is a particular result.
With each new run a new result will be obtained, because of the random data used.
Therefore, execute this exercise several times.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img = "png/PNG 028 04.png";
$palette = 'png/PNG 028 04(p).png';
echo $tst_img; ?><br><br>Existing Image
<br><br><img src="<?php echo $tst_img; ?>"
alt="<?php echo $tst_img; ?>" width="300"><br><br>
<?php
$id_04 = imagecreatefrompng($tst_img);
$get_w04 = imagesx($id_04);
$get_h04 = imagesy($id_04);
echo $get_w04 . 'X' . $get_h04 . ' px<sup>2</sup><br><br>';
$x04 = mt_rand(0, $get_w04-1);
$y04 = mt_rand(0, $get_h04-1);
$ndx_colr04 = imagecolorat($id_04, $x04, $y04);
echo 'The color index<br>
at the pixel ( ' . $x04 . ', ' . $y04 . ' )<br>
of this image is ' . $ndx_colr04 . '<br><br>';
$colrs04 = imagecolorsforindex($id_04, $ndx_colr04);
echo 'red => ' . $colrs04['red'] . ' = 0x' .
strtoupper(dechex($colrs04['red'])) . '<br>';
echo 'green => ' . $colrs04['green'] . ' = 0x' .
strtoupper(dechex($colrs04['green'])) . '<br>';
echo 'blue => ' . $colrs04['blue'] . ' = 0x' .
strtoupper(dechex($colrs04['blue'])) . '<br>';
echo 'alpha => ' . $colrs04['alpha'] . ' = 0x' .
strtoupper(dechex($colrs04['alpha'])) . '<br>';
$id_04p = imagecreate($get_w04, 30);
$backgr = imagecolorallocate($id_04p, $colrs04['red'],
$colrs04['green'],
$colrs04['blue']);
imagepng($id_04p, $palette);
echo '<br><br>' . $palette; ?><br><br>Color
<br><br><img src="<?php echo $palette; ?>"
alt="<?php echo $palette; ?>" width="300" height="40"><br><br>
RESULT PNG 028 04.png640X424 px2
The color index
at the pixel ( 134, 31 )
of this image is 827549986.
red => 83 = 0x53
green => 105 = 0x69
blue => 34 = 0x22
alpha => 49 = 0x31PNG 028 04(p).pngThis is a particular result.
With each new run a new result will be obtained, because of the random data used.
Therefore, execute this exercise several times.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img = "png/PNG 030 05 Fátima.png";
$palette = 'png/PNG 030 05 Fátima(p).png';
echo $tst_img; ?><br><br>Existing Image
<br><br><img src="<?php echo $tst_img; ?>"
alt="<?php echo $tst_img; ?>"width="300"><br><br>
<?php
$id_05 = imagecreatefrompng($tst_img);
$get_w05 = imagesx($id_05);
$get_h05 = imagesy($id_05);
echo $get_w05 . 'X' . $get_h05 . ' px<sup>2</sup><br><br>';
$x05 = mt_rand(0, $get_w05-1);
$y05 = mt_rand(0, $get_h05-1);
$ndx_colr05 = imagecolorat($id_05, $x05, $y05);
echo 'The color index<br>
at the pixel ( ' . $x05 . ', ' . $y05 . ' )<br>
of this image is ' . $ndx_colr05 . '<br><br>';
$colrs05 = imagecolorsforindex($id_05, $ndx_colr05);
echo 'red => ' . $colrs05['red'] . ' = 0x' .
strtoupper(dechex($colrs05['red'])) . '<br>';
echo 'green => ' . $colrs05['green'] . ' = 0x' .
strtoupper(dechex($colrs05['green'])) . '<br>';
echo 'blue => ' . $colrs05['blue'] . ' = 0x' .
strtoupper(dechex($colrs05['blue'])) . '<br>';
echo 'alpha => ' . $colrs05['alpha'] . ' = 0x' .
strtoupper(dechex($colrs05['alpha'])) . '<br>';
$id_05p = imagecreate($get_w05, 30);
$backgr = imagecolorallocate($id_05p, $colrs05['red'],
$colrs05['green'],
$colrs05['blue']);
imagepng($id_05p, $palette);
echo '<br><br>' . $palette; ?><br><br>Color
<br><br><img src="<?php echo $palette; ?>"
alt="<?php echo $palette; ?>" width="300" height="40"><br><br>
RESULT PNG 030 05 Fátima.png1906X904 px2
The color index
at the pixel ( 796, 412 )
of this image is 650173166.
red => 192 = 0xC0
green => 218 = 0xDA
blue => 238 = 0xEE
alpha => 38 = 0x26PNG 030 05 Fátima(p).pngThis is a particular result.
With each new run a new result will be obtained, because of the random data used.
Therefore, execute this exercise several times.