简体   繁体   中英

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

I am getting the error as mentioned on the title when I try to run my php code which will generate a PDF file. This is the current code I am using:

 $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();

My images are currently stored in the images folder and I have converted the images file type to "File" by using these codes:

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

        }
    }

I believed the problem lies with the file type. Is there any way allow FPDF to understand the file type?

The instructions in the error message are quite clear but I'll try to explain them with another words since you're finding some difficulties. The Image() function has a type parameter described this way:

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

For instance, if the picture is a GIF you need to type 'GIF' (don't forget the quotes). The following example is provided:

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

But you call the function this way:

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

You've left the type empty, so FPDF (as documented) will try to guess the image type from the file extension. The extension is the trailing part of the file name after the dot. For instance, if the file is called kitten.jpg then the extension is jpg and FPDF will assume it's a JPEG picture. The following example is provided:

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

Back to your code, I have no way to know what $image or $newFileName contain (you've managed to omit all the relevant code) but, given the error message, I'd say it doesn't end with a file extension that FPDF can recognise; it probably doesn't even have an extension at all. So you need to either append the file extension to the file name or store the file type anywhere else (eg a database table). You could also use heuristics to find out the image type but I don't think it's worth the effort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM