【问题标题】:FPDF error: Image file has no extension and no type was specifiedFPDF 错误:图像文件没有扩展名并且没有指定类型
【发布时间】:2013-08-21 01:24:39
【问题描述】:

当我尝试运行将生成 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();

我的图像当前存储在 images 文件夹中,我已使用以下代码将图像文件类型转换为“文件”:

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

【问题讨论】:

    标签: php fpdf


    【解决方案1】:

    错误消息中的说明非常清楚,但我会尝试用其他词来解释它们,因为您会发现一些困难。 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 可以识别的文件扩展名结尾;它甚至可能根本没有扩展名。因此,您需要将文件扩展名附加到文件名 将文件类型存储在其他任何地方(例如数据库表)。您也可以使用启发式方法来找出图像类型,但我认为这不值得。

    【讨论】:

    • 感谢您的回复,很抱歉省略了 $image 的相关代码。问题在于我的图像文件没有扩展名。但是,我删除了他们的扩展名,以便我的网页能够打印出图像,无论他们以前在哪个扩展名中。FPDF 是否可以读取并打印出没有扩展名的图像?
    • 究竟是什么意思?类似于Image() 函数中的可选参数,可以让您向 FPDF 指示图像类型?
    • Erm.. 我真的不知道什么是可选参数。 i.imgur.com/oICdKXc.png 该链接显示了我的图像当前存储的文件类型。基本上我需要让我的 PDF 打印出图像。有可能吗?
    • 保存图片的时候为什么不保存图片的扩展名,然后读取传递给`image`函数呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 2012-10-16
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2014-06-14
    • 2012-11-13
    相关资源
    最近更新 更多