Current Location: Home> Latest Articles> How to change the imageopenpolygon() drawing direction through rotation transformation

How to change the imageopenpolygon() drawing direction through rotation transformation

M66 2025-05-29

When drawing graphics using the GD library in PHP, the imageopenpolygon() function can be used to draw an open polygon (i.e., polylines that are not connected to the end). By default, imageopenpolygon() is drawn directly based on the dot set you provide. If you want to adjust the drawing direction of the polygon, such as rotating it at a certain angle, you need to rotate the coordinate points.

This article will explain in detail how to apply rotation transformation to the graph drawn by imageopenpolygon() in PHP.

Understand the principle of rotation transformation

On a two-dimensional plane, after rotating an angle θ around the origin (0, 0) , the calculation formula for the new coordinate (x', y') is:

 x' = x * cos(θ) - y * sin(θ)
y' = x * sin(θ) + y * cos(θ)

If you want to rotate around any point (cx, cy) , you need to first translate the coordinates to the origin, and then translate them back:

 x' = cos(θ) * (x - cx) - sin(θ) * (y - cy) + cx
y' = sin(θ) * (x - cx) + cos(θ) * (y - cy) + cy

Applying rotation transformation in PHP

We can write a small function to rotate a set of points based on the above formula. Here is a complete example of how to create an image, draw a rotated polygon, and output the result.

 <?php
// Define rotation function
function rotatePoints(array $points, float $angleDegrees, float $centerX = 0, float $centerY = 0): array {
    $angleRadians = deg2rad($angleDegrees);
    $cosTheta = cos($angleRadians);
    $sinTheta = sin($angleRadians);
    $rotatedPoints = [];

    for ($i = 0; $i < count($points); $i += 2) {
        $x = $points[$i];
        $y = $points[$i + 1];

        $xRotated = $cosTheta * ($x - $centerX) - $sinTheta * ($y - $centerY) + $centerX;
        $yRotated = $sinTheta * ($x - $centerX) + $cosTheta * ($y - $centerY) + $centerY;

        $rotatedPoints[] = $xRotated;
        $rotatedPoints[] = $yRotated;
    }

    return $rotatedPoints;
}

// Create a canvas
$width = 400;
$height = 400;
$image = imagecreatetruecolor($width, $height);

// Assign colors
$backgroundColor = imagecolorallocate($image, 255, 255, 255);
$lineColor = imagecolorallocate($image, 0, 0, 0);

// Fill the background
imagefill($image, 0, 0, $backgroundColor);

// Define original point set(A simple triangle)
$points = [
    200, 100,  // point1
    300, 300,  // point2
    100, 300   // point3
];

// Set the rotation angle
$angle = 45; // Rotate clockwise45Degree

// 计算Rotate后的point集
$rotatedPoints = rotatePoints($points, $angle, 200, 200); // Around the center(200,200)Rotate

// 绘制Rotate后的开放多边形
imageopenpolygon($image, $rotatedPoints, count($rotatedPoints) / 2, $lineColor);

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

// Destroy resources
imagedestroy($image);
?>

Things to note

  • imageopenpolygon() requires that the point array is a one-dimensional array in the order [x1, y1, x2, y2, ..., xn, yn] .

  • When rotating, it is best to use the center of the figure as the rotation center so that the figure will not deviate from the center of the canvas.

  • PHP's built-in trigonometric function is in units of radians, so deg2rad() is used to convert angle to radians.

Conclusion

Through simple rotation transformation processing, we can make the figure drawn by imageopenpolygon() rotate at any angle, which is very flexible. If you want to learn more about PHP image processing tips, you can refer to the detailed tutorial here .

I hope this article can help you better understand the combination of imageopenpolygon() and rotation transformation in PHP!