How to draw a filled polygon using an imagefilledpolygon() function in PHP?

The imagefilledpolygon() function is a built-in PHP function used to draw a filled polygon on an image. This function requires the GD extension to be enabled and creates solid-filled geometric shapes with multiple vertices.

Syntax

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

Parameters

The imagefilledpolygon() function takes four parameters −

  • $image − An image resource created using imagecreatetruecolor() or similar functions.

  • $points − An array containing the sequential x,y coordinates of polygon vertices in pairs.

  • $num_points − The total number of vertices in the polygon. Must be at least 3 to form a valid polygon.

  • $color − A color identifier created using imagecolorallocate() function.

Return Value

Returns true on success and false on failure.

Installation: This function requires the GD extension. Install it using: sudo apt-get install php-gd (Linux) or enable it in php.ini on Windows.

Example 1: Drawing a Complex Polygon

This example creates a complex polygon with 6 vertices using blue color −

<?php
    // Set up array of points for a polygon
    $values = array(
        40, 50,   // Point 1 (x, y)
        20, 240,  // Point 2 (x, y)
        60, 60,   // Point 3 (x, y)
        240, 20,  // Point 4 (x, y)
        50, 40,   // Point 5 (x, y)
        10, 10    // Point 6 (x, y)
    );
    
    // Create the image using imagecreatetruecolor function
    $img = imagecreatetruecolor(700, 350);

    // Allocate the blue and gray colors
    $bg = imagecolorallocate($img, 122, 122, 122);
    $blue = imagecolorallocate($img, 0, 0, 255);

    // Fill the background
    imagefilledrectangle($img, 0, 0, 350, 350, $bg);

    // Draw a filled polygon
    imagefilledpolygon($img, $values, 6, $blue);

    // Output image
    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
?>

Example 2: Drawing a Pentagon

This example creates a regular pentagon shape filled with red color −

<?php
    // Set the vertices of the pentagon
    $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)
    );
    
    // Create the image canvas
    $img = imagecreatetruecolor(700, 350);

    // Set the gray background image color
    $bg = imagecolorallocate($img, 122, 122, 122);

    // Set the red fill color
    $red = imagecolorallocate($img, 255, 0, 0);

    // Fill the background
    imagefilledrectangle($img, 0, 0, 350, 350, $bg);

    // Draw the filled pentagon
    imagefilledpolygon($img, $values, 5, $red);

    // Output the image
    header('Content-type: image/png');
    imagepng($img);
    imagedestroy($img);
?>

Key Points

  • The points array must contain x,y coordinate pairs in sequence

  • Minimum 3 points required to form a polygon

  • Always call imagedestroy() to free memory after image processing

  • Use imagepolygon() for outline-only polygons without fill

Conclusion

The imagefilledpolygon() function provides an efficient way to create filled geometric shapes in PHP. It's particularly useful for generating dynamic graphics, charts, and geometric illustrations in web applications.

Updated on: 2026-03-15T09:46:50+05:30

478 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements