PHP Tutorial - PHP imagepolygon() function






imagepolygon() creates a polygon in the given image.

Syntax

PHP imagepolygon() function has the following syntax.

bool imagepolygon ( resource $image , array $points , int $num_points , int $color )

Parameter

PHP imagepolygon() function has the following parameters.

  • image - An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().
  • points - An array containing the polygon's vertices.
  • num_points - Total number of points (vertices).
  • color - A color identifier created with imagecolorallocate().




Point Format

Take a look at the following code:

$myPoints = array( 20, 20, 
                   185, 55, 
                   70, 80 ); 
imagepolygon( $myImage, $myPoints, 3, $myBlack );  

This example creates an array of the polygon's points, called $myPoints. There are six elements in the array, or three x/y coordinate pairs.

This means that there are three corners to this polygon: at (20,20), (185,55), and (70,80).

Return

Returns TRUE on success or FALSE on failure.





Example


<?php//from   ww  w  .j  a  v  a  2  s.  c  o  m
// Create a blank image
$image = imagecreatetruecolor(400, 300);

// Allocate a color for the polygon
$col_poly = imagecolorallocate($image, 255, 255, 255);

// Draw the polygon
imagepolygon($image, array(
        0,   0,
        100, 200,
        300, 200
    ),
    3,
    $col_poly);

// Output the picture to the browser
header('Content-type: image/png');

imagepng($image);
imagedestroy($image);
?>