Unable to get the size of the image [./res/logo.gif]
I have just implemented the html2pdf and found this error when try exampl00.
Html2Pdf Error [2] Unable to get the size of the image [./res/logo.gif] File: C:\xampp\htdocs\learn_ci\ksi_gentelella\html2pdf\vendor\spipu\html2pdf\src\Html2Pdf.php Line: 1420 Image Src: ./res/logo.gif
Hi! Try to put the path in absolute, relative path may locate your file based on the Html2PDF path.
Hello: I had the same problem with images. But in my case i need load an autogenerated image like this img src="http://localhost/citadel/php/backend/dal/utilidades/dalGraficaCircular.class.php?identidad=7" style="width: 100mm;"
With static images i don't get any mistake, but on this case i got the same: Unable to get the size of the image [http://localhost/citadel/php/backend/dal/utilidades/dalGraficaCircular.class.php?identidad=7]
Hi,
I've got the same issue using with twig:
<img src="{{ app.request.scheme ~ '://' ~ app.request.httpHost ~ asset('bundles/whitelabelbackoffice/images/logo/up.jpg') }}" alt="logo_header" />
Looking for help since 1 week !!
I've added width height attribute but it does not work.
I,
I've the same issue. if it helps I noticed the problem on windows with IIS. On my Macbook with apache I do not have a problem.
Is it a problem of right to read the image?
sebast1
Edit : I think it's a issue with the twig method : absolute_url(). In my case, I use a specific url for my dev and I add this in host file on my Macbook pro. I did this to avoid typing the IP address of the machine every time. But on server with windows this custom url does not exist so the image is not found. I test that.
This error can also happen on an https dev domain using a self signed certificate, because getimagesize can't verify the self signed cert.
the image path can be:
- an absolute path
- a relative path (from the main php script you launch)
- a valid http or https url (for https, it will not work with a self signed cert)
Be carefull, absolute path on windows can produce some pb... use a linux server :p windows is bad for php
I am getting the same issue on both a windows server and a linux box. The path is set o absolute and is definitely correct on both. Any ideas how to resolve this?
The issue is the {{ asset('...')}} in twig. I changed the image to a absolute URL and the image error went away.
I am getting "Unable to get the size of the image" ! Despite turning allow_url_fopen on in PHP.ini and using a absolute URL with https. Infact the URL (image) in the error works when I open it in a new tab but this script just throws this error.
I was having the same issue. The way I fixed it was to use a relative URL: 'assets/img/image.png' instead of the absolute URL 'https://www.domain.com/assets/img/image.png'. Hope this helps!
I have also the same issue:
Unable to get the size of the image [http://mydomain/attachments/no-image-icon.png]
Same issue, it could be bypassed if base64 encoded data was managed for backgroud image as it s do for the image tag but its not the case in the code.
If you are Symfony user and you manage images with a controler, dont forget to check your firewall for allow html2pdf to go to the image url without to be authenticated.
I had the same problem. My environment: Symfony 5, PHP built-in web server, Ubuntu 20.04.
I solved the error by building the image src as follows: $ _SERVER ['DOCUMENT_ROOT']. 'my-images-dir/img.png'
I do not know if this has drawbacks, but the truth is that the other proposed solutions did not work for me.
I had the same problem and solved it. my virtual host was configured with forced https redirection with a non valid SSL but my image url was not https but http, so when the library load the image on http, my virtualhost redirects on non vali https, so it does not load. So I had to remove https redirection on my virtual host, restart apache2 server, clear browser cache then Boommmmmmmmmmmmmmm! everything went just perfect.
hope this might help!
For anyone else using this with an old system dockerized for local development: @spipu says image paths must be
- a valid http or https url (for https, it will not work with a self signed cert)
He didn't elaborate but the limitation here is the php native function getimagesize(). Unlike file_get_contents() and fopen() it can't be forced to ignore bad certificate information. The only workaround for local development is to create your own CA certificate, generate your sites local certificates from that, then update the php config to add a openssl.cafile=\path\to\rootCA.crt directive pointing to the root CA. This makes the local php install accept the bogus certificate so getimagesize won't crash html2pdf.
For people who use an absolute URL:
You can add this line:
ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)');
in vendor/spipu/html2pdf/src/Html2Pdf.php in line : 1520 in function _drawImage before $infos = @getimagesize($src);
This avoids type errors: HTTP request failed! HTTP/1.1 403 Forbidden
This is not a best practice, but until @spipu makes a fix...
My absolute path solution of 2019 no longer works. After reading @mzekukule87 suggestion, I did some testing based on the code in _drawImage().
Rather than changing the src files, I created a helper function to encode the image file and serve that as $src :
/**
* This function serves an asset as a base64 encoded data URL.
*
* @param string $file The absolute path to the asset file.
*
* @return string The base64 encoded data URL representing the asset, including its MIME type.
*/
function serveAsset(string $file): string
{
$filename = '/path/to/' . $file; // the absolute path to the file
$imageSize = getimagesize($filename);
$dataType = $imageSize['mime'];
$encodedImg = base64_encode(file_get_contents($filename));
return 'data:' . $dataType . ';base64,' . $encodedImg;
}
So <img src="<?= serveAsste($filename) ?>"> worked for me.
It also worked for a watermark background: <page backimg="<?= serveAsset($background) ?>">