2013-08-19 50 views
2

當我嘗試運行將生成PDF文件的php代碼時,我收到了標題中提到的錯誤。這是當前代碼我使用:FPDF錯誤:圖像文件沒有擴展名,也沒有指定類型

$pdf = new PDF(); 
$pdf->AliasNbPages(); 
$pdf->AddPage(); 
$pdf->SetFont('Times','',12); 

foreach($inventories as $key => $inventories) : 

    $image = $inventories['image']; 
    $resourceID = $inventories['resourceID']; 
    $learningcentre = $inventories['learningcentre']; 
    $title = $inventories['title']; 
    $quantity = $inventories['quantity']; 
    $description = $inventories['description']; 

    $html= 'Resource ID: '. $resourceID. '<br>Title: '.$title.'<br>Learning Centre: '.$learningcentre.'<br>Quantity: '.$quantity.'<br>Description: '.$description.'<br><br>'; 
    $pdf->Image('images/'.$image,10,6,30); 
    $pdf->WriteHTML($html);    
endforeach; 

$pdf->Output(); 

我的圖片當前存儲在圖像文件夾中,我已經用這些代碼轉換的圖像文件類型「文件」:

$fileTypes = array(
     'image/pjpeg', 
     'image/jpeg', 
     'image/png', 
     'image/gif' 
    ); 

    // default value for unsuccessful move file 
    $successfullyMoveFile = false; 

    // the name of the input type 
    $fileInputName = 'file'; 

    // an array to store all the possible errors related to uploading a file 
    $fileErrorMessages = array(); 

    //if file is not empty 
    $uploadFile = !empty($_FILES); 

    if ($uploadFile) 
    { 
     $fileUploaded = $_FILES[$fileInputName]; 

     // if we have errors while uploading!! 
     if ($fileUploaded['error'] != UPLOAD_ERR_OK) 
     { 
      $errorCode = $fileUploaded['error']; // this could be 1, 2, 3, 4, 5, 6, or 7. 
      $fileErrorMessages['file'] = $uploadErrors[$errorCode]; 
     } 

     // now we check for file type 
     $fileTypeUploaded = $fileUploaded['type']; 

     $fileTypeNotAllowed = !in_array($fileTypeUploaded, $fileTypes); 
     if ($fileTypeNotAllowed) 
     { 
      $fileErrorMessages['file'] = 'You should upload a .jpg, .png or .gif file'; 
     } 

     // if successful, we want to copy the file to our images folder 
     if ($fileUploaded['error'] == UPLOAD_ERR_OK) 
     { 

      $successfullyMoveFile = move_uploaded_file($fileUploaded["tmp_name"], $imagesDirectory . $newFileName); 

     } 
    } 

我相信問題在於文件類型。有什麼辦法可以讓FPDF理解文件類型嗎?

回答

0

錯誤信息中的說明很清楚,但我會嘗試用另一個詞來解釋它們,因爲您發現有些困難。該Image()函數有一個type參數描述是這樣的:

Image format. Possible values are (case insensitive): JPG, JPEG, PNG and GIF. If not specified, the type is inferred from the file extension.

舉例來說,如果照片是你需要鍵入'GIF'(不要忘了引號)一個GIF。下面的例子提供:

$pdf->Image('http://chart.googleapis.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World',60,30,90,0,'PNG'); 

但是你調用該函數是這樣的:

$pdf->Image('images/'.$image,10,6,30); 

你已經離開了型空的,所以FPDF(如記錄)會嘗試猜從圖像類型文件擴展名。擴展名是點後面的文件名的後面部分。例如,如果文件名爲kitten.jpg,則擴展名爲jpg,FPDF將假定它是JPEG圖片。下面的例子提供:

$pdf->Image('logo.png',10,10,-300); 

回到你的代碼,我也沒有辦法知道$image$newFileName包含(您已經成功地省略所有相關的代碼),但提供的錯誤消息,我d說它不以FPDF可以識別的文件擴展名結尾;它甚至可能根本沒有擴展。因此,您需要將文件擴展名附加到文件名將文件類型存儲在任何其他位置(例如數據庫表)。你也可以使用啓發式來找出圖像類型,但我認爲這不值得。

+0

感謝您的回覆,並遺憾地忽略了$ image的相關代碼。問題是我的圖像文件沒有擴展名。但是,我刪除了他們的擴展名,以便我的網頁能夠打印出圖像,而不管它們以前在哪個擴展名中。是否有FPDF讀取和打印沒有擴展名的圖像? – Arastal

+0

究竟是什麼意思?像'Image()'函數中的一個可選參數,讓您指導FPDF瞭解圖像類型? –

+0

呃..我真的不知道什麼是可選參數。 http://i.imgur.com/oICdKXc.png鏈接顯示我的圖像當前存儲在的文件類型。基本上我需要允許我的PDF打印出圖像。有可能這樣做嗎? – Arastal