Computer >> Computer tutorials >  >> Programming >> PHP

imagecolorallocate() function in PHP


The imagecolorallocate() function allocates a color for an image.

Syntax

int imagecolorallocate ( $img, $red, $green, $blue )

Parameters

  • img: Image resource created with imagecreatetruecolor().

  • red: Red color component

  • green: Green color component

  • blue: Blue color component

Return

The imagecolorallocate() function returns a color in RGB format.

Example

The following is an example:

<?php
   $img = imagecreatetruecolor(600, 220);
   $bgcolor = imagecolorallocate($img, 20, 50, 120);
   imagefill($img, 0, 0, $bgcolor);
   header("Content-type: image/png");
   imagepng($img);
   imagedestroy($img);
?>

Output

The following is the output:

imagecolorallocate() function in PHP