2014-11-21 60 views
6

我正在使用TCPDF創建動態生成的pdf文件。在我的pdf文件中,圖像是基於用戶輸入生成的,我想將該圖像添加到我的pdf文件中。這裏是我的代碼TCPDF錯誤:無法獲取圖像的大小

$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_image_leasing.php/?city=Calgary & suit_type = & min_area = & max_area =」 這對我網址這個創建圖像,因爲我想要的,但如果把這個網址,它不起作用。它說無法獲取圖像

的大小,但如果我把這樣的事情

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

它可以成功地生成與圖像的PDF。

我該如何解決?

我已經訪問了以下計算器鏈接,但這些都不來幫忙

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

回答

10

這可能是由於filesize()未能stat()遠程圖像文件通過HTTP wrapper(因爲包裝不支持它)。

根據TCPDF image() method documentation,您可以直接傳遞圖像數據,前面加上@符號。所以,你可以得到原始圖像數據,然後將它傳遞給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(); 

?> 
4

確認服務器能夠使用PHP的file_get_contentscURL下載該文件。 「無法獲取圖像的大小」是Image函數中的第一個錯誤,如果該文件無法訪問服務器上的這兩個函數,TCPDF將拋出該錯誤。

1

確保使用相對路徑,有時候絕對路徑不工作

OK:」 ../../myImage。PNG「

錯誤:」 http://www.example.com/myImage.png

+0

您的解決方案爲我工作,我不知道爲什麼?運行phpunit測試時出現這個錯誤 – 2016-06-11 14:25:13

0

我有這個錯誤在我的Magento商店

如果你打開tcpdf.php你會發現這個代碼,$文件是一個URL時,應該突出部分的。文件路徑:

// check if is a local file 
if ([email protected]_exists($file)) { 
     // try to encode spaces on filename 
     $tfile = str_replace(' ', '%20', $file); 

快速修復我添加以下代碼:

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

它工作!希望這對大多數人有幫助!

2

要調試這個問題,你可以在tcpdf.php刪除@getimagesize($文件)的@線周圍6850搜索[圖片]無法獲取圖像的大小:和滾動一些排隊。 @隱藏實際的錯誤信息。

如果您能夠從瀏覽器訪問圖片網址,則可能是您的系統沒有將網址指向請求的主機。相關消息是getimagesize():php_network_getaddresses:getaddrinfo失敗:。這意味着,你的本地PHP配置不知道在哪裏搜索網址。在這種情況下,您需要更改/ etc/hosts文件並將本地設置指向urls ip。這通常是本地主機設置的問題。

E.g.圍繞線6877 127.0.0.1 yoururlhere.local

+0

我試了一下,得到了很多警告。但你是對的,我可以從瀏覽器訪問圖像的網址。 – 2017-05-16 10:29:37

-3

搜索在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); 
     } 
    } 

更改爲:

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

兩個框中的文字相同嗎? – leiavoia 2018-03-05 19:45:41