In PHP, the imagecharup() function is a highly useful tool that enables vertical text rendering on images. With this capability, developers can dynamically overlay character data onto generated images. Common use cases include CAPTCHA creation, image annotations, and watermarks. This article provides a comprehensive explanation of how to use the imagecharup() function to dynamically add text to images.
The basic syntax of the imagecharup() function is as follows:
<span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-title function_ invoke__">imagecharup</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>
$image: The image resource where the text will be drawn.
$font: Font size (must be an integer between 1 and 5, where higher numbers mean larger fonts).
$x: The X coordinate where the text starts drawing.
$y: The Y coordinate where the text starts drawing.
$text: The string to be drawn.
$color: The color of the text, usually allocated with imagecolorallocate().
Returns a value greater than zero on success, or FALSE on failure.
To use this function, you must first create an image, choose a color, font size, and position, and then call imagecharup() to write the text onto the image.
Here's a simple example that shows how to vertically add text onto a blank image:
<span><span><span class="hljs-meta"><?php</span></span><span>
// Create a 200x200 blank image
$image = imagecreate(200, 200);
<p>// Allocate background color (white)<br>
$background_color = imagecolorallocate($image, 255, 255, 255);</p>
<p>// Allocate text color (black)<br>
$text_color = imagecolorallocate($image, 0, 0, 0);</p>
<p>// Set font size to 5<br>
$font_size = 5;</p>
<p>// Text content<br>
$text = "Hello, PHP!";</p>
<p>// Draw text at position (50, 50) vertically<br>
imagecharup($image, $font_size, 50, 50, $text, $text_color);</p>
<p>// Output image<br>
header('Content-Type: image/png');<br>
imagepng($image);</p>
<p>// Free memory<br>
imagedestroy($image);<br>
?></span>
Create Image Resource:
Use imagecreate() to create a 200x200 pixel blank image. You can change the size as needed.
Color Allocation:
Use imagecolorallocate() to assign colors. In this example, white is used for the background, black for the text.
Text Drawing:
Use imagecharup() to vertically draw the text at the coordinates (50, 50) with a font size of 5, which is valid for this function.
Output Image:
Use header() to set the correct MIME type, then imagepng() to output the image. You can also output as JPEG or GIF if needed.
Cleanup:
Use imagedestroy() to release memory and prevent memory leaks.
The imagecharup() function draws text starting from the (x, y) position. To change the placement of your text, simply adjust these coordinates.
Because the text is displayed vertically, spacing between characters depends on the font size. Choosing the right font and content will help maintain readability.
imagecharup() can be used to create dynamic CAPTCHA images. Random characters rendered vertically enhance the complexity and security of the CAPTCHA.
When adding watermarks to images, imagecharup() can vertically place text in a specific position. By tweaking the color, font, and opacity, the watermark can blend seamlessly with the background.
If you need to annotate or label an image, imagecharup() allows you to display explanatory text vertically—very useful for charts or reports.
imagecharup() is a powerful function that makes it easy and fast to dynamically add text to images. Whether you're generating CAPTCHAs, applying watermarks, or annotating graphics, imagecharup() plays an important role. With the right font size, position, and color settings, you can tailor the text rendering to meet specific needs. We hope this guide helps you understand and effectively use PHP's imagecharup() function.