In PHP, the imagestringup() function is used to draw strings vertically on an image. It works similarly to imagestring(), but the text is drawn vertically upwards, which is very useful when creating images with rotated text effects. However, when using imagestringup() to draw text, the text layout and alignment often need to be manually controlled by the developer. In this case, the imagefontwidth() function can come in handy, helping us accurately calculate the character width to better adjust the text layout and alignment.
imagestringup()
This function is used to draw a string vertically on an image. The syntax is as follows:
bool imagestringup(resource $image, int $font, int $x, int $y, string $string, int $color);
Where $font is the built-in font size (1–5), $x, $y are the starting coordinates, $string is the text, and $color is the color resource.
imagefontwidth()
This function returns the width of a character for the specified built-in font size. The syntax is as follows:
int imagefontwidth(int $font);
By combining these two functions, you can control the positioning of the text when drawing it, avoiding overlapping or poorly aligned text.
Since imagestringup() draws text vertically, the horizontal spacing between characters is fixed. However, without calculating the character width, directly providing coordinates to draw the text might cause it to deviate from the expected position, especially when drawing multiple lines or when centering the text. Therefore, it is necessary to first use imagefontwidth() to obtain the width of the characters and calculate the drawing position of each character, ensuring that the text layout is appropriate.
Suppose we want to draw a single line of text vertically in an image with a width of 200 pixels and achieve horizontal center alignment:
<?php
// Create a blank image
$width = 200;
$height = 100;
$image = imagecreatetruecolor($width, $height);
<p>// Allocate colors<br>
$white = imagecolorallocate($image, 255, 255, 255);<br>
$black = imagecolorallocate($image, 0, 0, 0);</p>
<p>// Fill background color<br>
imagefill($image, 0, 0, $white);</p>
<p>$text = "Hello";<br>
$font = 5; // Built-in font size 1~5</p>
<p>// Calculate the width of the text<br>
$fontWidth = imagefontwidth($font);<br>
$textWidth = strlen($text) * $fontWidth;</p>
<p>// Calculate the starting x coordinate to center the text horizontally<br>
$x = ($width - $textWidth) / 2;</p>
<p>// Set the y coordinate to start from the bottom and move upwards<br>
$y = 80;</p>
<p>// Use imagestringup() to draw vertical text<br>
imagestringup($image, $font, $x, $y, $text, $black);</p>
<p>// Output the image<br>
header("Content-Type: image/png");<br>
imagepng($image);<br>
imagedestroy($image);<br>
?><br>
Use imagefontwidth($font) to get the width of each character of the font.
Multiply the string length by the character width to get the total text width.
Subtract the text width from the image width and divide by 2 to center the text horizontally.
In the coordinates for imagestringup(), x is the left edge of the text, and y is the vertical coordinate of the baseline of the text's bottom.
If you need to draw multiple lines of vertical text, for example, displaying multiple text segments from left to right, with each segment vertically displayed, you can also use imagefontwidth() to calculate the width of each segment and dynamically adjust the x-coordinate:
<?php
$width = 300;
$height = 150;
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefill($image, 0, 0, $white);
<p>$font = 4;<br>
$texts = ["PHP", "Imagestringup", "Example"];</p>
<p>// Calculate the total width<br>
$fontWidth = imagefontwidth($font);<br>
$totalWidth = 0;<br>
foreach ($texts as $t) {<br>
$totalWidth += strlen($t) * $fontWidth + 10; // 10 pixels between each segment<br>
}<br>
$totalWidth -= 10; // No gap after the last segment</p>
<p>// Start x coordinate is centered<br>
$x = ($width - $totalWidth) / 2;<br>
$y = 130;</p>
<p>foreach ($texts as $t) {<br>
imagestringup($image, $font, $x, $y, $t, $black);<br>
$x += strlen($t) * $fontWidth + 10;<br>
}</p>
<p>header("Content-Type: image/png");<br>
imagepng($image);<br>
imagedestroy($image);<br>
?><br>
This approach allows you to flexibly control the arrangement and alignment of multiple lines of vertical text.