PHP | imagecolorallocatealpha() Function

The imagecolorallocatealpha() function is an inbuilt function in PHP which is used to allocate the color for an image. This function is same as imagecolorallocate() function with addition of transparency parameter $alpha. This function accepts five parameters and returns color identifier on true or false on failure.

Syntax:

int imagecolorallocatealpha ( $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 a color identifier on success or FALSE if the color allocation failed.

Exception: 5.1.3 or later version return false if allocation failed otherwise return -1 previously.



Below programs illustrate the imagecolorallocatealpha() function in PHP:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// It create the size of image or blank image.
$image = imagecreatetruecolor(500, 300);
  
// Set the background color of image.
$bg = imagecolorallocate($image, 0, 103, 0);
   
// Fill background with above selected color.
imagefill($image, 0, 0, $bg); 
  
// allocate colors with alpha values
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75);
$red    = imagecolorallocatealpha($image, 255, 0, 0, 75);
$blue   = imagecolorallocatealpha($image, 0, 0, 255, 75);
  
// Drawing filled circle
imagefilledellipse($image, 200, 100, 150, 150, $yellow);
imagefilledellipse($image, 275, 100, 150, 150, $red);
imagefilledellipse($image, 240, 180, 150, 150, $blue);
  
//output a correct header!
header('Content-Type: image/png');
  
//output the result
imagepng($image);
imagedestroy($image);
?>

chevron_right


Output:
opacity image

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// It create the size of image or blank image.
$image = imagecreatetruecolor(500, 300);
  
// Set the background color of image.
$bg = imagecolorallocate($image, 0, 103, 0);
   
// Fill background with above selected color.
imagefill($image, 0, 0, $bg); 
  
// allocate colors with alpha values
$yellow = imagecolorallocatealpha($image, 255, 255, 0, 75);
$blue   = imagecolorallocatealpha($image, 0, 0, 255, 75);
  
// Drawing filled circle
imagefilledellipse($image, 200, 150, 150, 150, $yellow);
imagefilledellipse($image, 280, 150, 150, 150, $blue);
  
//output a correct header!
header('Content-Type: image/png');
  
//output the result
imagepng($image);
imagedestroy($image);
?>

chevron_right


Output:
transparency

Related Articles:

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