PHP | imagecolorexactalpha() Function

The imagecolorexactalpha() function is an inbuilt function in PHP which is used to get the index of the specified color with alpha value. This function returns the index of the specified color and alpha value (RGBA value) in the palette of the image.

Syntax:

int imagecolorexactalpha ( $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 specified color and alpha in the palette of the image on success, or -1 if the color does not exist in the image’s palette.

Below programs illustrate the imagecolorexactalpha() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Setup an image
$image = imagecreatefrompng(
  
$colors   = Array();
$colors[] = imagecolorexactalpha($image, 255, 0, 255, 0);
$colors[] = imagecolorexactalpha($image, 0, 225, 146, 127);
$colors[] = imagecolorexactalpha($image, 255, 56, 255, 55);
$colors[] = imagecolorexactalpha($image, 100, 55, 252, 20);
  
print_r($colors);
  
// Free from memory
imagedestroy($image);
?>

chevron_right


Output:

Array ( 
    [0] => 16711935 
    [1] => 2130764178 
    [2] => 939473151 
    [3] => 342112252 
) 

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Setup an image
$image = imagecreatefrompng(
   
$colors   = Array(
    imagecolorexactalpha($image, 255, 120, 255, 45),
    imagecolorexactalpha($image, 160, 25, 146, 127),
    imagecolorexactalpha($image, 255, 56, 55, 155),
    imagecolorexactalpha($image, 190, 155, 252, 200)
);
print_r($colors);
   
// Free from memory
imagedestroy($image);
?>

chevron_right


Output:

Array ( 
    [0] => 771717375 
    [1] => 2141198738 
    [2] => -1677772745 
    [3] => -927032324 
) 

Related Articles:

Reference: http://php.net/manual/en/function.imagecolorexactalpha.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.