imagecolorat GETS the index of the color of the pixel at the specified location in an image file.
This function get the index of an specific pixel color $image.
$x = 0 and $y = 0 are the coordinates of the top left point.
If $image is truecolor, this function returns the RGB value of that pixel as integer.
This function returns a Boolean FALSE or a non-Boolean wich evaluates to FALSE on failure.
The === operator or the var_dump could be used to test the value returned by this function.
<?php
int|false imagecolorat ( GdImage $image , int $x , int $y )
where,
$image = The image identifier
$x = The x-coordinate of the point
$y = The y-coordinate of the point
?>
$image
The image identifier.
$x
x-coordinate of the point.
$y
y-coordinate of the point.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img01 = "gif/GIF 011 03.gif";
echo $tst_img01; ?><br><br>Existing palette Image
<br><br><img src="<?php echo $tst_img01; ?>" alt="<?php echo $tst_img01; ?>"><br><br>
<?php
$im01 = imagecreatefromgif($tst_img01);
$get_w01 = imagesx($im01);
$get_h01 = imagesy($im01);
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_color01 = imagecolorat($im01, $x01, $y01);
echo 'The color index at the pixel ( ' . $x01 . ', ' . $y01 . ' )
of this image is ' . $ndx_color01 . '<br>';
?>
RESULT GIF 011 03.gifThe color index
at the pixel ( 309, 189 )
of this image is 57.
This 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
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img02 = "jpeg/JPEG 011 01 Katedrála Sv. Víta (bmp2jpeg).jpg";
echo $tst_img02; ?><br><br>Existing truecolor Image
<br><br><img src="<?php echo $tst_img02; ?>"
alt="<?php echo $tst_img02; ?>" width="400"><br><br>
<?php
$im02 = imagecreatefromjpeg($tst_img02);
$get_w02 = imagesx($im02);
$get_h02 = imagesy($im02);
echo $get_w02 . 'X' . $get_h02 . ' px<sup>2</sup><br><br>';
for($x02=30; $x02 <= 32; $x02++)
for($y02=30; $y02 <= 32; $y02++)
{
$ndx_color02 = imagecolorat($im02, $x02, $y02);
echo 'The color index at<br>the pixel ( ' . $x02 . ', ' . $y02 . ' )
<br> is ( ' . $ndx_color02 . ' )<sub>D</sub> = ( ' . dechex($ndx_color02) . ' )<sub>H</sub><br><br>';
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This exercise consumes a lot of time when:
$x02 = 0
$x02 < $get_w02
and
$y02 = 0
$y02 < $get_h02
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT JPEG 011 01 Katedrála Sv. Víta (bmp2jpeg).jpg616X408 px2
The color index at
the pixel ( 30, 30 )
is ( 11384786 )D = ( adb7d2 )H
The color index at
the pixel ( 30, 31 )
is ( 11384786 )D = ( adb7d2 )H
The color index at
the pixel ( 30, 32 )
is ( 11712981 )D = ( b2b9d5 )H
The color index at
the pixel ( 31, 30 )
is ( 11450579 )D = ( aeb8d3 )H
The color index at
the pixel ( 31, 31 )
is ( 11384786 )D = ( adb7d2 )H
The color index at
the pixel ( 31, 32 )
is ( 11778774 )D = ( b3bad6 )H
The color index at
the pixel ( 32, 30 )
is ( 11712981 )D = ( b2b9d5 )H
The color index at
the pixel ( 32, 31 )
is ( 11712981 )D = ( b2b9d5 )H
The color index at
the pixel ( 32, 32 )
is ( 11909847 )D = ( b5bad7 )H
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img03 = "bmp/BMP 004 01 Katedrála Sv. Víta.bmp";
echo $tst_img03; ?><br><br>Existing truecolor Image
<br><br><img src="<?php echo $tst_img03; ?>"
alt="<?php echo $tst_img03; ?>" width="400"><br><br>
<?php
$im03 = imagecreatefrombmp($tst_img03);
$get_w03 = imagesx($im03);
$get_h03 = imagesy($im03);
echo "$get_w03 px X $get_h03 px<br><br>";
for($x03=40; $x03 < 44; $x03++)
for($y03=40; $y03 < 44; $y03++)
{
$ndx_color03 = imagecolorat($im03, $x03, $y03);
echo 'The color index at<br>the pixel ( ' . $x03 . ', ' . $y03 . ' )
<br>is ( ' . $ndx_color03 . ' )<sub>D</sub> = ( ' . strtoupper(dechex($ndx_color03)) . ' )<sub>H</sub><br><br>';
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This exercise consumes a lot of time when:
$x03 = 0
$x03 < $get_w03
and
$y03 = 0
$y03 < $get_h03
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
RESULT BMP 004 01 Katedrála Sv. Víta.bmp616 px X 408 px
The color index at
the pixel ( 40, 40 )
is ( 12369369 )D = ( BCBDD9 )H
The color index at
the pixel ( 40, 41 )
is ( 12171737 )D = ( B9B9D9 )H
The color index at
the pixel ( 40, 42 )
is ( 12632795 )D = ( C0C2DB )H
The color index at
the pixel ( 40, 43 )
is ( 12237783 )D = ( BABBD7 )H
The color index at
the pixel ( 41, 40 )
is ( 12237270 )D = ( BAB9D6 )H
The color index at
the pixel ( 41, 41 )
is ( 12040147 )D = ( B7B7D3 )H
The color index at
the pixel ( 41, 42 )
is ( 12698074 )D = ( C1C1DA )H
The color index at
the pixel ( 41, 43 )
is ( 12237780 )D = ( BABBD4 )H
The color index at
the pixel ( 42, 40 )
is ( 12698073 )D = ( C1C1D9 )H
The color index at
the pixel ( 42, 41 )
is ( 12566486 )D = ( BFBFD6 )H
The color index at
the pixel ( 42, 42 )
is ( 12632534 )D = ( C0C1D6 )H
The color index at
the pixel ( 42, 43 )
is ( 12369620 )D = ( BCBED4 )H
The color index at
the pixel ( 43, 40 )
is ( 12698076 )D = ( C1C1DC )H
The color index at
the pixel ( 43, 41 )
is ( 12369622 )D = ( BCBED6 )H
The color index at
the pixel ( 43, 42 )
is ( 12697818 )D = ( C1C0DA )H
The color index at
the pixel ( 43, 43 )
is ( 12369619 )D = ( BCBED3 )H
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img04 = "png/PNG 026 03 P de Galinhas.png";
echo $tst_img04; ?><br><br>Existing truecolor Image
<br><br><img src="<?php echo $tst_img04; ?>"
alt="<?php echo $tst_img04; ?>" width="400"><br><br>
<?php
$im04 = imagecreatefrompng($tst_img04);
$get_w04 = imagesx($im04);
$get_h04 = imagesy($im04);
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_color04 = imagecolorat($im04, $x04, $y04);
echo 'The color index<br>at the pixel ( ' . $x04 . ', ' . $y04 . ' )
<br>is ( ' . dechex($ndx_color04) . ' )<sub>H</sub> = ( ' . strtoupper(dechex($ndx_color04)) . ' )<sub>H</sub><br>';
?>
RESULT PNG 026 03 P de Galinhas.png624X468 px2
This exercise displays a new result for each new run.
lax(USA).png
EXERCISE
<?php
// RUN this exercise
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$tst_img05 = "png/lax(USA).png";
echo $tst_img05; ?><br><br>Existing Image
<br><br><img src="<?php echo $tst_img05; ?>"
alt="<?php echo $tst_img05; ?>"><br><br>
<?php
$info05 = getimagesize($tst_img05);
/*
echo '<pre>';
var_dump($info05);
echo '</pre>';
*/
$im05 = imagecreatefrompng($tst_img05);
$get_w05 = imagesx($im05);
$get_h05 = imagesy($im05);
echo $get_w05 . 'X' . $get_h05 . ' px<sup>2</sup><br><br>';
for($x05=0; $x05 < $get_w05; $x05++)
for($y05=0; $y05 < $get_h05; $y05++)
{
$ndx_color05 = imagecolorat($im05, $x05, $y05);
echo 'At the pixel ( ' . $x05 . ', ' . $y05 . ' )
<br>is ( ' . dechex($ndx_color05) . ' )<sub>H</sub> = ( ' . $ndx_color05 . ' )<sub>D</sub><br><br>';
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This exercise consumes a lot of time when executed
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>