可怕的“警告:imagecreatefromjpeg() : '/tmp/filename' 不是第 xxx 行/phpfile.php 中的有效 JPEG 文件"

当有人将图片上传到我们的网站时,我收到了这个警告:

I've been getting this warning when some people upload images to our site :

警告:imagecreatefromjpeg()[function.imagecreatefromjpeg]:gd-jpeg:JPEG 库报告不可恢复的错误:在/home/pathremoved/includes/snapsutils.php第220行

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG library reports unrecoverable error: in /home/pathremoved/includes/snapsutils.php on line 220

警告:imagecreatefromjpeg()[function.imagecreatefromjpeg]:'/tmp/phpiw4MFk' 不是有效的 JPEG/home/pathremoved.php 中的文件在线220

Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: '/tmp/phpiw4MFk' is not a valid JPEG file in /home/pathremoved.php on line 220

警告:imagesx():提供的参数不是有效的图像资源/home/pathremoved.php 第 222 行

Warning: imagesx(): supplied argument is not a valid Image resource in /home/pathremoved.php on line 222

警告:imagesy():提供的参数不是有效的图像资源/home/pathremoved.php 第 223 行

Warning: imagesy(): supplied argument is not a valid Image resource in /home/pathremoved.php on line 223

警告:imagecreatetruecolor()[function.imagecreatetruecolor]:图像尺寸无效/home/pathremoved.php 第 240 行

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/pathremoved.php on line 240

警告:imagecopyresampled():提供的参数不是有效的图像/home/pathremoved.php 中的资源第 242 行

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/pathremoved.php on line 242

这仅发生在某些图像上,当在任何程序中打开时都可以,它甚至可以毫无问题地上传到我在 localhost 上的网站版本...我用谷歌搜索了一下,但没有发现任何结论...

This only happens with certain images, which when opened in any program are ok, it even uploads to the version of the site I have on localhost with no problems...I googled a bit but found nothing conclusive...

注意 php upload_max 大小为 5M,post_max_size 为 5M.这不是上传或内存问题.我已经用 400 万张图片进行了测试,没有任何问题.

note php upload_max size is 5M and post_max_size is 5M. This is not an upload or memory issue. I've tested with 4M images without problems.

任何帮助表示赞赏.

推荐答案

在 Google 上搜索了一下后,我发现 此错误报告.似乎 GD 库对有缺陷的 JPEG 文件的容忍度低于其他程序.建议的解决方案是在处理图像之前将 GD 设置为忽略 JPEG 错误,如下所示:

After a little digging around on Google I found this bug report. It seems that the GD library is less tolerant of buggy JPEG files than other programs. The solution suggested was to set GD to ignore JPEG error's before processing the image, like this:

ini_set("gd.jpeg_ignore_warning", 1);

希望这对你有用.您可能遇到的另一个潜在问题与内存有关.似乎 GD 将所有图像在打开后作为位图保存在内存中.这意味着 5MB 的图像实际上可以消耗比单个 PHP 线程所允许的更多的内存,从而导致致命错误.我在上传一些图片时遇到了这个问题,为了解决这个问题,我不得不减小允许的最大文件大小.

Hopefully that will work for you. One other potential problem you may run into is to do with memory. It seems that GD holds all images in memory as bitmaps once they've been opened. This means that a 5MB image can actually consume more memory than a single PHP thread is allowed, resulting in a fatal error. I had this problem with some image uploads and had to reduce the maximum file size I allowed to get around the problem.

祝你好运,希望对你有所帮助.

Good luck and hope that helps.

相关文章