PHP imagepolygon() function

In this chapter you will learn:

  1. What is PHP imagepolygon() function
  2. Syntax for PHP imagepolygon() function
  3. Parameter for PHP imagepolygon() function
  4. Point Format
  5. Return value for PHP imagepolygon() function
  6. Example - PHP imagepolygon() function

Description

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   ja v a 2s .co 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);
?>

Next chapter...

What you will learn in the next chapter:

  1. What is PHP imagerectangle() function
  2. Syntax for PHP imagerectangle() function
  3. Parameter for PHP imagerectangle() function
  4. Return value for PHP imagerectangle() function
  5. Example - PHP imagerectangle() function
Home » PHP Tutorial » PHP Image Functions
PHP imagearc() function
PHP imagecreate() function
PHP imagecreatetruecolor () function
PHP imageellipse() function
PHP imagefilledarc() function
PHP imageline() function
PHP imagepolygon() function
PHP imagerectangle() function
PHP imagesetpixel() function
PHP imagettftext() function