Current Location: Home> Latest Articles> Imageopenpolygon() application practice in image labeling system

Imageopenpolygon() application practice in image labeling system

M66 2025-05-29

In image processing and labeling systems, we often need to label specific areas in the image. A common requirement is to label the area of ​​interest by drawing polygons. As a powerful server-side scripting language, PHP can be used to process images through the GD library. The imageopenpolygon() function in the GD library provides the function of drawing polygons, which is especially suitable for implementing polygon annotation on images.

This article will use an example to show how to implement polygon annotation using the imageopenpolygon() function in PHP, and provide some practical application techniques.

1. Preparation

First, we need to make sure that the GD library of PHP is installed and enabled. If you have not installed the GD library, you can install it through the following command:

 sudo apt-get install php-gd

Then, make sure to enable GD extensions in your PHP script:

 <?php
phpinfo();  // Check if it is enabledGDExtended
?>

2. Create an image and prepare to draw

Next, we will create a simple image and draw a polygon using the imageopenpolygon() function.

 <?php
// Create a new image,Background is white
$image = imagecreatetruecolor(500, 500);
$white = imagecolorallocate($image, 255, 255, 255);  // White background
$black = imagecolorallocate($image, 0, 0, 0);  // Black painted

// 填充Background is white
imagefill($image, 0, 0, $white);

// Vertex coordinates of polygons
$polygon = [
    100, 100,  // vertex1
    400, 100,  // vertex2
    400, 400,  // vertex3
    100, 400   // vertex4
];

// useimageopenpolygonDraw polygons
imageopenpolygon($image, $polygon, count($polygon) / 2, $black);

// Output image to browser
header('Content-Type: image/png');
imagepng($image);

// Free image memory
imagedestroy($image);
?>

3. Code explanation

  1. Create an image : First, use the imagecreatetruecolor() function to create an image of 500x500 pixels. Next, we use imagecolorallocate() to assign color resources to the image, drawing lines for white background and black respectively.

  2. Define the vertices of a polygon : The vertices of a polygon are defined by an array. The $polygon array contains the coordinates of 4 vertices, and each two numbers represent the X and Y coordinates of a point.

  3. Draw polygons : Use the imageopenpolygon() function to draw polygons. The first parameter of this function is the image resource, the second parameter is the vertex array, the third parameter is the number of vertices (note: the array contains X and Y coordinates, so the number of vertices should be half of the length of the array), and the fourth parameter is the drawing color.

  4. Output image : Finally, use imagepng() to output the image to the browser and set the response header to image/png so that the browser can correctly parse and display the image.

4. Further application

In practical applications, you can adjust the shape and position of the polygon according to your needs. For example, you can generate dynamic polygon annotations by generating coordinates entered by users, or draw annotations by combining data in the database. For example, you can get a set of coordinates from the database and generate different polygons to label different regions.

In addition, you can add more elements to the image, such as text comments, labels of different colors, etc. Imageopenpolygon() , you can easily implement the area labeling function on the image.

5. Summary

The imageopenpolygon() function provides us with a convenient way to implement polygon drawing in PHP's GD library, which is very suitable for image annotation system. By flexibly using this function, you can add custom annotations to the image to achieve richer and more accurate image marking functions.