imagecolorresolvealpha
GET index of the specified color plus alpha or its closest possible alternative 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 imagecolorresolvealpha ( 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 027 03.png";
$dst_img01 = "png/PNG 027 03-acra0.png";
$idorgimg = imagecreatefrompng($org_img01);
$nbr_c_o = imagecolorstotal($idorgimg);
if($nbr_c_o == 0)
{
$en = '<br><br>TRUECOLOR IMAGE<br>';
}
else
{
$en = '<br><br>PALETTE IMAGE<br>'. $nbr_c_o .' 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
$idorgimg = imagecreatefrompng($org_img01);
$save_flag = mt_rand(0, 1);
// RANDOMIZED
$save_alpha = imagesavealpha ($idorgimg, $save_flag);
imagepng($idorgimg, $dst_img01);
?>
<?php echo $dst_img01; ?> <?php echo '<br><br>save flag = ' .
$save_flag; ?><br><br>
<img src="<?php echo $dst_img01; ?>"
alt="<?php echo $dst_img01; ?>"
title="<?php echo $dst_img01; ?>"><br><br>
<?php
$pal_tst_ndx = mt_rand(10, 88888888);
// RANDOMIZED
$pal_org = "png/PNG 027 03-acra1.png";
$col_tst_ndx = imagecolorsforindex($idorgimg, $pal_tst_ndx);
$en = 'Palette Test Index = ';
echo '' . $$lang . $pal_tst_ndx
. '<br><br><pre>';
print_r($col_tst_ndx);
echo '</pre>';
$red = $col_tst_ndx['red'];
$green = $col_tst_ndx['green'];
$blue = $col_tst_ndx['blue'];
$alpha = $col_tst_ndx['alpha'];
$idpalorg = imagecreate(imagesx($idorgimg), 40);
imagecolorallocate($idpalorg, $red, $green, $blue);
imagepng($idpalorg, $pal_org);
?>
<br>
<?php echo $pal_org;
$en = "Color: ";
?>
<br><br><img src="<?php echo $pal_org; ?>"
alt="<?php echo $pal_org; ?>"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $red . ', ' . $green . ', ' . $blue . ' )<br><br>';
$nwalpha = mt_rand(0, 30);
$pal_res_ndx = imagecolorresolvealpha($idorgimg, $red, $green, $blue, $nwalpha);
$col_res_ndx =
imagecolorsforindex($idorgimg, $pal_res_ndx);
$en = 'Palette Resolved Index = ';
echo '<br><br>' . $$lang . $pal_res_ndx
. ' ( RANDOMIZED )<br><br><pre>';
print_r($col_res_ndx);
echo '</pre>';
$nwred = $col_res_ndx['red'];
$nwgreen = $col_res_ndx['green'];
$nwblue = $col_res_ndx['blue'];
$pal_dst = "png/PNG 027 03-acra2.png";
$idpaldst = imagecreate(imagesx($idorgimg), 40);
imagecolorallocate($idpaldst, $nwred, $nwgreen, $nwblue);
imagepng($idpaldst, $pal_dst);
?>
<br><br>
<?php echo $pal_dst;
$en = "Color: ";
?>
<br><br><img src="<?php echo $pal_dst; ?>"
alt="<?php echo $pal_dst; ?>"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $nwred . ', ' . $nwgreen . ', ' . $nwblue . ' )<br><br>';
?>
RESULT PNG 027 03.pngEXISTING 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 013 02.gif";
$dst_img02 = "gif/GIF 013 02-acra0.gif";
$idorgimg = imagecreatefromgif($org_img02);
$nbr_c_o = imagecolorstotal($idorgimg);
if($nbr_c_o == 0)
{
$en = '<br><br>TRUECOLOR IMAGE<br>';
}
else
{
$en = '<br><br>PALETTE IMAGE<br>'. $nbr_c_o .' 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
$idorgimg = imagecreatefromgif($org_img02);
$save_flag = mt_rand(0, 1);
// RANDOMIZED
$save_alpha = imagesavealpha ($idorgimg, $save_flag);
imagegif($idorgimg, $dst_img02);
?>
<?php echo $dst_img02; ?> <?php echo '<br><br>save flag = ' .
$save_flag; ?><br><br>
<img src="<?php echo $dst_img02; ?>"
alt="<?php echo $dst_img02; ?>"
title="<?php echo $dst_img02; ?>"><br><br>
<?php
$pal_tst_ndx = mt_rand(10, 251);
// RANDOMIZED
$pal_org = "gif/GIF 013 02-acra1.gif";
$col_tst_ndx = imagecolorsforindex($idorgimg, $pal_tst_ndx);
$en = 'Palette Test Index = ';
echo '' . $$lang . $pal_tst_ndx
. '<br><br><pre>';
print_r($col_tst_ndx);
echo '</pre>';
$red = $col_tst_ndx['red'];
$green = $col_tst_ndx['green'];
$blue = $col_tst_ndx['blue'];
$alpha = $col_tst_ndx['alpha'];
$idpalorg = imagecreate(imagesx($idorgimg), 60);
imagecolorallocate($idpalorg, $red, $green, $blue);
imagegif($idpalorg, $pal_org);
?>
<br>
<?php echo $pal_org;
$en = "Color: ";
?>
<br><br><img src="<?php echo
$pal_org; ?>" alt="<?php echo $pal_org; ?>"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $red . ', ' . $green . ', ' . $blue . ' )<br><br>';
$nwalpha = mt_rand(0, 30);
$pal_res_ndx = imagecolorresolvealpha($idorgimg,
$red,
$green,
$blue,
$nwalpha);
$col_res_ndx = imagecolorsforindex($idorgimg, $pal_res_ndx);
$en = 'Palette Resolved Index = ';
echo '<br><br>' . $$lang . $pal_res_ndx
. ' ( RANDOMIZED )<br><br><pre>';
print_r($col_res_ndx);
echo '</pre>';
$nwred = $col_res_ndx['red'];
$nwgreen = $col_res_ndx['green'];
$nwblue = $col_res_ndx['blue'];
$pal_dst = "gif/GIF 013 02-acra2.gif";
$idpaldst = imagecreate(imagesx($idorgimg), 60);
imagecolorallocate($idpaldst, $nwred, $nwgreen, $nwblue);
imagegif($idpaldst, $pal_dst);
?>
<br><br>
<?php echo $pal_dst;
$en = "Color: ";
?>
<br><br><img src="<?php echo
$pal_dst; ?>" alt="<?php echo $pal_dst; ?>"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $nwred . ', ' . $nwgreen . ', ' . $nwblue .
' )<br><br>';
?>
RESULT GIF 013 02.gifEXISTING PALETTE IMAGE
255 colors
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_img03 = "jpeg/JPEG 020 03 Mary.jpg";
$dst_img03 = "jpeg/JPEG 020 03 Mary-acra0.jpg";
$pal_org = "jpeg/JPEG 020 03 Mary-acra1.jpg";
$pal_dst = "jpeg/JPEG 020 03 Mary-acra2.jpg";
$idorgimg = imagecreatefromjpeg($org_img03);
$nbr_c_o = imagecolorstotal($idorgimg);
if($nbr_c_o == 0)
{
$en = '<br><br>TRUECOLOR IMAGE<br>';
}
else
{
$en = '<br><br>PALETTE IMAGE<br>'. $nbr_c_o .' colors';
}
?>
<?php echo $org_img03; ?><?php echo $$lang; ?><br><br>
<img src="<?php echo $org_img03; ?>"
alt="<?php echo $org_img03; ?>"
title="<?php echo $org_img03; ?>" width="400"><br><br>
<?php
$idorgimg = imagecreatefromjpeg($org_img03);
$save_flag = mt_rand(0, 1);
// RANDOMIZED
$save_alpha = imagesavealpha ($idorgimg, $save_flag);
imagejpeg($idorgimg, $dst_img03);
?>
<?php echo $dst_img03; ?> <?php echo '<br><br>save flag = ' .
$save_flag; ?><br><br>
<img src="<?php echo $dst_img03; ?>"
alt="<?php echo $dst_img03; ?>"
title="<?php echo $dst_img03; ?>" width="400"><br><br>
<?php
$pal_tst_ndx = mt_rand(10, 251);
// RANDOMIZED
$col_tst_ndx = imagecolorsforindex($idorgimg, $pal_tst_ndx);
$en = 'Palette Test Index = ';
echo '' . $$lang . $pal_tst_ndx
. '<br><br><pre>';
print_r($col_tst_ndx);
echo '</pre>';
$red = $col_tst_ndx['red'];
$green = $col_tst_ndx['green'];
$blue = $col_tst_ndx['blue'];
$alpha = $col_tst_ndx['alpha'];
$idpalorg = imagecreate(400, 24);
imagecolorallocate($idpalorg, $red, $green, $blue);
imagejpeg($idpalorg, $pal_org);
?>
<br>
<?php echo $pal_org;
$en = "Color: ";
?>
<br><br><img src="<?php echo
$pal_org; ?>" alt="<?php echo $pal_org; ?>" width="400"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $red . ', ' . $green . ', ' . $blue . ' )<br><br>';
$nwalpha = mt_rand(0, 30);
// RANDOMIZED
$pal_res_ndx =
imagecolorresolvealpha($idorgimg, $red, $green, $blue, $nwalpha);
$col_res_ndx =
imagecolorsforindex($idorgimg, $pal_res_ndx);
$en = 'Palette Resolved Index = ';
echo '<br><br>' . $$lang . $pal_res_ndx
. ' ( RANDOMIZED )<br><br><pre>';
print_r($col_res_ndx);
echo '</pre>';
$nwred = $col_res_ndx['red'];
$nwgreen = $col_res_ndx['green'];
$nwblue = $col_res_ndx['blue'];
$idpaldst = imagecreate(400, 24);
imagecolorallocate($idpaldst, $nwred, $nwgreen, $nwblue);
imagejpeg($idpaldst, $pal_dst);
?>
<br><br>
<?php echo $pal_dst;
$en = "Color: ";
?>
<br><br><img src="<?php echo
$pal_dst; ?>" alt="<?php echo $pal_dst; ?>" width="400"><br><br>
<?php
echo '' . $$lang
. ' RGB = ( ' . $nwred . ', ' . $nwgreen . ', ' . $nwblue .
' )<br><br>';
?>
RESULT JPEG 020 03 Mary.jpg
EXISTING TRUECOLOR IMAGE
Every new execution a new result can be obtained.
DO THE TEST YOURSELF.