PHP | imagefilledpolygon() Function

The imagefilledpolygon() function is an inbuilt function in PHP which is used to draw a filled polygon. This function Returns TRUE on success and returns FALSE otherwise.

Syntax:

bool imagefilledpolygon( $image, $points, $num_points, $color )

Parameters: This function accepts four parameters as mentioned above and described below:

  • $image: The imagecreatetruecolor() function is used to create a blank image in a given size.
  • $points: This parameter is used to hold the consecutive vertices of polygon.
  • $num_points: This parameter contains total number of vertices in a polgon. It must be greater then 3, because minimum three vertices required to create a polygon.
  • $color: This variable contains the filled color identifier. A color identifier created with imagecolorallocate() function.

Return Value: This function returns TRUE on success or FALSE on failure.

Below programs illustrate the imagefilledpolygon() function in PHP.



Program 1:

<?php

// Set the vertices of polygon
$values = array(
            150,  50, // Point 1 (x, y)
            50, 250,  // Point 2 (x, y)
            250,  250 // Point 3 (x, y)
        );
 
// It create the size of image or blank image.
$image = imagecreatetruecolor(300, 300);
 
// Set image background color
$bg   = imagecolorallocate($image, 255, 255, 255);

// Set image color
$gr = imagecolorallocate($image, 0, 153, 0);
 
// fill the background
imagefilledrectangle($image, 0, 0, 300, 300, $bg);
 
// Draw the polygon
imagefilledpolygon($image, $values, 3, $gr);
 
// Output of the image.
header('Content-type: image/png');
imagepng($image);
?>

Output:
Imagefilledcolor function

Program 2:

<?php

// Set the vertices of polygon
$values = array(
            150, 50, // Point 1 (x, y)
            55, 119, // Point 2 (x, y)
            91, 231, // Point 3 (x, y)
            209, 231, // Point 4 (x, y)
            245, 119  // Point 5 (x, y)
            );
 
// It create the size of image or blank image.
$image = imagecreatetruecolor(300, 300);
 
// Set image background color
$bg   = imagecolorallocate($image, 255, 255, 255);

// Set image color
$blue = imagecolorallocate($image, 0, 153, 0);
 
// fill the background
imagefilledrectangle($image, 0, 0, 300, 300, $bg);
 
// Draw the polygon
imagefilledpolygon($image, $values, 5, $blue);
 
// Output of the image.
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>

Output:
imagefilledcolor

Related Articles:

Reference: http://php.net/manual/en/function.imagefilledpolygon.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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Article Tags :
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.






User Actions