PHP | imagecolorclosestalpha() Function

The imagecolorclosestalpha() function is an inbuilt function in PHP which is used to get the index of closet color with alpha value in the given image. This function returns the index of the color in the palette of the image which is closest to the specified RGB value and alpha level. The alpha value represents the transparency of the image.

Syntax:

int imagecolorclosestalpha ( $image, $red, $green, $blue, $alpha )

Parameters: This function accepts five parameters as mentioned above and described below:



  • $image: It is returned by one of the image creation functions, such as imagecreatetruecolor(). It is used to create size of image.
  • $red: This parameter is used to set value of red color component.
  • $green: This parameter is used to set value of green color component.
  • $blue: This parameter is used to set value of blue color component.
  • $alpha: This parameter is used to set the transparency of image. The value of $alpha lies between 0 to 127 where 0 represents completely opaque while 127 represents completely transparent.

Return Value: This function returns the index of the closest color in the palette.

Below programs illustrate the imagecolorclosestalpha() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Convert an image into a palette-based image
$image = imagecreatefrompng(
  
imagetruecolortopalette($image, false, 255);
  
// Find closest color in image
$output = imagecolorclosestalpha($image, 155, 40, 200, 50);
$output = imagecolorsforindex($image, $output);
$output = "({$output['red']}, {$output['green']},
          {$output['blue']}, {$output['alpha']})";
  
echo "Closest match: " . $output . "\n";
  
imagedestroy($image);
?>

chevron_right


Output:

Closest match: (100, 58, 108, 0)

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Convert an image into a palette-based image
$image = imagecreatefrompng(
  
imagetruecolortopalette($image, false, 255);
  
// Search the given rgb color.
$color = array(
    array(155, 40, 200, 50),
    array(235, 205, 188, 127),
    array(135, 00, 132, 0),
);
  
// Loop to return the closest color match.
foreach($color as $id => $rgb)
{
    $output = imagecolorclosestalpha($image, $rgb[0],
                          $rgb[1], $rgb[2], $rgb[3]);
                            
    $output = imagecolorsforindex($image, $output);
      
    $output = "({$output['red']}, {$output['green']}, 
              {$output['blue']}, {$output['alpha']})";
  
    echo "Given color: ($rgb[0], $rgb[1], $rgb[2], $rgb[3])
                 => Closest match: $output <br>";
}
  
imagedestroy($image);
?>

chevron_right


Output:

Given color: (155, 40, 200, 50) => Closest match: (100, 58, 108, 0) 
Given color: (235, 205, 188, 127) => Closest match: (100, 58, 108, 0) 
Given color: (135, 0, 132, 0) => Closest match: (100, 58, 108, 0) 

Related Articles:

Reference: http://php.net/manual/en/function.imagecolorclosestalpha.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.