imagecolorexact 


gd apg

GET the index for a specific 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 in the palette of the image.

If the image was created from a file, only colors used in the image are resolved.

Colors present only in the palette are not resolved.

This function returns the index of the specified color in the palette, or -1 if the color does not exist.


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

int imagecolorexact
GdImage $image,
                                           
int $red,
                                           
int $green,
                                           
int $blue )

where,

$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 

?>
 

  $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.



  1 EXERCISE   

<?php

// RUN this code several times

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$org_file01 "png/PNG 027 03.png";

$pal_file01 "png/PNG 027 03(p1).png"

echo 
$org_file01?>
<br><br><img src="<?php echo $org_file01?>
alt="<?php echo $org_file01?>">

<?php

$red01 
mt_rand(0255);
$green01 mt_rand(0255);
$blue01 mt_rand(0255);

$im01 imagecreatefrompng($org_file01);

$ndx_colr01 imagecolorexact($im01$red01$green01,  $blue01);

if(
$ndx_colr01 == -1)
{
   echo 
'The reported color is not part 
                         of the image!'
;
}    
else
{

echo 
'<br><br>The reported color is part 
                         of the image!<br><br>'
;

$colrs01 imagecolorsforindex($im01$ndx_colr01); 

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><br>';

$rsr02p imagecreate(imagesx($im01), 60);
imagecolorallocate($rsr02p$colrs01['red'], 
                                 
$colrs01['green'], 
                                 
$colrs01['blue']);

imagepng($rsr02p$pal_file01);

echo 
$pal_file01?>
<br><br><img src="<?php echo $pal_file01?>
alt="<?php echo $pal_file01?>">

<?php
}

?> 

 RESULT   

PNG 027 03.png

PNG 027 03.png apr

The reported color is part of the image palette!

red => 166 = 0xA6
green => 25 = 0x19
blue => 156 = 0x9C


PNG 027 03(p1).png

PNG 027 03(p1).png apr

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.

This exercise always returns existing colors in the image file because it is based on the color indexes then existing.


  2 EXERCISE   

<?php

// RUN this code several times

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$org_file02 =  "gif/GIF 013 02.gif";

$pal_file02p "gif/GIF 013 02(p1).gif";

$pal_file02n "gif/GIF 013 02(p2).gif";

echo 
$org_file02?>
<br><br><img src="<?php echo $org_file02?>
alt="<?php echo $org_file02?>">

<?php

$red02 
48;
$green02 mt_rand(4849);
$blue02 45;

$im02 imagecreatefromgif($org_file02);

$ndx_colr02 imagecolorexact($im02$red02$green02$blue02);

if(
$ndx_colr02 == -1)
{
   echo 
'<br><br>The reported color is not part
                     of the image!<br><br>'
;
   echo 
'red => ' $red02 ' = 0x' 
   
strtoupper(dechex($red02)) . '<br>';
   echo 
'green => ' $green02' = 0x' 
   
strtoupper(dechex($green02)) . '<br>'
   echo 
'blue => ' $blue02 ' = 0x' 
   
strtoupper(dechex($blue02)) . '<br><br>';
   
   
$im02n imagecreate(imagesx($im02), 60);
   
imagecolorallocate($im02n$red02$green02$blue02);

   
imagegif($im02n$pal_file02n);

   echo 
$pal_file02n?><br><br>
   <img src="<?php echo $pal_file02n?>
   alt="<?php echo $pal_file02n?>">

<?php 
}    
else
{

echo 
'<br><br>The reported  color is part
                     of the image!<br><br>'
;

$colrs02 imagecolorsforindex($im02$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><br>';

$im02p imagecreate(imagesx($im02), 60);
imagecolorallocate($im02p$red02$green02$blue02);

imagegif($im02p$pal_file02p);

echo 
$pal_file02p?>
<br><br><img src="<?php echo $pal_file02p?>
alt="<?php echo $pal_file02p?>">

<?php
}

?> 

 RESULT   

GIF 013 02.gif

GIF 013 02.gif apr

The reported color is part of the image palette!

red => 48 = 0x30
green => 49 = 0x31
blue => 45 = 0x2D


GIF 013 02(p1).gif

GIF 013 02(p1).gif apr

  3 EXERCISE   

<?php

// RUN this code several times

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$org_file03 "jpeg/JPEG 014 01 Belém (tga2jpeg).jpg";

$pal_file03 "jpeg/JPEG 014 01 Belém (tga2jpeg)(p1).jpg"

echo 
$org_file03?>
<br><br><img src="<?php echo $org_file03?>
alt="<?php echo $org_file03?>" width="400">

<?php

$red03 
mt_rand(0255);
$green03 mt_rand(0255);
$blue03 mt_rand(0255);

$im03 imagecreatefromjpeg($org_file03);

$ndx_colr03 imagecolorexact($im03$red03$green03$blue03);

if(
$ndx_colr03 == -1)
{
   echo 
'The reported color is not part of the image!';
}    
else
{

echo 
'<br><br>Index of color = ' $ndx_colr03 '<br><br>';

$colrs03 imagecolorsforindex($im03$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><br>'

$im03p imagecreate(imagesx($im03), 60);
imagecolorallocate($im03p$red03$green03$blue03);

imagejpeg($im03p$pal_file03);

echo 
$pal_file03?>
<br><br><img src="<?php echo $pal_file03?>
alt="<?php echo $pal_file03?>" width="400">

<?php
}

?> 

 RESULT   

JPEG 014 01 Belém (tga2jpeg).jpg

JPEG 014 01 Belém (tga2jpeg).jpg apr

Index of color = 12524956

red => 191 = 0xBF
green => 29 = 0x1D
blue => 156 = 0x9C


JPEG 014 01 Belém (tga2jpeg)(p1).jpg

JPEG 014 01 Belém (tga2jpeg)(p1).jpg apr

This is a particular result.

Every new execution a new result can be obtained.

DO THE TEST YOURSELF.


  4 EXERCISE   

<?php

// RUN this code several times

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$org_file04 "png/PNG 028 04.png";

$pal_file04 "png/PNG 028 04(p1).png"

echo 
$org_file04?>
<br><br><img src="<?php echo $org_file04?>
alt="<?php echo $org_file04?>" width="400">

<?php

$red04 
mt_rand(0255);
$green04 mt_rand(0255);
$blue04 mt_rand(0255);

$im04 imagecreatefrompng($org_file04);

$ndx_colr04 imagecolorexact($im04$red04$green04$blue04);

if(
$ndx_colr04 == -1)
{
   echo 
'The reported color is not part 
                         of the image!'
;
}    
else
{

echo 
'<br><br>The reported color is part 
                         of the image!<br><br>'
;

$colrs04 imagecolorsforindex($im04$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><br>';


$im04p imagecreate(imagesx($im04), 60);
imagecolorallocate($im04p$red04$green04$blue04);

imagepng($im04p$pal_file04);

echo 
$pal_file04?>
<br><br><img src="<?php echo $pal_file04?>
alt="<?php echo $pal_file04?>" width="400">

<?php
}

?> 

 RESULT   

PNG 028 04.png

PNG 028 04.png apr

The reported color is part of the image palette!

red => 247 = 0xF7
green => 3 = 0x3
blue => 3 = 0x3


PNG 028 04(p1).png

PNG 028 04(p1).png apr

This is a particular result.

Every new execution a new result can be obtained.

DO THE TEST YOURSELF.


  5 EXERCISE   

<?php

// RUN this code several times

echo 'PHP VERSION: ' PHP_VERSION '<br><br>';

$org_file05 "png/PNG 031 05 Fátima.png";

$pal_file05 "png/PNG 031 05 Fátima(p).png"

echo 
$org_file05?>
<br><br><img src="<?php echo $org_file05?>
alt="<?php echo $org_file05?>" width="400">

<?php

$red05 
mt_rand(0255);
$green05 mt_rand(0255);
$blue05 mt_rand(0255);

$im05 imagecreatefrompng($org_file05);

$ndx_colr05 imagecolorexact($im05$red05$green05$blue05);

if(
$ndx_colr05 == -1)
{
   echo 
'The reported color is not part 
                         of the image!'
;
}    
else
{

echo 
'<br><br>The reported color is part 
                         of the image!<br><br>'
;

$colrs05 imagecolorsforindex($im05$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><br>';


$im05p imagecreate(imagesx($im05), 60);
imagecolorallocate($im05p$red05$green05$blue05);

imagepng($im05p$pal_file05);

echo 
$pal_file05?>
<br><br><img src="<?php echo $pal_file05?>
alt="<?php echo $pal_file05?>" width="400" height="40">

<?php
}

?> 

 RESULT   

PNG 031 05 Fátima.png

PNG 031 05 Fátima.png apr

The reported color is part of the image palette!

red => 255 = 0xFF
green => 29 = 0x1D
blue => 95 = 0x5F
alpha => 0 = 0x0


PNG 031 05 Fátima(p)

PNG 031 05 Fátima(p).png apr

This is a particular result.

Every new execution a new result can be obtained.

DO THE TEST YOURSELF.