PHP Imagecolorallocate() Function

Imagecolorallocate( ) function is another inbuilt PHP function mainly used to implement a new color to an image. It returns the color of an image in an RGB format (RED GREEN BLUE)

Syntax

Parameters: This function accepts four parameters.

S.NoParameterDescriptionOptional / mandatory
1$ imageThis parameter is used to define the size of the image we want to display. This parameter is used by an image resource like the imagecreatetruecolor ( ) function or imagecreate( ) function, which returns an image source.Mandatory
2$ redThis parameter is used to define the value of the red color component of the imageMandatory
3$ greenThis parameter is used to define the value of the green color component of the imageMandatory
4$ blueThis parameter is used to define the value of the blue color component of the image.Mandatory

The imagecolorallocate( ) function returns the identifier of the new color on successful execution of the program. If the color of the image is not specified or does not contain any color, it will return a false on a failed attempt.

Program 1: PHP program to display the basic use of imagecolorallocate( ) function

Output

The above code gives the following output.

PHP Imagecolorallocate() Function

Here in this program, we have declared various variables like $ image to define the size of the image that we require $background color to define the color of background we require. We have used the image fill ( ) function to set the declared color to the image's background. To display the output of the image, we have used an inbuilt PHP command header and imagepng to display on the browser.

Program 2: PHP program to display the basic use of imagecolorallocate ( ) with imagestring ( ) function

Output

The above program gives this output.

PHP Imagecolorallocate() Function

Here in this program, we have declared various variables like $ image to define the size of the image that we require, $background color to define the color of background we require, $text color to define the color of text we require. We have used the image string ( ) function to declare the string we want to display as an image. To display the output of the image, we have used an inbuilt PHP command header and imagepng to display on the browser.

Program 3: PHP program to display the basic use of imagecolorallocate ( ) with imagefilledrectangel ( ) function

Output

PHP Imagecolorallocate() Function

Here in this program, we have declared various variables like $ image to define the size of the image that we require, $background color to define the background we require, $background - color - 2 to define the other color of background we require. We have used the imagefilledrectangle ( ) function to create a green rectangle. To display the output of the image, we have used an inbuilt PHP command header and imagepng to display on the browser.

Program 4: PHP program to display the use of imagecolorallocate ( ) function to draw a polygon using imagecreatetruecolor ( ) function

Output

PHP Imagecolorallocate() Function

Here in this program, we have declared various variables like $ image to define the size of the image that we require, $background color to define the color of background we require, $text color to define the color of text we require, an array $values to set the coordinates of the polygon we need to declare, and we have used image polygon ( ) function to display the polygon we want to display as an image, to display the output of the image we have used an inbuilt PHP command header and imagepng to display on browser.

Program 4: PHP program to display the use of imagecolorallocate ( ) function to draw a polygon.

Output

PHP Imagecolorallocate() Function

Here in this program, we have declared various variables like $ image to define the size of the image that we require, $background color to define the color of background we require, $text color to define the color of text we require, an array $values to set the coordinates of the polygon we need to declare, and we have used image polygon ( ) function to display the polygon we want to display as an image, to display the output of the image we have used an inbuilt PHP command header and imagepng to display on browser.