当我尝试运行将生成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可以识别的文件扩展名结束;它可能根本没有扩展。因此,您需要将文件扩展名附加到文件名,或者将文件类型存储在其他任何位置(例如数据库表)。你也可以使用启发法来找出图像类型,但我认为这不值得。