
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
How to draw a filled polygon using an imagefilledpolygon() function in PHP?
imagefilledpolygon() is an inbuilt PHP function that is used to draw a filled polygon.
Syntax
bool imagefilledpolygon($image, $points, $num_points, $color)
Parameters
imagefilledpolygon() takes four different parameters − $image, $points, $num_points, and $color.
$image − Creates a blank image in a given size using the imagecreatetruecolor() function.
$points − Holds the sequential vertices of the polygon.
$num_points − Contains the total number of vertices in a polygon. The total number of points/vertices must be at least three to create a polygon.
$color − Contains the filled color identifier using imagecolorallocate() function.
Return Values
It returns True on success and False on failure.
Example 1
<?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); // allocated the blue and gray colors $bg = imagecolorallocate($img, 122, 122, 122); $blue = imagecolorallocate($img, 0, 0, 255); // filled the background imagefilledrectangle($img, 0, 0, 350, 350, $bg); // draw a polygon imagefilledpolygon($img, $values, 6, $blue); // flush image header('Content-type: image/png'); imagepng($img); imagedestroy($img); ?>
Output
Example 2
<?php // Set the vertices of the 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 creates the size of the image or blank image. $img = imagecreatetruecolor(700, 350); // Set the gray background image color $bg = imagecolorallocate($img, 122, 122, 122); // Set the red image color $red = imagecolorallocate($img, 255, 0, 0); // fill the background imagefilledrectangle($img, 0, 0, 350, 350, $bg); // Draw the polygon image imagefilledpolygon($img, $values, 5, $red); // Output of the image. header('Content-type: image/png'); imagepng($img); imagedestroy($img); ?>
Output
- Related Articles
- Draw a filled polygon using the OpenCV function fillPoly()
- How to draw an open polygon using the imageopenpolygon() function n PHP?
- How to draw a filled polygon in OpenCV using Java?
- How to draw an ellipse using imageellipse() function in PHP?
- How to draw a line using imageline() function in PHP?
- How to draw a string vertically using imagestringup() function in PHP?
- How to draw a polygon in OpenCV using Java?
- How to draw an octagon with Polygon object using FabricJS?
- How to draw a filled circle in OpenCV using Java?
- How to draw a filled ellipse in OpenCV using Java?
- How to draw a hexagon with Polygon using FabricJS?
- How to draw filled ellipses in OpenCV using Python?
- How to draw a partial arc and fill it using imagefilledarc() function in PHP?
- How to draw a text string image horizontally by using imagestring() function in PHP?
- How to draw a rectangle with Polygon object using FabricJS?

Advertisements