天天看點

Warning: imagecreatefromjpeg()

前一陣子手機端上傳相冊功能,總發現錯誤日志中有一些使用者上傳的圖檔檔案報錯,代碼檢查了很多次,測試也做的比較充分了,但還是會出現上傳失敗報錯的問題,很是郁悶,今天終于找到了原因和解決辦法。

報錯:

Warning: imagecreatefromjpeg(): ‘/tmp/img’ is not a valid JPEG file in /path/uploadfile.php on line 1
           

原因:PHP GD庫對image/pjpeg格式的圖檔支援不是非常完美,或者說是要求太嚴格,GD函數庫檢測發現檔案是非标準JPEG圖檔格式導緻。

image/pjpeg究竟是什麼,與image/jpeg有什麼差別?

image/pjpeg對應.jfif檔案,image/jpeg對應.jpg、.jpe、.jpeg檔案。

JFIF檔案格式(JPEG檔案交換格式,   JPEG   File   Interchonge   Format,是 progressive JPEG 的縮寫)。JFIF檔案格式隻是将一種圖像格或環繞JPEG壓縮的一種簡單方法,它們沒有其他的更多功能。

JPEG檔案格式(Joint   Photographic   Experts   Group(聯合圖像專家組))是一種有損壓縮格式,能夠将圖像壓縮在很小的儲存空間,圖像中重複或不重要的資料會被丢失,是以容易造成圖像資料的損傷。 尤其是使用過高的壓縮比例,将使最終解壓縮後恢複的圖像品質明顯降低,如果追求高品質圖像,不宜采用過高壓縮比例。

英文原文:What is progressive JPEG?

A simple or “baseline” JPEG file is stored as one top-to-bottom scan of the image. Progressive JPEG divides the file into a series of scans. The first scan shows the image at the equivalent of a very low quality setting, and therefore it takes very little space. Following scans gradually improve the quality. Each scan adds to the data already provided, so that the total storage requirement is roughly the same as for a baseline JPEG image of the same quality as the final scan. (Basically, progressive JPEG is just a rearrangement of the same data into a more complicated order.) The advantage of progressive JPEG is that if an image is being viewed on-the-fly as it is transmitted, one can see an approximation to the whole image very quickly, with gradual improvement of quality as one waits longer; this is much nicer than a slow top-to-bottom display of the image. The disadvantage is that each scan takes about the same amount of computation to display as a whole baseline JPEG file would. So progressive JPEG only makes sense if one has a decoder that’s fast compared to the communication link. (If the data arrives quickly, a progressive-JPEG decoder can adapt by skipping some display passes. Hence, those of you fortunate enough to have T1 or faster net links may not see any difference between progressive and regular JPEG; but on a modem-speed link, progressive JPEG is great.) Up until recently, there weren’t many applications in which progressive JPEG looked attractive, so it hasn’t been widely implemented. But with the popularity of World Wide Web browsers running over slow modem links, and with the ever-increasing horsepower of personal computers, progressive JPEG has become a win for WWW use. IJG’s free JPEG software (see part 2, item 15) now supports progressive JPEG, and the capability is spreading fast in WWW browsers and other programs. Except for the ability to provide progressive display, progressive JPEG and baseline JPEG are basically identical, and they work well on the same kinds of images. It is possible to convert between baseline and progressive representations of an image without any quality loss. (But specialized software is needed to do this; conversion by decompressing and recompressing is *not* lossless, due to roundoff errors.) A progressive JPEG file is not readable at all by a baseline-only JPEG decoder, so existing software will have to be upgraded before progressive JPEG can be used widely. See item 16 in part 2 for the latest news about which programs support it.

解決方法:

如果PHP版本 > 5.1.3,可以在php.ini中增加:

gd.jpeg_ignore_warning = 1
           

然後,重新開機伺服器環境。

如果不想重新開機環境的話,可以通過ini_set 修改php配置

echo ini_get('gd.jpeg_ignore_warning');
ini_set('gd.jpeg_ignore_warning',1);
echo ini_get('gd.jpeg_ignore_warning');
           

在 MIME 類型中,圖像方面有這樣兩種: image/jpeg 與 image/pjpeg ,GD庫隻認識前者的傳統格式,後者是 progressive JPEG 的縮寫。