When using PHP for image processing, we sometimes face a problem: How to properly arrange text according to the width of characters to form a visual infographic with clear structure? This is especially important in scenarios where precise layout is required based on the actual display width of the character, such as making character statistics, typesetting analysis, or text view under monospace fonts.
PHP's GD library provides some basic but powerful functions to support image manipulation, where imagefontwidth() is a practical function for getting the width of characters in a specified font. This article will focus on this function to demonstrate how to use PHP and GD libraries to create a visual information layout based on character width.
The function prototype of imagefontwidth() is as follows:
int imagefontwidth(int $font);
This function takes a font size (the built-in font sizes supported by the GD library are 1 to 5) and returns the pixel width of a character in the font. Use imagefontheight() to get the font height.
example:
$width = imagefontwidth(3); // Get the font size as 3 Character width
$height = imagefontheight(3);
This is crucial for us to subsequently calculate the placement of characters in the image.
Imagine an example: We have a piece of text data that we want to draw each character onto an image according to its width occupied by the monospace font, leaving a uniform spacing between characters and marking the position index of each character.
Here is a sample code that demonstrates how to use imagefontwidth() to draw a layout diagram based on character width:
<?php
// Text data
$text = "PHPVisualization";
// Use font size
$font = 4;
// Calculate character width and height
$charWidth = imagefontwidth($font);
$charHeight = imagefontheight($font);
// Image size(Width of each character + interval * Number of characters + padding)
$charSpacing = 5;
$padding = 20;
$imageWidth = strlen($text) * ($charWidth + $charSpacing) + $padding * 2;
$imageHeight = $charHeight + $padding * 2;
// Create image resources
$image = imagecreate($imageWidth, $imageHeight);
// Color settings
$background = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);
$lineColor = imagecolorallocate($image, 180, 180, 180);
// Draw characters
for ($i = 0; $i < strlen($text); $i++) {
$x = $padding + $i * ($charWidth + $charSpacing);
$y = $padding;
// Draw reference lines
imageline($image, $x, 0, $x, $imageHeight, $lineColor);
// Draw characters
imagestring($image, $font, $x, $y, $text[$i], $textColor);
}
// Output image
header("Content-Type: image/png");
imagepng($image);
imagedestroy($image);
?>
Save the above code as visualize.php and deploy it to a PHP environment that supports GD library. For example, put it in http://m66.net/visualize.php to access it and see the output image.
In addition to character drawing, you can also add index numbers at the bottom of the character, count the frequency of each character or color to distinguish different character types. Additionally, if you draw each row as a data set, you can even generate a chart-like character heat map.