繁体   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