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 的缩写。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值