Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

PHP | imagecolorclosestalpha() Function

  • Last Updated : 10 Sep, 2021

The imagecolorclosestalpha() function is an inbuilt function in PHP which is used to get the index of closest 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:  

PHP




<?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);
?>

Output: 

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

Program 2:  

PHP




<?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);
?>

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
Recommended Articles
Page :

Start Your Coding Journey Now!