TCPDF error :Unable to get the size of the image

TCPDF error :Unable to get the size of the image

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

Stefan Bogdanescu

Founder & Senior Architect

TCPDF Error :Unable to get the size of the image

When working with TCPDF, encountering the error [Image] Unable to get the size of the image is a common frustration, but it almost always stems from a misunderstanding of how PHP handles dynamic content versus static file paths. The specific scenario you described—where a direct URL to an external image works in a browser but fails inside TCPDF while a local relative path succeeds—highlights a critical distinction between fetching data and embedding files.

The core issue lies in the difference between a remote URL and a local file system path. In your working example, $map_image points to /wp-content/themes/v3/resources/images/public/logo_side.jpg. This is a relative path resolved against your web root. TCPDF can read this because PHP can access the file directly on the server's filesystem using fopen() or imagecreatefromjpeg(). The library opens the file, reads its headers to determine dimensions, and embeds it into the PDF stream.

However, when you switch $map_image to https://example.com/wp-content/themes/example/map_image_leasing.php/?city=Calgary, you are passing a remote URL to TCPDF. While modern versions of TCPDF (specifically those using GD or Imagick) can handle remote URLs via cURL or the file stream wrapper, there are specific conditions where this fails. If the server running your PHP application cannot resolve the example.com domain, lacks outbound network connectivity to that host, or if the request times out, TCPDF throws the "Unable to get the size" error immediately because it never successfully opened the image stream to read its metadata.

Furthermore, there is a subtle but often overlooked aspect: Content-Type validation and HTTP redirects. If the URL you provided generates an image by executing PHP (map_image_leasing.php), the server might be returning the image with a Content-Type header of text/html or failing to set the correct binary stream before TCPDF reads it. TCPDF expects a raw binary image stream. If the remote endpoint returns HTML, JSON, or an error page, the library cannot parse the dimensions and aborts with that specific error message.

To resolve this without relying on external network calls (which can be flaky in production environments), the most robust solution is to fetch the image content into a temporary file before passing it to TCPDF. This decouples the PDF generation logic from network latency or remote server failures.

Here is a pattern that ensures reliability:

$map_image_url = "https://example.com/wp-content/themes/example/map_image_leasing.php/?city=Calgary";

// Fetch the image content first
$image_content = file_get_contents($map_image_url);

if ($image_content !== false) {
    // Create a temporary file to store the fetched image
    $temp_file = tempnam(sys_get_temp_dir(), 'tcpdf_');
    
    if (move_uploaded_file(
        fopen('php://temp', 'r+') === false) { 
        // If you need to handle specific headers or redirects, use cURL instead of file_get_contents
    } else {
        $file_pointer = fopen($temp_file, 'w+');
        fwrite($file_pointer, $image_content);
        fclose($file_pointer);

        // Now pass the local path to TCPDF
        $pdf->Image(
            $temp_file, 
            55, 
            19, 
            '', 
            '', 
            'JPG', 
            '', 
            'T', 
            false, 
            300, 
            '', 
            false, 
            false, 
            0, 
            false, 
            false
        );

        // Clean up the temporary file after PDF generation
        unlink($temp_file);
    }
} else {
    // Handle the case where the image could not be fetched
    $error_message = "Failed to fetch image from URL";
}

If you must use file_get_contents, ensure that the remote server sends a proper 200 OK status code with a binary image stream. If the endpoint requires specific headers or handles redirects, switching to cURL is often safer:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $map_image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$image_data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($http_code === 200 && $image_data !== false) {
    // Proceed with saving to temp file and passing to TCPDF as shown above
}

By fetching the image data first and storing it in a temporary location, you guarantee that TCPDF is always working with a local path. This approach avoids potential issues with PHP stream wrappers rejecting remote URLs due to allow_url_fopen restrictions or firewall blocks on your server. It also allows you to validate the HTTP response code before attempting to generate the PDF, providing better error handling than relying solely on TCPDF's internal exception.

Our network