Current Location: Home> Latest Articles> Manually align text drawing with imagesetpixel()

Manually align text drawing with imagesetpixel()

M66 2025-06-05

Drawing text is a common requirement in the PHP image processing extension GD library. Usually we use imagestring() or imagettftext() to draw text, but these functions are simpler in alignment and layout, and have limited flexibility. If you want to accurately control text position at the pixel level, especially custom alignment, you can combine imagefontwidth() and imagesetpixel() to achieve finer grained text drawing and typography.

This article will introduce the role of these two functions and how to use them to achieve manual alignment of text drawing.


1. Understand imagefontwidth()

The imagefontwidth() function is used to get the width (units: pixels) of a character in a specified built-in font. The GD library comes with several built-in fonts (1~5), and the character width and height of each font are fixed.

 int imagefontwidth(int $font);
  • Parameter $font : Font size, range 1 to 5.

  • Return value: The width (pixel) of a single character of the font.

For example:

 $font = 3;
$charWidth = imagefontwidth($font);
echo "The font width is $charWidth Pixels";

With this width, you can estimate the pixel length required for drawing a line of text, and then adjust the alignment.


2. Understand imagesetpixel()

The imagesetpixel() function is used to set the color value of a single pixel on the specified image resource.

 bool imagesetpixel(resource $image, int $x, int $y, int $color);
  • $image : Image resource.

  • $x, $y : The coordinates of the pixel point.

  • $color : Color index or RGB color.

This means that we can customize the position of text by pixel operation, and even fine-tune it to achieve more precise text layout.


3. Implementation idea: combine the two to achieve manual alignment

Usually when we draw text, we can only roughly estimate the overall position according to the font size and text length, such as center, left or right. But when more meticulous adjustments are needed (such as fine-tuning individual character positions, staggered arrangements, and aligning special graphics), imagefontwidth() and imagesetpixel() come in handy.

Example ideas:

  1. Use imagefontwidth() to obtain the font width and calculate the pixel width of the entire line of text.

  2. Calculate the X coordinate of the text's starting point based on the requirement (such as center alignment).

  3. Use a loop to iterate through each character in the string and draw one by one:

    • Use imagestring() to draw characters first.

    • Or use imagesetpixel() to fine-tune the characters, such as adding extra pixels to adjust the position.

  4. By operating a single pixel, a detailed displacement and alignment effect is achieved.


4. Sample code

In the following example, it demonstrates how to manually calculate the text drawing starting point, achieve center alignment, and fine-tune the edge pixels of text with imagesetpixel() to enhance the text visual effect.

 <?php
// Create a 300x100 Images
$img = imagecreatetruecolor(300, 100);

// Set background color(White)
$white = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $white);

// Set text color(black)
$black = imagecolorallocate($img, 0, 0, 0);

$text = "Hello GD!";
$font = 3; // Built-in fonts

// Get single character width and height
$charWidth = imagefontwidth($font);
$charHeight = imagefontheight($font);

// Calculate text width
$textWidth = strlen($text) * $charWidth;

// Calculation level centered starting point X coordinate
$startX = (imagesx($img) - $textWidth) / 2;
$startY = (imagesy($img) - $charHeight) / 2;

// Manual verbatim drawing,Implement fine-tuning(如错开每个字一个Pixels)
for ($i = 0; $i < strlen($text); $i++) {
    $char = $text[$i];
    // Start position of each character,Increase Y Axis staggered offset
    $x = $startX + $i * $charWidth;
    $y = $startY + ($i % 2); // Odd and even staggered 1 Pixels

    // Draw characters
    imagestring($img, $font, $x, $y, $char, $black);

    // Additional fine adjustments:use imagesetpixel() 给字符右下角加一个Pixels点,Increase视觉效果
    imagesetpixel($img, $x + $charWidth - 1, $y + $charHeight - 1, $black);
}

// Output image
header("Content-Type: image/png");
imagepng($img);
imagedestroy($img);
?>

5. Summary

  • imagefontwidth() is a key function to understand the built-in font width and calculate text length and position.

  • imagesetpixel() can be fine-tuned or embellished after the drawing is completed to achieve a more flexible text visual effect.

  • Combining the two, text layout can be accurately controlled through character-by-character drawing and pixel-by-pixel operation to meet customized needs.

  • This technique is especially suitable for image text rendering scenes with strict alignment requirements and complex typesetting.