Current Location: Home> Latest Articles> The complete process of drawing a regular triangle with imageopenpolygon()

The complete process of drawing a regular triangle with imageopenpolygon()

M66 2025-05-29

In PHP, we can use the imageopenpolygon() function to draw polygons. By rationally utilizing this function, we can not only draw any polygon, but also draw simple shapes such as regular triangles. This article will introduce in detail how to draw a regular triangle through the imageopenpolygon() function.

What is the imageopenpolygon() function?

The imageopenpolygon() function is part of the GD library in PHP, which allows us to draw polygons through a set of vertices. The basic usage format of the function is as follows:

 imageopenpolygon($image, $points, $num_points, $color);
  • $image : Image resource, usually an image resource created by imagecreatetruecolor() or other image creation function.

  • $points : an array containing the coordinates of polygon vertexes.

  • $num_points : The number of vertices.

  • $color : The color used when drawing polygons.

How to draw a regular triangle?

The characteristic of a regular triangle is that the lengths of the three sides are equal and the three inner angles are equal (60 degrees). In order to draw a regular triangle on the canvas, we first need to determine the coordinates of the three vertices. Suppose we choose a suitable side length and starting point to calculate the positions of these three vertices.

Step 1: Create an image resource

We first need to create an image resource. Use the imagecreatetruecolor() function to create a blank canvas.

 $image = imagecreatetruecolor(200, 200);  // Create a 200x200 Canvas

Step 2: Define the color of the triangle

Use the imagecolorallocate() function to define the color for the drawn triangle.

 $white = imagecolorallocate($image, 255, 255, 255);  // Set the color to white
$black = imagecolorallocate($image, 0, 0, 0);  // Set the border color to black

Step 3: Calculate the vertices of the triangle

Based on the geometric properties of a regular triangle, we can calculate its vertices. Assume that the side is 100 pixels long and the vertices of the triangle are placed in the center of the canvas.

 $centerX = 100;  // Canvas Center X coordinate
$centerY = 100;  // Canvas Center Y coordinate
$sideLength = 100;  // Side length

// 计算三角形的三个vertexcoordinate
$points = [
    $centerX, $centerY - $sideLength / 2, // vertex1
    $centerX - $sideLength / 2, $centerY + $sideLength / 2, // vertex2
    $centerX + $sideLength / 2, $centerY + $sideLength / 2 // vertex3
];

Step 4: Draw the triangle

Now, use the imageopenpolygon() function to draw the triangle.

 imagefilledpolygon($image, $points, 3, $black);  // Fill triangle,3 为vertex数

Step 5: Output the image and clean up the resources

Finally, output the image and free the resource.

 header("Content-type: image/png");
imagepng($image);  // Output image
imagedestroy($image);  // Destroy image resources

Complete code example

 <?php
// Create image resources
$image = imagecreatetruecolor(200, 200);  // Create a 200x200 Canvas

// Define the color
$white = imagecolorallocate($image, 255, 255, 255);  // White
$black = imagecolorallocate($image, 0, 0, 0);  // black

// 计算三角形的vertex
$centerX = 100;
$centerY = 100;
$sideLength = 100;
$points = [
    $centerX, $centerY - $sideLength / 2, // vertex1
    $centerX - $sideLength / 2, $centerY + $sideLength / 2, // vertex2
    $centerX + $sideLength / 2, $centerY + $sideLength / 2 // vertex3
];

// Draw a triangle
imagefilledpolygon($image, $points, 3, $black);  // Fill triangle

// Output image并清理资源
header("Content-type: image/png");
imagepng($image);  // Output image
imagedestroy($image);  // Destroy image resources
?>

illustrate

  • imagecreatetruecolor() is used to create a canvas with a size of 200x200 enough to accommodate a regular triangle.

  • imagecolorallocate() is used to set the color on the canvas, fill the background with white, and draw triangles in black.

  • imagefilledpolygon() is a key function for drawing triangles, which draws triangles based on the provided vertex array.

Things to note

  • imageopenpolygon() is only suitable for polygons, so to ensure that the boundaries are filled, we use the imagefilledpolygon() function.

  • If you need to generate an image and save it to a file, you can use imagepng() to save the image to a specified file, for example:

 imagepng($image, "triangle.png");

Conclusion

With the above method, you can draw a regular triangle using the imageopenpolygon() and imagefilledpolygon() functions. You can adjust the size, color, or position of the triangle as needed. This example shows how to draw basic geometric shapes using the PHP GD library, which you can extend to draw more complex shapes.