imagecolorset
SET the color for the specified palette index.
Determine a new color for a given index from the palette of an image.
Modify the color of one or more pixels in a given image.
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.
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
bool imagecolorset( GdImage $image,
int $color,
int $red,
int $green,
int $blue,
int $alpha = 0 )
where,
$image = The image identifier
$color = The color index obtained by imagecolorat
$red = The value of red component of the color
$green = The value of green component of the color
$blue = The value of blue component of the color
$alpha = The value of alpha component of the color
?>
$image
The image identifier.
$color
The color index in the palette obtained by imagecolorat.
$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 the color.
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$tst_img = "gif/GIF 014 01 white-red.gif";
$exact_col_img = "gif/GIF 014 01 exact color.gif";
$replcd_col_img = "gif/GIF 014 01 color to replace.gif";
$nw_img = "gif/GIF 014 01 result.gif";
$pl_red = 220;
$pl_green = 57;
$pl_blue = 29;
$rp_red = 0;
$rp_green = 0;
$rp_blue = 255;
echo $tst_img; ?><br><br>Original image
<br><br><img src="<?php echo $tst_img; ?>"
alt="<?php echo $tst_img; ?>"><br><br>
<?php
$im_paint01 = imagecreatefromgif($tst_img);
$sx = imagesx($im_paint01);
$pal_ndx = imagecolorexact($im_paint01, $pl_red, $pl_green, $pl_blue);
if ($pal_ndx == -1)
{
$im_paint01e = imagecreate($sx, 30);
imagecolorallocate($im_paint01e, $pl_red, $pl_green, $pl_blue);
imagegif($im_paint01e, $exact_col_img);
$en = 'The reported color is not part of the image!';
echo 'RGB = ( ' . $pl_red . ', ' . $pl_green
. ', ' . $pl_blue . ' )<br><br>' . $$lang . '<br><br>';
echo $exact_col_img . '<br>';
echo '<br><img src="' . $exact_col_img
. '" alt="' . $exact_col_img . '"' . '>';
exit();
$en = 'Index = ' . $ndx_exc_pal;
echo '<br><br>' . $$lang;
}
else
{
$im_paint01e = imagecreate($sx, 30);
imagecolorallocate($im_paint01e, $pl_red, $pl_green, $pl_blue);
imagegif($im_paint01e, $exact_col_img);
$en = 'Existing color';
echo '<br>RGB = ( ' . $pl_red . ', ' . $pl_green
. ', ' . $pl_blue . ' ) ( ' . $$lang .
' )<br><br><br>';
echo $exact_col_img;
$en = 'Index = ' . $pal_ndx;
echo '<br>' . $$lang . '<br>';
echo '<br><img src="' . $exact_col_img
. '" alt="' . $exact_col_img . '"' . '><br><br><br><br>';
$im_paint01n = imagecreate($sx, 30);
imagecolorallocate($im_paint01n, $rp_red, $rp_green, $rp_blue);
imagegif($im_paint01n, $replcd_col_img);
$en = 'Color Replacement';
echo 'RGB = ( ' . $rp_red . ', ' . $rp_green
. ', ' . $rp_blue . ' ) ( ' . $$lang .
' ) <br><br><br>';
echo $replcd_col_img . '<br><br>';
echo '<img src="' . $replcd_col_img
. '" alt="' . $replcd_col_img . '"' . '><br><br><br><br>';
imagecolorset($im_paint01, $pal_ndx, $rp_red, $rp_green, $rp_blue);
imagegif($im_paint01, $nw_img);
$en = "New Image";
?>
<?php echo $nw_img; ?> ( <?php echo $$lang; ?> )
<br><br><img src="<?php echo $nw_img; ?>"
alt="<?php echo $nw_img; ?>">
<?php
}
?>
RESULT GIF 014 01 white-red.gifGIF 014 01 exact color.gifRGB = ( 220, 57, 29 )
Existing color palette
Palette Index = 0GIF 014 01 exact color.gifRGB = ( 0, 0, 255 )
Substitute ColorGIF 014 01 result.gifNew Image
EXERCISE
<?php
echo 'PHP VERSION: ' . PHP_VERSION . '<br><br>';
$lang = 'en';
$tst_img = "bmp/BMP 005 02 P de Galinhas.bmp";
$exact_col_img = "bmp/BMP 005 02 P de Galinhas-to be replaced.bmp";
$replcd_col_img = "bmp/BMP 005 02 P de Galinhas-replaced.bmp";
$nw_img = "bmp/BMP 005 02 P de Galinhas-result.bmp";
$pl_red = mt_rand(240, 243);
$pl_green = 1;
$pl_blue = 87;
$rp_red = 255;
$rp_green = 255;
$rp_blue = 255;
echo $tst_img; ?><br><br>Original image
<br><br><img src="<?php echo $tst_img; ?>"
alt="<?php echo $tst_img; ?>" width="400"><br><br>
<?php
$im_paint02 = imagecreatefrombmp($tst_img);
$sx = imagesx($im_paint02);
$pal_ndx = imagecolorexact($im_paint02, $pl_red, $pl_green, $pl_blue);
if ($pal_ndx == -1)
{
$im_paint02e = imagecreate($sx, 30);
imagecolorallocate($im_paint02e, $pl_red, $pl_green, $pl_blue);
imagebmp($im_paint02e, $exact_col_img);
$en = 'The reported color is not part of the image!';
echo 'RGB = ( ' . $pl_red . ', ' . $pl_green
. ', ' . $pl_blue . ' )<br><br>' . $$lang . '<br><br>';
echo $exact_col_img . '<br>';
echo '<br><img src="' . $exact_col_img
. '" alt="' . $exact_col_img . '"' . ' width="400">';
exit();
$en = 'Index = ' . $ndx_exc_pal;
echo '<br><br>' . $$lang;
}
else
{
$im_paint02e = imagecreate($sx, 30);
imagecolorallocate($im_paint02e, $pl_red, $pl_green, $pl_blue);
imagebmp($im_paint02e, $exact_col_img);
$en = 'Existing color';
echo '<br><br>RGB = ( ' . $pl_red . ', ' . $pl_green
. ', ' . $pl_blue . ' ) ( ' . $$lang .
' )<br><br><br>';
echo $exact_col_img;
$en = 'Index = ' . $pal_ndx;
echo '<br>' . $$lang . '<br>';
echo '<br><img src="' . $exact_col_img
. '" alt="' . $exact_col_img . '"' . ' width="400"><br><br><br><br>';
$im_paint02n = imagecreate($sx, 30);
imagecolorallocate($im_paint02n, $rp_red, $rp_green, $rp_blue);
imagebmp($im_paint02n, $replcd_col_img);
$en = 'Color Replacement';
echo 'RGB = ( ' . $rp_red . ', ' . $rp_green
. ', ' . $rp_blue . ' ) ( ' . $$lang .
' ) <br><br><br>';
echo $replcd_col_img . '<br><br>';
echo '<img src="' . $replcd_col_img
. '" alt="' . $replcd_col_img . '"' . ' width="400"><br><br><br><br>';
imagecolorset($im_paint02, $pal_ndx, $rp_red, $rp_green, $rp_blue);
imagebmp($im_paint02, $nw_img);
$en = "New Image";
?>
<?php echo $nw_img; ?> ( <?php echo $$lang; ?> )
<br><br><img src="<?php echo $nw_img; ?>"
alt="<?php echo $nw_img; ?>" width="400">
<?php
}
?>
RESULT BMP 005 02 P de Galinhas.bmpOriginal image
RGB = ( 240, 1, 87 ) ( Existing color )
Index = 15728983
BMP 005 02 P de Galinhas-to be replaced.bmpRGB = ( 255, 255, 255 ) ( Color Replacement )
BMP 005 02 P de Galinhas-replaced.bmp
BMP 005 02 P de Galinhas-result.bmp ( New Image )
EXERCISE
<?php
$lang = 'en';
$tst_img = "png/PNG 032 03 Fátima.png";
$exact_col_img = "png/PNG 032 03 Fátima.png-to be replaced.png";
$replcd_col_img = "png/PNG 032 03 Fátima.png-replaced.png";
$nw_img = "png/PNG 032 03 Fátima.png-result.png";
$pl_red = 192;
$pl_green = 218;
$pl_blue = 238;
$pl_alpha = 38;
$rp_red = 255;
$rp_green = 255;
$rp_blue = 255;
$rp_alpha = 38;
echo $tst_img; ?><br><br>Original image
<br><br><img src="<?php echo $tst_img; ?>"
alt="<?php echo $tst_img; ?>" width="400"><br><br>
<?php
$im_paint03 = imagecreatefrompng($tst_img);
$sx = imagesx($im_paint03);
$pal_ndx = imagecolorexact($im_paint03, $pl_red, $pl_green, $pl_blue);
if ($pal_ndx == -1)
{
$im_paint03e = imagecreate($sx, 30);
imagecolorallocate($im_paint03e, $pl_red, $pl_green, $pl_blue);
imagepng($im_paint03e, $exact_col_img);
$en = 'The reported color is not part of the image!';
echo 'RGB = ( ' . $pl_red . ', ' . $pl_green
. ', ' . $pl_blue . ' )<br><br>' . $$lang . '<br><br>';
echo $exact_col_img . '<br>';
echo '<br><img src="' . $exact_col_img
. '" alt="' . $exact_col_img . '"' . ' Width="400">';
exit();
$en = 'Index = ' . $ndx_exc_pal;
echo '<br><br>' . $$lang;
}
else
{
$im_paint03e = imagecreate($sx, 30);
imagecolorallocate($im_paint03e, $pl_red, $pl_green, $pl_blue);
imagepng($im_paint03e, $exact_col_img);
$en = 'Existing color';
echo '<br><br>RGB = ( ' . $pl_red . ', ' . $pl_green
. ', ' . $pl_blue . ' ) ( ' . $$lang .
' )<br><br><br>';
echo $exact_col_img;
$en = 'Index = ' . $pal_ndx;
echo '<br>' . $$lang . '<br>';
echo '<br><img src="' . $exact_col_img
. '" alt="' . $exact_col_img . '"' . ' width="400" height="40"><br><br><br><br>';
$im_paint03n = imagecreate($sx, 30);
imagecolorallocate($im_paint03n, $rp_red, $rp_green, $rp_blue);
imagepng($im_paint03n, $replcd_col_img);
$en = 'Color Replacement';
echo 'RGB = ( ' . $rp_red . ', ' . $rp_green
. ', ' . $rp_blue . ' ) ( ' . $$lang .
' ) <br><br><br>';
echo $replcd_col_img . '<br><br>';
echo '<img src="' . $replcd_col_img
. '" alt="' . $replcd_col_img . '"' . ' width="400" height="40"><br><br><br><br>';
imagecolorset($im_paint03, $pal_ndx, $rp_red, $rp_green, $rp_blue, $rp_alpha);
imagepng($im_paint03, $nw_img);
$en = "New Image";
?>
<?php echo $nw_img; ?> ( <?php echo $$lang; ?> )
<br><br><img src="<?php echo $nw_img; ?>"
alt="<?php echo $nw_img; ?>" width="400">
<?php
}
?>
RESULT PNG 032 03 Fátima.pngOriginal image
RGB = ( 192, 218, 238 ) ( Existing color )
Index = 12638958
PNG 032 03 Fátima.png-to be replaced.pngRGB = ( 255, 255, 255 ) ( Color Replacement )
PNG 032 03 Fátima.png-replaced.png
PNG 032 03 Fátima.png-result.png ( New Image )