簡體   English   中英

FPDF錯誤:圖像文件沒有擴展名且未指定類型

[英]FPDF error: Image file has no extension and no type was specified

當我嘗試運行將生成PDF文件的php代碼時,出現標題上提到的錯誤。 這是我正在使用的當前代碼:

 $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理解文件類型?

錯誤消息中的說明非常清楚,但是由於您遇到了一些困難,因此我將嘗試用另一句話來解釋它們。 Image()函數具有這樣描述的type參數:

圖像格式。 可能的值是(不區分大小寫):JPG,JPEG,PNG和GIF。 如果未指定,則從文件擴展名推斷類型。

例如,如果圖片是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包含什么(您已經設法省略了所有相關代碼),但是鑒於錯誤消息,我會說它沒有以文件擴展名結尾FPDF可以識別; 它甚至可能根本沒有擴展。 因此,您需要在文件名后附加文件擴展名, 將文件類型存儲在其他任何位置(例如數據庫表)。 您也可以使用啟發式方法找出圖像類型,但我認為這樣做不值得。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM