简体   繁体   English

FPDF错误:图像文件没有扩展名且未指定类型

[英]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. 当我尝试运行将生成PDF文件的php代码时,出现标题上提到的错误。 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? 有什么方法可以让FPDF理解文件类型?

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()函数具有这样描述的type参数:

Image format. 图像格式。 Possible values are (case insensitive): JPG, JPEG, PNG and GIF. 可能的值是(不区分大小写):JPG,JPEG,PNG和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). 例如,如果图片是GIF,则需要输入'GIF' (请不要忘记引号)。 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. 您将类型保留为空,因此FPDF(如所记录)将尝试从文件扩展名中猜测图像类型。 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. 例如,如果文件名为kitten.jpg则扩展名为jpg而FPDF将假定它是JPEG图片。 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; 回到您的代码,我无法知道$image$newFileName包含什么(您已经设法省略了所有相关代码),但是鉴于错误消息,我会说它没有以文件扩展名结尾FPDF可以识别; 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. 您也可以使用启发式方法找出图像类型,但我认为这样做不值得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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