TCPDF error :Unable to get the size of the image

This might be due to filesize() failing to stat() the remote image file via the HTTP wrapper (since the wrapper doesn't support it).

According to the TCPDF image() method documentation you can pass the image data in directly by prepending it with an @ symbol. So you could get the raw image data and then pass it to TCPDF like so:

$img = file_get_contents('http://example.com/wp-content/themes/example/map_image_leasing.php/?city=Calgary&suit_type=&min_area=&max_area=');

$pdf->Image('@' . $img, 55, 19, '', '', 'JPG', '', 'T', false, 300, '', false, false, 0, false, false, false);

Note that I haven't tested this (and the TCPDF documentation is sparse) so you might need to experiment a little to get it to work correctly.


Edit:

This is a fully working example (on my PC). Use this to test if you can successfully retrieve the image and output the PDF to your browser. Of course you'll need to set a known valid path for the image!

<?php

require './tcpdf/tcpdf.php';

$pdf = new TCPDF();

$pdf->AddPage();

$img = file_get_contents('http://path/to/your.jpg');
$pdf->Image('@' . $img);

$pdf->Output();

?>

Confirm that the server is able to use PHP's file_get_contents or cURL to download the file. "Unable to get the size of the image" is the first error in the Image function that TCPDF will throw if the file is unaccessible to both of those functions on the server.


I had this error on my Magento store.

If you open tcpdf.php you will find this code, $file was a url when it should be jut the path to the file:

// check if is a local file
if (!@file_exists($file)) {
        // try to encode spaces on filename
        $tfile = str_replace(' ', '%20', $file);

For a quick fix I added this code:

$file = str_replace("http://theurliwantgone/","",$tfile);

and it worked! Hope this helps most of you!


To debug this issue you may delete the @ from @getimagesize($file) in tcpdf.php around line 6850. Search for [Image] Unable to get the size of the image: and scroll some lines up. The @ hides the actual error message.

If you are able to reach the image url from the browser, it may is, that your system does not point the url to the requested host. The related message is getimagesize(): php_network_getaddresses: getaddrinfo failed:. That means, that your local php configuration has no idea where to search for the url. In that case you need to alter your /etc/hosts file and point the local setup to the urls ip. This often is an issue on localhost setups.

E.g. 127.0.0.1 yoururlhere.local