Open In App

PHP | imagecolorexactalpha() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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:




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


Output:

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

Program 2:




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


Output:

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

Related Articles:

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



Last Updated : 06 Sep, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads