[英]PHP - "FPDF error: Not a PNG file", but the image is
我目前在 fpdf 方面遇到了一个非常奇怪的问题。 我发现了一个没有答案的类似问题: not a PNG file in FPDF 。 我通过浏览器将图像上传到我的文件服务器,然后拉入 fpdf 报告。 当此图像是 png 时,我收到错误消息:“FPDF 错误:不是 PNG 文件”。 当上传的图片是 jpg 时,我没有收到任何错误。 这个问题似乎是在几周前一夜之间出现的。
更奇怪的是,这只会在上传新的 png 时发生。 报告中有一个 png 生成良好。 当我从系统下载该 png 并重新上传时,错误再次出现。
以下是我在尝试解决问题时采取的一些步骤:
我保存图像的方式没有任何改变,但这是代码:
$original = $time."_".$name."_o.".$extension; $thumbnail = $time."_".$name."_t.".$extension; include('SimpleImage.php'); $image = new SimpleImage(); $image->load($_FILES['file']['tmp_name']); $image->save($A_path."images/".$original); $image->resizeToHeight(200); $image->save($A_path."images/thumbs/".$thumbnail); $photo = "images/".$original; $thumb = "images/thumbs/".$thumbnail;
变通方法 - 我已经在保存时将 png 转换为 jpg,而不是重新编码图像。 谢谢您的帮助。
通过将图片格式手动更改为JPG,然后重复此过程来解决此问题。
错误消息指示文件的前八个字节有问题(“ png签名”)。
使用“ od -c | head -1”检查前16个字节。 每个PNG文件都以以下内容开头:
211 P N G \r \n 032 \n \0 \0 \0 \r I H D R
如果愿意,请使用“ xxd file.png | head -1”,并希望看到以下内容:
0000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 .PNG........IHDR
这16个字节是PNG签名以及第一个块的长度和名称。 前8个字节是格式名称,外加换行符(换行符)和回车符,用于检测各种传输错误。 接下来的8个字节是IHDR块的开头,其长度必须为13,以4字节整数表示,名称为“ IHDR”。
有关详细信息,请参见PNG规范 。
检查图像的深度。 FPDF支持24位深度(我不确定大约32位深度),也不支持alpha通道。 我会尝试使用ImageMagick(或Windows下的paint.net)将其重新编码为png。
convert input.png -depth 8 +matte output.png
我找到了一个对我有用的 crud 解决方案,但这将在您的主机上占用更多空间。 但是您可以确定哪个扩展有效并删除其余的但是这是值得的。
首先获取文件内容并将它们转换为 base64_encode。 创建您希望文件采用“png”、“jpg”、“jpeg”格式的文件格式数组,并通过文件扩展名对 base64 图像进行解码。 这将在您的文件夹中重新创建具有三个文件扩展名的图像。
使用
try{
}catch (Exception $e) {
}
循环并找到哪个图像扩展有效并使用它。 这是我的完整代码
$base64 = base64_encode(file_get_contents("full/domain/path/to/image"));
$f_ex = array('.png', '.jpg', '.jpeg'); //array of extensions to recreate
$path = "path/to/new/images"; //this folder will have there images.
$i = 0;
$end = 3;
while ($i < $end) {
$data = base64_decode($base64); //decode the image file from base64
$filename = "unique_but_memorable_filename(eg invoice id)" . $f_ex[$i]; //$f_ex loops through the file extensions
file_put_contents($path . $filename, $data); //we save our new images to the path above
$i++;
}
在设置图像的 FPDF 中,我们遍历重新创建的图像,看看哪个有效,然后停在那里
try {
$filename = "remember_unique_but_memorable_filename(eg invoice id)" . $f_ex[0];
$logo = "your domail.com where image was stored" . '/' . $path . $filename;
$pdf->Image($logo, 10, 17, 100, 100);
//Put your code here to delete the other image formats.
} catch (Exception $e) {
try {
$filename = "remember_unique_but_memorable_filename(eg invoice id)" . $f_ex[1];
$logo = "your domail.com where image was stored" . '/' . $path . $filename;
$pdf->Image($logo, 10, 17, 100, 100);
//Put your code here to delete the other image formats.
} catch (Exception $e) {
try {
$filename = "remember_unique_but_memorable_filename(eg invoice id)" . $f_ex[2];
$logo = "your domail.com where image was stored" . '/' . $path . $filename;
$pdf->Image($logo, 10, 17, 100, 100);
//Put your code here to delete the other image formats.
} catch (Exception $e) {
//if all the three formats fail, lets see the error
echo 'Message: ' . $e->getMessage();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.