Php TCPDF错误:无法获取图像的大小

Php TCPDF错误:无法获取图像的大小,php,image,tcpdf,Php,Image,Tcpdf,我正在使用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, '', '',

我正在使用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);
如果我将“example.com/wp-content/themes/example/map\u-image\u-leasing.php/?city=Calgary&suit\u-type=&min\u-area=&max\u-area=”粘贴到我的url上,这将创建我想要的图像,但如果放置此url,则无法工作。它表示,无法获取图像的大小

但是如果我放了这样的东西

$map_image = '/wp-content/themes/v3/resources/images/public/logo_side.jpg';
它可以成功地生成带有该图像的pdf

我怎样才能解决它

我访问了下面的stackoverflow链接,但这些都没有任何帮助

这可能是由于未能通过访问远程映像文件(因为包装器不支持它)

根据,您可以通过在图像数据前面加上
@
符号,直接将图像数据传入。因此,您可以获取原始图像数据,然后将其传递给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);
请注意,我还没有对此进行测试(而且TCPDF文档很少),因此您可能需要进行一些实验,以使其正常工作


编辑:

这是一个完全有效的示例(在我的电脑上)。使用此选项测试是否可以成功检索图像并将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();

?>


确认服务器能够使用PHP或下载文件。“无法获取图像大小”是
图像
函数中的第一个错误,如果服务器上的两个函数都无法访问文件,TCPDF将抛出该错误。

请确保使用相对路径,有时绝对路径不起作用

确定:“../../myImage.png”


错误:“

我在我的Magento存储上有此错误

如果打开
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);
为了快速修复,我添加了以下代码:

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

成功了!希望这对你们大多数人都有帮助

要调试此问题,您可以删除tcpdf.php第6850行附近的@from@getimagesize($file)。搜索[图像]无法获取图像的大小:并向上滚动一些行。@隐藏了实际的错误消息

如果您能够从浏览器访问图像url,则可能是系统没有将url指向请求的主机。相关消息是getimagesize():php\u network\u getaddresses:getaddrinfo失败:。这意味着,您的本地php配置不知道在哪里搜索url。在这种情况下,您需要更改/etc/hosts文件,并将本地设置指向URL ip。这通常是本地主机设置中的一个问题


例如,127.0.0.1 yoururlhere.local

在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);
        }
    }
改为:

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

奇怪的是(2019年),我发现我必须移除该组件!include/tcpdf_static.php中第1924行的。出于某种原因,如果它为false,但我的服务器设置为true,那么它只会查看
ini\u get('allow\u url\u fopen')
——因此修改了代码,效果很好。这也是以前的工作,但突然停止

我的代码做的几乎相同:

$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, '');
$img\u base64\u encoded=$values[“image\u field\u in\u DB”];
$imageContent=file\u get\u contents($img\u base64\u encoded);
$my_file=$_服务器['DOCUMENT_ROOT']。'tmp/image.png';
$handle=fopen($my_文件,'w')或die($my_文件:');
fwrite($handle,$imageContent);
$img='';
$pdf->writeHTML($img,true,false,true,false,”);

你的解决方案对我有效,我想知道为什么?我在运行phpunit testI时遇到了这个错误,我尝试了它并得到了很多警告。但你是对的,我可以从浏览器访问图像的url。两个框中的文本相同?@leiavoia第一行不同(
FALSE
,第一行,
TRUE
),我在PLESK更新(2020年)后遇到了这个问题。TCPDF在其图像函数中不再接受URL。阿尔索