Current Location: Home> Latest Articles> Detailed Explanation of Text Rotation Function in imagettftext() and Common Issue Solutions

Detailed Explanation of Text Rotation Function in imagettftext() and Common Issue Solutions

gitbox 2025-08-07

Detailed Explanation of Text Rotation Function in imagettftext() and Common Issue Solutions

In PHP, the imagettftext() function is a very practical tool used to add TrueType font (TTF) based text to images. With this function, you can precisely control the style, size, color, position, and even the rotation angle of the text. Today, we will focus on the text rotation feature of the imagettftext function and offer solutions for some common problems.

1. Overview of the imagettftext() Function

The basic syntax of the imagettftext() function is as follows:

<span><span><span class="hljs-title function_ invoke__">imagettftext</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-variable">$size</span></span><span>, </span><span><span class="hljs-variable">$angle</span></span><span>, </span><span><span class="hljs-variable">$x</span></span><span>, </span><span><span class="hljs-variable">$y</span></span><span>, </span><span><span class="hljs-variable">$color</span></span><span>, </span><span><span class="hljs-variable">$fontfile</span></span><span>, </span><span><span class="hljs-variable">$text</span></span><span>);
</span></span>
  • $image: The image resource to which the text will be added.

  • $size: The font size of the text.

  • $angle: The rotation angle of the text in degrees (positive values mean counterclockwise rotation, negative values mean clockwise rotation).

  • $x, $y: The starting position coordinates of the text (bottom-left corner).

  • $color: The color of the text, usually created by the imagecolorallocate() function.

  • $fontfile: The path to the TrueType font file.

  • $text: The content of the text to display.

2. Analysis of the Text Rotation Feature

One of the key features of the imagettftext() function is its text rotation capability, implemented via the $angle parameter. By adjusting this parameter, we can control the rotation angle of the text. The angle is measured in degrees, with positive values rotating the text counterclockwise and negative values rotating it clockwise.

For example, if we want the text to rotate 45 degrees clockwise, we can call it like this:

<span><span><span class="hljs-title function_ invoke__">imagettftext</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-number">20</span></span><span>, -</span><span><span class="hljs-number">45</span></span><span>, </span><span><span class="hljs-number">100</span></span><span>, </span><span><span class="hljs-number">150</span></span><span>, </span><span><span class="hljs-variable">$color</span></span><span>, </span><span><span class="hljs-variable">$fontfile</span></span><span>, </span><span><span class="hljs-string">"Hello, World!"</span></span><span>);
</span></span>

This line of code will render the text “Hello, World!” in font size 20, rotated 45 degrees clockwise, drawn on the image at coordinates (100, 150).

3. How to Calculate the Actual Text Position

When rotating text, you may encounter issues with text positioning. Since the bounding box of the text changes after rotation, the rotated text is not always based on the original coordinates.

A common solution is to use the imagettfbbox() function to get the bounding box of the rotated text. This function returns the coordinates of the four corners of the text, which can be used to calculate the correct position for the text.

<span><span><span class="hljs-variable">$bbox</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagettfbbox</span></span><span>(</span><span><span class="hljs-number">20</span></span><span>, -</span><span><span class="hljs-number">45</span></span><span>, </span><span><span class="hljs-variable">$fontfile</span></span><span>, </span><span><span class="hljs-string">"Hello, World!"</span></span><span>);
</span><span><span class="hljs-variable">$x</span></span><span> = </span><span><span class="hljs-number">100</span></span><span> - </span><span><span class="hljs-variable">$bbox</span></span><span>[</span><span><span class="hljs-number">0</span></span><span>];  </span><span><span class="hljs-comment">// Adjust starting position based on bounding box</span></span><span>
</span><span><span class="hljs-variable">$y</span></span><span> = </span><span><span class="hljs-number">150</span></span><span> - </span><span><span class="hljs-variable">$bbox</span></span><span>[</span><span><span class="hljs-number">1</span></span><span>];
</span><span><span class="hljs-title function_ invoke__">imagettftext</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-number">20</span></span><span>, -</span><span><span class="hljs-number">45</span></span><span>, </span><span><span class="hljs-variable">$x</span></span><span>, </span><span><span class="hljs-variable">$y</span></span><span>, </span><span><span class="hljs-variable">$color</span></span><span>, </span><span><span class="hljs-variable">$fontfile</span></span><span>, </span><span><span class="hljs-string">"Hello, World!"</span></span><span>);
</span></span>

4. Common Issues When Rotating Text and Their Solutions

4.1 Text Position Offset

When rotating text, position offset issues may occur. This is often because the bounding box changes after rotation. The solution is to use the imagettfbbox() function to calculate the rotated text’s bounding box and then adjust the coordinates accordingly.

4.2 Text Overlapping

If multiple rotated texts are added to the same image, they may overlap. The solution is to manually adjust each text’s $x and $y coordinates or use a loop to generate multiple texts with different starting positions for each.

4.3 Font Clarity Issues

Sometimes, rotated text may appear blurry or distorted, especially at smaller font sizes. This issue is usually related to the image resolution. To fix it, try increasing the image resolution (i.e., enlarging the width and height of the image) and then scaling it down to the target size.

4.4 Incorrect Rotation Angle

Make sure the rotation angle value is correct, especially when using negative values. In PHP, the imagettftext() function interprets positive angles as counterclockwise and negative angles as clockwise rotations. If the text rotates in the opposite direction than expected, try adjusting the sign of the angle.

5. Summary

The text rotation feature in the imagettftext() function is a very powerful tool that allows developers to flexibly add rotated text to images. By properly using the rotation angle and combining it with the imagettfbbox() function to calculate bounding boxes, you can effectively resolve positioning deviations and overlapping issues during rotation. Mastering these techniques will enable you to handle image operations involving rotated text with greater ease.