How to add text to an image with PHP GD library

How to add text to an image with PHP GD library

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Debugging Image Rendering Issues in PHP GD: A Case Study

You are encountering a blank page when rendering an image because your script is failing silently before it can send any output to the browser. The root cause lies in the ImageCreateFromGif function call within image_creator.php. This function attempts to load a GIF file named photo.gif, but if that file does not exist relative to the script's execution path, PHP throws an error.

In older versions of PHP, this would trigger a fatal error and stop the script immediately. However, depending on your server configuration—specifically the display_errors directive set in your php.ini or .user.ini file—and whether error reporting is enabled via HTML, you might see no output at all if errors are being logged to the server rather than displayed to the client. Even if errors are configured to display, a missing image file is a hard stop; the script cannot allocate an image resource from nothing.

To resolve this, you must ensure that photo.gif exists in the directory where image_creator.php resides. Furthermore, it is best practice to explicitly check for file existence before attempting to load it. This prevents the script from crashing if the file is missing and allows you to handle the error gracefully or exit cleanly.

Here is a revised version of your image_creator.php that includes validation:

<?php
// Ensure error reporting is visible during development
error_reporting(E_ALL);
ini_set('display_errors', 1);

header("Content-Type: image/jpeg");

// Define the path to the source image relative to the script
$sourceImage = __DIR__ . '/photo.gif';

// Check if the file exists before attempting to load it
if (!file_exists($sourceImage)) {
    // Log the error or handle it appropriately
    echo "Error: The source GIF file was not found at: $sourceImage"; 
    exit;
}

$im = ImageCreateFromGif($sourceImage);

// If the image resource is false, GD failed to load the file (e.g., corrupted file)
if ($im === false) {
    echo "GD error loading GIF: " . imageerrorstring();
    exit;
}

$black = ImageColorAllocate($im, 255, 255, 255);
$start_x = 10;
$start_y = 20;

// Ensure the font file exists as well, or Imagettftext will fail
if (!file_exists('verdana.ttf')) {
    echo "Error: Font file 'verdana.ttf' not found.";
    exit;
}

Imagettftext($im, 12, 0, $start_x, $start_y, $black, 'verdana.ttf', "text to write");

// Convert and output the image
Imagejpeg($im, '', 100);

// Free memory
ImageDestroy($im);
?>

The Imagejpeg Return Value Trap

A common pitfall when using GD functions like Imagejpeg, Imagepng, or Imagettftext is ignoring their return values. These functions typically return true on success and false on failure. If you do not check the result, a corrupted image file or an invalid font path can cause the function to fail silently (depending on error settings), leaving your script hanging or outputting nothing visible.

In the example above, we check $im === false. Note that ImageCreateFromGif returns the image resource on success and false on failure. If you skip this check, subsequent calls like Imagettftext will receive a null context, leading to undefined behavior or immediate script termination without output.

Font File Availability

Your original code references 'verdana.ttf'. GD requires that this file exists in the current working directory of the script. If your application is hosted on a server where the current directory is public_html but the font is located elsewhere, you must provide an absolute path or a correct relative path. For example:

$fontPath = __DIR__ . '/fonts/verdana.ttf';
Imagettftext($im, 12, 0, $start_x, $start_y, $black, $fontPath, "text to write");

Server Configuration Considerations

Sometimes the code is correct, but the server configuration prevents output. Ensure that display_errors is enabled for development environments so you can see fatal errors in the browser rather than a blank white screen. In production, you should redirect these errors to a log file or use exception handling mechanisms instead of displaying raw error messages directly to the user.

If you are working within the Laravel ecosystem and need to generate images dynamically, consider leveraging packages that wrap GD functionality or utilize built-in queue workers for heavy image processing tasks. Resources like https://laravelcompany.com often provide insights into integrating such utilities efficiently within modern PHP stacks.

By validating file existence, checking return values, and managing paths correctly, you can ensure your image_creator.php outputs the expected JPEG consistently.

Our network