簡體   English   中英

PHP - “FPDF 錯誤:不是 PNG 文件”,但圖像是

[英]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 並重新上傳時,錯誤再次出現。

以下是我在嘗試解決問題時采取的一些步驟:

  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,然后重復此過程來解決此問題。

錯誤消息指示文件的前八個字節有問題(“ 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM