Current Location: Home> Latest Articles> Create colored text output in conjunction with imagecolorallocate()

Create colored text output in conjunction with imagecolorallocate()

M66 2025-05-24

When processing images in PHP, it is often necessary to draw text on the image and it is desired to accurately control the position and color of the text. This article will focus on how to use the imagefontwidth() function combined with imagecolorallocate() to achieve the effect of outputting color text and dynamically calculating text width, so that we can achieve more accurate layout during image processing.

1. Introduction to basic functions

  • imagefontwidth(int $font): int

    This function returns the character width of the specified built-in font, in pixels. PHP built-in font numbers range from 1 to 5, and different font sizes correspond to different widths.

  • imagecolorallocate(resource $image, int $red, int $green, int $blue): int

    Assign a color to the image resource, and the color value is specified with three parameters RGB. Returns the identifier of the color, used for subsequent drawings.

2. Why use imagefontwidth() ?

When drawing text, you usually need to know the width occupied by the text in order to center, align or line break. Because the font width drawn by the imagestring() function is fixed, the width of each character can be obtained by imagefontwidth() , and then multiplied by the length of the string to get the width of the entire text.

3. Sample code explanation

Here is a simple example showing how to generate a picture in PHP, draw text of different colors on the picture, and dynamically calculate the text width to achieve text centering.

 <?php
// Create a canvas,Width300,high100,Background is white
$width = 300;
$height = 100;
$image = imagecreatetruecolor($width, $height);

// White background
$white = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $white);

// Select a font size,scope 1-5
$font = 5;

// 获取文本和计算Width度
$text = "Hello, Colorful text!";
$font_width = imagefontwidth($font);
$text_width = strlen($text) * $font_width;

// Assign colors - red
$red = imagecolorallocate($image, 255, 0, 0);

// Calculate the start of text on the canvasxcoordinate,Center display
$x = ($width - $text_width) / 2;
$y = ($height - imagefontheight($font)) / 2;

// Output text
imagestring($image, $font, $x, $y, $text, $red);

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

4. Code details

  • Get the width of each character by imagefontwidth($font) .

  • Multiply by the string length strlen($text) to get the total width of the text.

  • Use the canvas width to subtract the text width and divide it by 2 to achieve horizontal centering.

  • Use imagecolorallocate() to assign the required color, and here is a red example.

  • Draw text in specified coordinates via imagestring() .

5. Expand ideas

  • If you need to draw multiple segments of text of different colors, you can first calculate the text widths of each segment and draw them in turn.

  • Using the imagefontheight() function, vertical centering can be achieved.

  • For complex text, it is recommended to use imagettftext() in combination with TrueType font, but imagettfbbox() is required to calculate width.

Through this method, the function of dynamically calculating text width and drawing color text in PHP image processing can be easily realized, which facilitates accurate layout when generating pictures with text.