imagecolorclosestalpha
GET the index of the closest color to the specified color plus alpha in an image file.
This function get the index of an specific pixel color $image.
This function is guaranteed to return a color index for a requested color, either the exact color or the closest possible alternative.
0 ≤ $alpha ≤ 127.
If $alpha = 0 indicates completely opaque.
If $alpha = 127 indicates completely transparent.
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 imagecolorclosestalpha ( GdImage $image,
int $red,
int $green,
int $blue,
int $alpha )
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
$alpha = The value of alpha
?>
$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.
$alpha
The value of alpha component of color.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$org_img01 = "png/PNG 037 01 Brooklin-SP(BRA).png";
$dst_img01 = "png/PNG 037 01 Brooklin-SP(BRA)-sfn0.png";
$pal_org01 = "png/PNG 037 01 Brooklin-SP(BRA)-sfn1.png";
$pal_dst01 = "png/PNG 037 01 Brooklin-SP(BRA)-sfn2.png";
$idorgimg01 = imagecreatefrompng($org_img01);
$nbr_c_o01 = imagecolorstotal($idorgimg01);
if($nbr_c_o01 == 0)
{
$en = '<br>TRUECOLOR IMAGE';
}
else
{
$en = '<br>PALETTIZED IMAGE,br>'. $nbr_c_o01 .' colors';
}
?>
<?php echo $org_img01; ?><?php echo $$lang; ?><br><br>
<img src="<?php echo $org_img01; ?>"
alt="<?php echo $org_img01; ?>"
title="<?php echo $org_img01; ?>"><br><br>
<?php
$idorgimg01 = imagecreatefrompng($org_img01);
$save_flag01 = mt_rand(0, 1);
// RANDOMIZED
$save_alpha01 =
imagesavealpha ($idorgimg01, $save_flag01);
imagepng($idorgimg01, $dst_img01);
?>
<?php echo $dst_img01 . '<br>save flag = ' .
$save_flag01; ?><br><br>
<img src="<?php echo $dst_img01; ?>"
alt="<?php echo $dst_img01; ?>"
title="<?php echo $dst_img01; ?>"><br><br>
<?php
$pal_tst_ndx01 = mt_rand(10, 88888888);
$col_tst_ndx01 =
imagecolorsforindex($idorgimg01, $pal_tst_ndx01);
$en = 'Palette Test Index = ';
echo '' . $$lang . $pal_tst_ndx01
. '<br><br><pre>';
print_r($col_tst_ndx01);
echo '</pre>';
$red01 = $col_tst_ndx01['red'];
$green01 = $col_tst_ndx01['green'];
$blue01 = $col_tst_ndx01['blue'];
$alpha01 = $col_tst_ndx01['alpha'];
$idpalorg01 = imagecreate(imagesx($idorgimg01), 30);
imagecolorallocate($idpalorg01,
$red01, $green01, $blue01);
imagepng($idpalorg01, $pal_org01);
?>
<br>
<?php echo $pal_org01;
$en = "Color: ";
?>
<br><br><img src="<?php echo
$pal_org01; ?>" alt="<?php echo $pal_org01; ?>"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $red01 . ', ' . $green01 . ', ' . $blue01 . ' )<br><br>';
$nwalpha01 = mt_rand(0, 30);
// RANDOMIZED
$pal_res_ndx01 = imagecolorclosestalpha($idorgimg01,
$red01,
$green01,
$blue01,
$nwalpha01);
$col_res_ndx01 =
imagecolorsforindex($idorgimg01, $pal_res_ndx01);
$en = 'Palette Closest Index = ';
echo '<br><br>' . $$lang . $pal_res_ndx01
. ' ( RANDOMIZED )<br><br><pre>';
print_r($col_res_ndx01);
echo '</pre>';
$nwred01 = $col_res_ndx01['red'];
$nwgreen01 = $col_res_ndx01['green'];
$nwblue01 = $col_res_ndx01['blue'];
$idpaldst = imagecreate(imagesx($idorgimg01), 30);
imagecolorallocate($idpaldst,
$nwred01,
$nwgreen01,
$nwblue01);
imagepng($idpaldst, $pal_dst01);
?>
<br><br>
<?php echo $pal_dst01;
$en = "Color: ";
?>
<br><br><img src="<?php echo
$pal_dst01; ?>" alt="<?php echo $pal_dst01; ?>"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $nwred01 . ', ' .
$nwgreen01 . ', ' .
$nwblue01 . ' )<br><br>';
?>
RESULT PNG 037 01 Brooklin-SP(BRA).png
EXISTING TRUECOLOR IMAGE
Every new execution a new result can be obtained.
DO THE TEST YOURSELF.
EXERCISE
<?php
// RUN this code several times
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$org_img02 = "gif/GIF 018 02 Brooklin-SP(BRA).gif";
$dst_img02 = "gif/GIF 018 02 Brooklin-SP(BRA)-sfn0.gif";
$pal_org02 = "gif/GIF 018 02 Brooklin-SP(BRA)-sfn1.gif";
$pal_dst02 = "gif/GIF 018 02 Brooklin-SP(BRA)-sfn2.gif";
$idorgimg02 = imagecreatefromgif($org_img02);
$nbr_c_o02 = imagecolorstotal($idorgimg02);
if($nbr_c_o02 == 0)
{
$en = '<br>TRUECOLOR IMAGE';
}
else
{
$en = '<br>PALETTIZED IMAGE<br>'. $nbr_c_o02 .' colors';
}
?>
<?php echo $org_img02; ?><?php echo $$lang; ?><br><br>
<img src="<?php echo $org_img02; ?>"
alt="<?php echo $org_img02; ?>"
title="<?php echo $org_img02; ?>"><br><br>
<?php
$idorgimg02 = imagecreatefromgif($org_img02);
$save_flag02 = mt_rand(0, 1);
// RANDOMIZED
$save_alpha02 =
imagesavealpha ($idorgimg02, $save_flag02);
imagegif($idorgimg02, $dst_img02);
?>
<?php echo $dst_img02 . '<br>save flag = ' .
$save_flag02; ?><br><br>
<img src="<?php echo $dst_img02; ?>"
alt="<?php echo $dst_img02; ?>"
title="<?php echo $dst_img02; ?>"><br><br>
<?php
$pal_tst_ndx02 = mt_rand(10, 255);
// RANDOMIZED
$col_tst_ndx02 =
imagecolorsforindex($idorgimg02, $pal_tst_ndx02);
$en = 'Palette Test Index = ';
echo '' . $$lang . $pal_tst_ndx02
. '<br><br><pre>';
print_r($col_tst_ndx02);
echo '</pre>';
$red02 = $col_tst_ndx02['red'];
$green02 = $col_tst_ndx02['green'];
$blue02 = $col_tst_ndx02['blue'];
$alpha02 = $col_tst_ndx02['alpha'];
$idpalorg02 = imagecreate(imagesx($idorgimg02), 30);
imagecolorallocate($idpalorg02, $red02,
$green02,
$blue02);
imagegif($idpalorg02, $pal_org02);
?>
<br>
<?php echo $pal_org02;
$en = "Color: ";
?>
<br><br><img src="<?php echo
$pal_org02; ?>" alt="<?php echo $pal_org02; ?>"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $red02 . ', ' . $green02 . ', ' . $blue02 . ' )<br><br>';
$nwalpha02 = mt_rand(0, 30);
$pal_res_ndx02 =
imagecolorclosestalpha($idorgimg02, $red02,
$green02,
$blue02,
$nwalpha02);
$col_res_ndx02 =
imagecolorsforindex($idorgimg02, $pal_res_ndx02);
$en = 'Palette Closest Index = ';
echo '<br><br>' . $$lang . $pal_res_ndx02
. ' ( RANDOMIZED )<br><br><pre>';
print_r($col_res_ndx02);
echo '</pre>';
$nwred02 = $col_res_ndx02['red'];
$nwgreen02 = $col_res_ndx02['green'];
$nwblue02 = $col_res_ndx02['blue'];
$idpaldst = imagecreate(imagesx($idorgimg02), 30);
imagecolorallocate($idpaldst, $nwred02,
$nwgreen02,
$nwblue02);
imagegif($idpaldst, $pal_dst02);
?>
<br><br>
<?php echo $pal_dst02;
$en = "Color: ";
?>
<br><br><img src="<?php echo
$pal_dst02; ?>" alt="<?php echo $pal_dst02; ?>"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $nwred02 . ', ' .
$nwgreen02 . ', ' .
$nwblue02 . ' )<br><br>';
?>
RESULT GIF 018 02 Brooklin-SP(BRA).gif
EXISTING PALETTIZED IMAGE
Every new execution a new result can be obtained.
DO THE TEST YOURSELF.