PHP - "FPDF错误:不是PNG文件"但图像是


PHP - "FPDF error: Not a PNG file", but the image is

我目前遇到了一个非常奇怪的问题。我发现一个类似的问题没有答案:不是PNG文件在FPDF。我通过浏览器将图像上传到文件服务器,然后将其拉入fpdf报告中。当这个图像是png时,我得到错误:"FPDF错误:不是png文件"。当上传的图像是jpg格式时,我没有得到任何错误。这个问题似乎是在几周前一夜之间出现的。

更奇怪的是,它只发生在新的png上传时。报告中有一个png正在生成fine。当我从系统中下载png并重新上传时,错误又出现了。

下面是我在尝试解决这个问题时所采取的一些步骤:

  1. 我已经确保图像实际上是png(通过它的属性)。
  2. 我保存图像的方式没有改变,但这里是代码:

    $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;
    
  3. 我已经检查了,看看他们是否有任何改变PNG格式或FPDF更新,没有运气。
  4. 我已经通过gimp将jpg转换为png。
  5. 通过gimp将png格式的文件转换为jpg格式,然后将jpg格式的文件上传到系统,不会产生任何错误。

解决方法-我已经继续并将png的格式转换为jpg的保存,而不是重新编码的图像。谢谢你的帮助。

手动更改图片格式为JPG,然后重复此过程。

错误信息表明文件的前8个字节("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(或paint.net在windows下)重新编码为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();
        }
    }
}
相关文章: