Current Location: Home> Latest Articles> Implement basic text image output in conjunction with imagecreate()

Implement basic text image output in conjunction with imagecreate()

M66 2025-05-31

In PHP, the GD library provides a complete set of functions for processing images, which can be used to dynamically generate image files. For some scenarios where text needs to be output in an image, such as generating verification codes, creating watermarked images, generating signature images, etc., imagecreate() and imagefontwidth() are two very practical functions.

This article will introduce in detail how to use imagefontwidth() to get the font width and combine imagecreate() to create a simple text image output.

1. Understand the imagefontwidth() function

imagefontwidth() is a function used in the PHP GD library to obtain a font width, and its definition is as follows:

 int imagefontwidth(int $font);
  • The $font parameter specifies the font size and can only be built-in fonts (1 to 5).

  • The return value is the pixel width of each character in the font.

For example, imagefontwidth(5) usually returns 9, indicating that font 5 is 9 pixels wide for each character.

2. Use imagecreate() to create an image canvas

imagecreate() is used to create a blank image resource, and its basic syntax is as follows:

 resource imagecreate(int $width, int $height);

The parameters are the width and height of the canvas (units: pixels), respectively. The returned is an image resource handle that can be used for subsequent drawings.

3. Combine the two and output text images

Here is a complete example showing how to dynamically create an image canvas based on the length of the input text and output the text using imagestring() .

 <?php
// Set content
$text = "Welcome to visit m66.net!";
$font = 5;

// Calculate image size
$font_width = imagefontwidth($font);
$font_height = imagefontheight($font);
$width = $font_width * strlen($text);
$height = $font_height + 10;

// Create a canvas
$image = imagecreate($width, $height);

// Set color
$white = imagecolorallocate($image, 255, 255, 255); // White background
$black = imagecolorallocate($image, 0, 0, 0);       // Black font

// Write string
imagestring($image, $font, 0, 5, $text, $black);

// Output image to browser
header("Content-Type: image/png");
imagepng($image);

// Destroy resources
imagedestroy($image);
?>

When running the above code, PHP will dynamically generate a PNG image, and the text "Welcome to m66.net!" is displayed on the image.

IV. Expand the application

Although the built-in fonts are limited, using custom TTF fonts with imagettftext() can achieve more exquisite typography. However, when quickly outputting test text or being used for simple image markup, the combination of imagefontwidth() and imagecreate() is still very efficient.

Summarize

Through the explanation of this article, we learned how to use imagefontwidth() to get character width and create canvas with imagecreate() , thus implementing a basic text image output program. This process is very suitable for beginners to understand the basic principles of PHP image processing, and also lays the foundation for subsequent more complex image processing.