Current Location: Home> Latest Articles> Imagefttext function tricks to draw shadowed text

Imagefttext function tricks to draw shadowed text

gitbox 2025-05-26

When processing images using PHP, the imagefttext function is a very practical tool that allows us to draw text on images that supports TrueType fonts. In addition to ordinary text drawing, we can also add shadow effects to the text to enhance visual hierarchy and aesthetics.

This article will explain in detail how to use the imagefttext function to draw shadowed text, and use actual code examples to help you master this technique quickly.

1. Preparation

First, make sure your server environment has GD extension enabled and PHP supports the FreeType font library. Additionally, you need to prepare a TTF font file, such as the common arial.ttf . You can copy from system fonts, or download your favorite font files and store them in the project directory.

2. Review of basic usage

The basic syntax of imagefttext is as follows:

 imagefttext(
    GdImage $image,
    float $size,
    float $angle,
    int $x,
    int $y,
    int $color,
    string $font_filename,
    string $text,
    array $options = []
): array

Draw text without adding shadows, the code might look like this:

 $im = imagecreatetruecolor(400, 200);
$bg = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 400, 200, $bg);

$textColor = imagecolorallocate($im, 0, 0, 0);
$font = __DIR__ . '/arial.ttf';

imagefttext($im, 20, 0, 50, 100, $textColor, $font, 'Hello World');

3. Draw shadowed text

The trick to draw shadows is: first draw the shadows, then draw the main text . Usually the shadows are the same text with slightly offset and darker colors or less transparency.

Here is a complete example of adding a shadow effect:

 $im = imagecreatetruecolor(400, 200);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 400, 200, $white);

// Shadow color(grey)
$shadowColor = imagecolorallocate($im, 100, 100, 100);
// Main text color(black)
$textColor = imagecolorallocate($im, 0, 0, 0);

$font = __DIR__ . '/arial.ttf';
$text = 'Hello Shadow';

// Text position and size
$fontSize = 24;
$angle = 0;
$x = 50;
$y = 100;

// Draw the shadow first(Offset to the lower right2px)
imagefttext($im, $fontSize, $angle, $x + 2, $y + 2, $shadowColor, $font, $text);

// Draw the main text
imagefttext($im, $fontSize, $angle, $x, $y, $textColor, $font, $text);

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

4. Effect description

  • imagecolorallocate is used to define the colors of the main text and shadows.

  • Shadows are implemented by fine-tuning of text position, for example (x+2, y+2) .

  • The shade color should be lower than the main text color brightness, or the transparency can be set through imagecolorallocatealpha to obtain a more natural shadow.

5. Online demonstration and testing

If you want to quickly view the effect, you can save the above code as a shadow-text.php file and upload it to a PHP-enabled server, for example:

 https://gitbox.net/shadow-text.php

Make sure the server has the corresponding font file (such as arial.ttf ) in the same directory, otherwise the font loading will fail.

6. Summary

With simple coordinate offsets and reasonable color matching, we can use imagefttext to achieve satisfactory text shadow effect. This technique is widely used in verification code generation, watermark drawing, and personalized image output. Making good use of the GD library can greatly improve your image processing capabilities.

In future projects, you might as well try more changes in text styles, such as overlaying blur shadows, multi-layer projection, stroke effects, etc., to further beautify the output image.