Current Location: Home> Latest Articles> How to correctly specify text color in imagefttext function

How to correctly specify text color in imagefttext function

gitbox 2025-05-29

In PHP, the imagefttext function is a powerful tool for drawing text on images. It supports TrueType fonts and complex text typesetting. Properly specifying text colors is one of the keys to using imagefttext . This article will introduce in detail how to correctly set text colors and help you understand better through examples.


1. What is the imagefttext function?

The imagefttext function is defined as follows:

 array imagefttext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text [, array $extrainfo = null ] )
  • $image : Target image resource

  • $size : Font size

  • $angle : Text rotation angle

  • $x , $y : text start coordinates

  • $color : text color, use imagecolorallocate function to obtain

  • $fontfile : Font file path

  • $text : The text content to be drawn

  • $extrainfo : Extra information, usually not available


2. How to correctly specify text color?

Text color is assigned through the imagecolorallocate() function, which accepts 4 parameters:

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

  • $red , $green , $blue : The RGB component of the color, the value range is 0-255

For example, red can be obtained by imagecolorallocate($image, 255, 0, 0) .

Notes:

  • Colors must be assigned before drawing text.

  • After color allocation, an integer identifier will be returned, and this value must be passed to the $color parameter of the imagefttext function.

  • The color identifiers of different image resources are independent and cannot be used across images.


3. Detailed step demonstration

Step 1: Create an image resource

 $image = imagecreatetruecolor(400, 200);

Step 2: Assign color to the background and fill it

 $bg_color = imagecolorallocate($image, 255, 255, 255); // White
imagefilledrectangle($image, 0, 0, 399, 199, $bg_color);

Step 3: Assign text color

 $text_color = imagecolorallocate($image, 0, 0, 255); // blue

Step 4: Specify the font file path

The font file must be a real TTF file path, for example:

 $font_path = 'gitbox.net/fonts/arial.ttf';

Note: The domain name part is replaced by gitbox.net , and you need to replace it with the actual path of the font file on your server.

Step 5: Draw text

 $text = "Hello, PHP imagefttext!";
$size = 20;
$angle = 0;
$x = 10;
$y = 50;

imagefttext($image, $size, $angle, $x, $y, $text_color, $font_path, $text);

Step 6: Output the image

 header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);

4. Complete sample code

 <?php
// Create image resources
$image = imagecreatetruecolor(400, 200);

// Assign background color and fill
$bg_color = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, 399, 199, $bg_color);

// Assign text color
$text_color = imagecolorallocate($image, 0, 0, 255);

// Font path(Please make sure the path is correct)
$font_path = 'gitbox.net/fonts/arial.ttf';

// Text to be drawn
$text = "Hello, PHP imagefttext!";
$size = 20;
$angle = 0;
$x = 10;
$y = 50;

// Draw text
imagefttext($image, $size, $angle, $x, $y, $text_color, $font_path, $text);

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

5. Frequently Asked Questions

  • Font file not found <br> Make sure the font file path is correct. Imagefttext cannot automatically look for font files. The path must be a valid path on the server.

  • Invalid color or abnormal display <br> Please check if the imagecolorallocate function is used correctly and the color identifier is passed into imagefttext .

  • Text not displayed <br> Check whether the coordinates are within the image range and whether the font size and angle are reasonable.