php TCPDF 错误:无法获取图像的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27060947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TCPDF error :Unable to get the size of the image
提问by Mithun Sarker Shuvro
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
我正在使用 TCPDF 创建动态生成的 pdf 文件。在我的 pdf 文件中,图像是根据用户输入生成的,我想将该图像添加到我的 pdf 文件中。这是我的代码
$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
如果我将“ example.com/wp-content/themes/example/map_image_leasing.php/?city=Calgary&suit_type=&min_area=&max_area=”粘贴到我的网址上,这将根据我的需要创建图像,但是如果放置此网址,则不会工作。它说无法获取图像的大小
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 .
它可以成功生成带有该图像的 pdf。
How can I solve it ?
我该如何解决?
I have visited the following stackoverflow link , but none of this came to any help
我访问了以下 stackoverflow 链接,但这些都没有帮助
tcpdf 在本地主机上工作,但不在我的服务器上,给出错误 TCPDF 错误:[图像] 无法获取图像:
cakephp tcpdf image error [Image] Unable to get image
cakephp tcpdf 图像错误 [图像] 无法获取图像
回答by timclutton
This might be due to filesize()
failing to stat()
the remote image file via the HTTP wrapper(since the wrapper doesn't support it).
这可能是由于filesize()
无法stat()
通过HTTP 包装器访问远程图像文件(因为包装器不支持它)。
According to the TCPDF image() method documentationyou 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:
根据TCPDF image() 方法文档,您可以通过在图像数据前面加上@
符号来直接传递图像数据。因此,您可以获取原始图像数据,然后将其传递给 TCPDF,如下所示:
$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.
请注意,我尚未对此进行测试(并且 TCPDF 文档很少),因此您可能需要进行一些试验才能使其正常工作。
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!
这是一个完整的示例(在我的 PC 上)。使用它来测试您是否可以成功检索图像并将 PDF 输出到您的浏览器。当然,您需要为图像设置一个已知的有效路径!
<?php
require './tcpdf/tcpdf.php';
$pdf = new TCPDF();
$pdf->AddPage();
$img = file_get_contents('http://path/to/your.jpg');
$pdf->Image('@' . $img);
$pdf->Output();
?>
回答by Sean Fahey
Confirm that the server is able to use PHP's file_get_contents
or cURLto 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.
确认服务器能够使用 PHPfile_get_contents
或cURL下载文件。Image
如果服务器上的这两个函数都无法访问文件,则 TCPDF 将抛出的函数中的第一个错误是“无法获取图像的大小” 。
回答by Mathias Methner
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.
要调试此问题,您可以从 tcpdf.php 中 6850 行附近的 @getimagesize($file) 中删除 @。搜索[Image] Unable to get the size of the image:并向上滚动一些行。@ 隐藏了实际的错误信息。
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.
如果您能够从浏览器访问图像 url,则可能是您的系统没有将 url 指向请求的主机。相关消息是getimagesize(): php_network_getaddresses: getaddrinfo failed:。这意味着,您的本地 php 配置不知道在哪里搜索 url。在这种情况下,您需要更改 /etc/hosts 文件并将本地设置指向 urls ip。这通常是本地主机设置的问题。
E.g. 127.0.0.1 yoururlhere.local
例如127.0.0.1 yoururlhere.local
回答by Justo Salcedo
Make sure to use a relative path, some times absolutely path don't work
确保使用相对路径,有时绝对路径不起作用
ok: "../../myImage.png"
好的:“../../myImage.png”
回答by Elvin Ragip
I had this error on my Magento store.
我的 Magento 商店出现此错误。
If you open tcpdf.php
you will find this code, $file was a url when it should be jut the path to the file:
如果你打开tcpdf.php
你会发现这个代码,$file 是一个 url,它应该是文件的路径:
// 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!
它奏效了!希望这对你们大多数人有帮助!
回答by Ukuser32
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!
奇怪的是(在 2019 年)我发现我必须删除 ! include/tcpdf_static.php 中的第 1924 行。出于某种原因,它只会查看ini_get('allow_url_fopen')
它是否为假,但我的服务器设置为真 - 所以修改了代码并且它工作正常。这也是以前工作但突然停止!
回答by Hennie van Rooyen
Search around line 6877 in tcpdf.php
在 tcpdf.php 中搜索第 6877 行
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);
}
}