Current Location: Home> Latest Articles> How to Use the imagestringup Function in PHP to Draw Vertical Text on Images: A Detailed Guide

How to Use the imagestringup Function in PHP to Draw Vertical Text on Images: A Detailed Guide

gitbox 2025-08-25

1. imagestringup() Function Overview

imagestringup() is a function in PHP's GD library used to draw vertical text on images. Similar to the imagestring() function, imagestringup() can place text at a specified position on an image, but its key difference is that the text is arranged vertically instead of horizontally.

Function Prototype:

<span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-title function_ invoke__">imagestringup</span></span><span> ( resource </span><span><span class="hljs-variable">$image</span></span><span> , </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$font</span></span><span> , </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$x</span></span><span> , </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$y</span></span><span> , </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$text</span></span><span> , </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$color</span></span><span> )
</span></span>

Parameters:

  • image: The target image resource, created using functions like imagecreate() or imagecreatefromjpeg().

  • font: Font size, using GD's built-in fonts or specifying the size of a custom font.

  • x: The X-coordinate for the starting position of the text.

  • y: The Y-coordinate for the starting position of the text.

  • text: The string of text to draw.

  • color: Text color, usually generated with the imagecolorallocate() function.

Return Value:

This function returns an integer indicating whether the drawing operation was successful, but in most cases, you don't need to pay attention to the return value.


2. Basic Steps to Draw Vertical Text Using imagestringup()

Drawing vertical text on an image follows a similar process to drawing horizontal text, except that you use imagestringup() instead of imagestring(). Here are the detailed steps:

  1. Create an image resource.

  2. Allocate color resources.

  3. Use the imagestringup() function to draw text on the image.

  4. Output the image to the browser or save it to a file.

Example Code:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Create a blank true color image</span></span><span>
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreate</span></span><span>(</span><span><span class="hljs-number">300</span></span><span>, </span><span><span class="hljs-number">200</span></span><span>);
<p></span>// Allocate background and text colors<br>
$backgroundColor = imagecolorallocate($image, 255, 255, 255);  // White background<br>
$textColor = imagecolorallocate($image, 0, 0, 0);  // Black text</p>
<p>// Set the text to draw<br>
$text = "Vertical Text Example";</p>
<p>// Draw vertical text<br>
imagestringup($image, 5, 50, 50, $text, $textColor);</p>
<p>// Output the image to the browser<br>
header("Content-type: image/png");<br>
imagepng($image);</p>
<p>// Destroy the image resource<br>
imagedestroy($image);<br>
?><br>
</span>

In the code above, we created a 300x200 blank image with a white background and black text. Then, we called imagestringup() to draw the vertical text "Vertical Text Example" on the image. Finally, the image is output as a PNG to the browser, and the image resource is destroyed to free memory.


3. Common Drawing Tips and Considerations

3.1 Adjusting Font Size

The font size in imagestringup() is determined by the second parameter. This value is a number representing the font size. Larger numbers result in taller and bolder text. Adjust the size according to your needs.

3.2 Controlling Text Position

When calling imagestringup(), you need to provide the starting coordinates (x and y). Since the text is vertical, the x coordinate sets the horizontal position, and the y coordinate sets the vertical starting point. Adjust these parameters to control the exact placement of the text.

3.3 Using Custom Fonts

imagestringup() only supports built-in fonts (for example, font number 5). If you need more complex or stylized fonts, use imagettftext(), which allows TrueType fonts. imagestringup() is suitable for simple vertical text, but for advanced font control, imagettftext() is the better choice.

3.4 Text and Background Contrast

To ensure vertical text is clearly readable, choose colors with high contrast against the background. Use dark text on light backgrounds and vice versa.

3.5 Adjusting Image Size

If the text is long or the font is large, it may exceed the image boundaries. Measure the text dimensions beforehand to ensure the image is large enough to fit all the text.