Thursday, October 23, 2025
HomeLanguagesPHP | imageopenpolygon() Function

PHP | imageopenpolygon() Function

The imageopenpolygon() function is an inbuilt function in PHP which is used to draws an open polygon.

Syntax:

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

Parameters: This function accepts four parameters as mentioned above and described below:

  • $image: It specifies the image resource to work on.
  • $points: It specifies the points of polygon.
  • $num_points: It specifies the number of points.
  • $color: It specifies the color of polygon.

Return Value: This function returns TRUE on success or FALSE on failure.

Below examples illustrate the imageopenpolygon() function in PHP:

Example 1: In this example, we will draw polygon on a blank drawing.




<?php
  
// Create a blank image
$image = imagecreatetruecolor(400, 300);
  
// Prepare the colors
$red = imagecolorallocate($image, 255, 0, 0);
  
// Points of array
$points =  array(
    80, 150,
    150, 250,
    300, 250,
    370, 150,
    230, 50,
    80, 150
);
  
// Create an polygon
imageopenpolygon($image, $points, 6, $red);
  
// Output to browser
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>


Output:

Example 2: In this example, drawing polygon on a image.




<?php
  
// Create an image instance
$image = imagecreatefrompng(
  
// Prepare the colors
$red = imagecolorallocate($image, 255, 0, 0);
  
// Points of array
$points =  array(
    10, 10,
    660, 10,
    660, 100,
    10, 100,
    10, 10
);
  
// Create an polygon
imageopenpolygon($image, $points, 5, $red);
  
// Output to browser
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
?>


Output:

Reference: https://www.php.net/manual/en/function.imageopenpolygon.php

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS