How to draw an open polygon using the imageopenpolygon() function n PHP?


imageopenpolygon() is an inbuilt function in PHP that is used to draw an open polygon on a given image.

Syntax

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

Parameters

imageopenpolygon() takes four different parameters: $image, $points, $num_points and$color. 

  • $image − Specifies the image resource to work on.

  • $image − Specifies the image resource to work on.

  • $points − Specifies the points of the polygon.

  • $num_points − Specifies the number of points. The total number of (vertices) points must be at least three.

  • $color − This parameter specifies the color of the polygon.

Return Values

imageopenpolygon() returns True on success and False on failure.

Example 1

<?php
   // Create a blank image using imagecreatetruecolor() function.
   $img = imagecreatetruecolor(700, 300);

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

   // Draw the polygon
   imageopenpolygon($img, array(
      0, 0,
      100, 200,
      400, 200
   ),
   3,
   $col_poly);

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

Output

Example 2

<?php
   // Create a blank image using imagecreatetruecolor() function.
   $image = imagecreatetruecolor(700, 300);
   
   // allocate the colors
   $blue = imagecolorallocate($image, 0, 255, 255);

   // Six points of the array
   $points = array(
      60, 130,
      130, 230,
      280, 230,
      350, 130,
      210, 30,
      60, 130
   );
   
   // Create a polygon
   imageopenpolygon($image, $points, 6, $blue);

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

Output

Updated on: 09-Aug-2021

289 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements