TCPDF error :Unable to get the size of the image
Asked Answered
B

8

15

I am using TCPDF to create dynamically generated pdf file . In my pdf file a image is generated based on user input and I want to add that image on my pdf file . Here is my code

 $map_image = "example.com/wp-content/themes/example/map_image_leasing.php/?city=Calgary&suit_type=&min_area=&max_area=";

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

If i paste "example.com/wp-content/themes/example/map_image_leasing.php/?city=Calgary&suit_type=&min_area=&max_area=" this on my url this create image as I wanted , but If put this url , it doesn't work . It says Unable to get the size of the image

But if I put something like this

$map_image = '/wp-content/themes/v3/resources/images/public/logo_side.jpg';

It can generate pdf with that image successfully .

How can I solve it ?

I have visited the following stackoverflow link , but none of this came to any help

tcpdf working on localhost but not on my sever giving error TCPDF ERROR: [Image] Unable to get image:

cakephp tcpdf image error [Image] Unable to get image

TCPDF ERROR: [Image] Unable to get image

Belay answered 21/11, 2014 at 12:1 Comment(0)
E
21

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();

?>
Esquivel answered 21/11, 2014 at 12:59 Comment(0)
C
5

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.

Chanterelle answered 18/1, 2015 at 4:20 Comment(1)
I've had this problem after a PLESK update (in 2020). TCPDF didn't accept urls anymore in its image functions. Also <img tags in the pdf views didn't work anymore. Every time it gave the error TCPDF ERROR: [Image] Unable to get the size of the image. When using a server path like /var/www/etc the functionality was working great. After some digging i've found that the php.ini setting: allow_url_include was set to Off instead to On. This caused the file_get_contents function not to work anymore. Changing this setting to On fixed the error message and TCPDF is working again.Hersh
B
5

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!

Banded answered 11/8, 2016 at 9:24 Comment(0)
S
4

Oddly for me (in 2019) I found that I had to remove the ! of line 1924 in include/tcpdf_static.php. For some reason it would only look at ini_get('allow_url_fopen') if it was false but my server setting was true - so amended the code and it worked fine. This also was previously working but suddenly stopped!

Sneer answered 30/4, 2019 at 14:51 Comment(2)
Lifesaver, than youMcalpine
before !ini_get('allow_url_fopen') after ini_get('allow_url_fopen') mine in line 1958 same file, and its works,, WoWBragi
B
3

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

Beak answered 3/12, 2016 at 14:59 Comment(1)
I tried it and got lots of warning. but you are right I can access the url of the image from the browser.Discommend
D
1

Make sure to use a relative path, some times absolutely path don't work

ok: "../../myImage.png"

wrong:"http://www.example.com/myImage.png"

Dermoid answered 28/1, 2016 at 8:13 Comment(1)
Your solution worked for me, I wonder why? I got this error when running phpunit testSacks
F
0

My code do almost the same:

$img_base64_encoded = $values["image_field_in_DB"];
$imageContent = file_get_contents($img_base64_encoded);

$my_file = $_SERVER['DOCUMENT_ROOT'].'tmp/image.png';
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
fwrite($handle, $imageContent);
$img = '<img src="'.$my_file.'" width="150" height="auto" alt="image"  data-default="KG" />';

$pdf->writeHTML($img, true, false, true, false, '');
Finalist answered 4/3, 2021 at 23:32 Comment(0)
M
-3

Search around line 6877 in tcpdf.php

if ($imsize === FALSE) {
        if (($w > 0) AND ($h > 0)) {
            // get measures from specified data
            $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
            $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
            $imsize = array($pw, $ph);
        } else {
            $this->Error('[Image] Unable to get the size of the image: '.$file);
        }
    }

Change to:

if ($imsize === TRUE) {
        if (($w > 0) AND ($h > 0)) {
            // get measures from specified data
            $pw = $this->getHTMLUnitToUnits($w, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
            $ph = $this->getHTMLUnitToUnits($h, 0, $this->pdfunit, true) * $this->imgscale * $this->k;
            $imsize = array($pw, $ph);
        } else {
            $this->Error('[Image] Unable to get the size of the image: '.$file);
        }
    }
Moiramoirai answered 5/1, 2018 at 0:23 Comment(2)
Same text in both boxes?Talkathon
@Talkathon The first line is different (FALSE in the first, TRUE in the second).Topheavy

© 2022 - 2024 — McMap. All rights reserved.