Open In App

PHP | imagefilledpolygon() Function

Last Updated : 03 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 the polygon.
  • $num_points: This parameter contains the total number of vertices in a polygon. It must be greater than 3 because a minimum of three vertices is required to create a polygon.
  • $color: This variable contains the filled color identifier. A color identifier was created with imagecolorallocate() function.

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

The below programs illustrate the imagefilledpolygon() function in PHP.

Program 1: 

php




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




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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads