Friday, December 27, 2024
Google search engine
HomeLanguagesPHP | imagecolorallocatealpha() Function

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:




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


Output:
opacity image

Program 2:




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


Output:
transparency

Related Articles:

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

RELATED ARTICLES

Most Popular

Recent Comments