weixin_39847732
weixin_39847732
2021-01-11 09:15

"Premature end of JPEG file" error.

Hello,

Attempting to use Imagemin in a node module (RWI) and it's throwing the following error:


/Users/haydenbleasel/Projects/rwi/index.js:62
                    throw error;
                          ^
Error: Premature end of JPEG file

    at Socket.<anonymous> (/Users/haydenbleasel/Projects/rwi/node_modules/imagemin/node_modules/imagemin-jpegtran/index.js:51:7)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:764:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:426:10)
    at emitReadable (_stream_readable.js:422:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Socket.Readable.push (_stream_readable.js:127:10)
    at Pipe.onread (net.js:528:21)
npm ERR! Test failed.  See above for more details.
npm ERR! not ok code 0
</anonymous></anonymous>

Occurs with every .jpg image I give it. Also, using the latest dependencies and Node.js.

该提问来源于开源项目:imagemin/imagemin

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

8条回答

  • weixin_39893893 weixin_39893893 3月前

    The file is incomplete. Could you give me an example image to test?

    点赞 评论 复制链接分享
  • weixin_39847732 weixin_39847732 3月前

    I did think so initially but as I mentioned, it happened with every .jpg that I tested. For example, this image from Unsplash.

    点赞 评论 复制链接分享
  • weixin_39893893 weixin_39893893 3月前

    That image works fine for me running directly through imagemin (although it made it larger, lol). It's probably a problem somewhere in your code.

    点赞 评论 复制链接分享
  • weixin_39847732 weixin_39847732 3月前

    Yep, that image breaks on my config. So assuming the image isn't actually corrupted, it's either a problem with my setup or perhaps a weird edge case in Imagemin. Any ideas on what to try?

    点赞 评论 复制链接分享
  • weixin_39893893 weixin_39893893 3月前

    I would assume it's nothing wrong with your setup or imagemin (since it's working fine for me) but rather somewhere in the RWI code.

    点赞 评论 复制链接分享
  • weixin_39847732 weixin_39847732 3月前

    Couldn't find any noticeable bugs in my code, so I created a bunch of basic square .jpg images for testing: 500px, 1000px, 1500px and 2000px. Only the 500px and 1000px ones worked without exploding. Seeing as the images that exploded are the 1500px and 2000px square images and the Unsplash image which are all fairly large images, I think it may be a case of file size. It does work on the CLI easily though, did you test using that?

    点赞 评论 复制链接分享
  • weixin_39893893 weixin_39893893 3月前

    I tested both CLI and the programmatic way and both worked fine.

    点赞 评论 复制链接分享
  • weixin_39847732 weixin_39847732 3月前

    So I figured out, due to my sheer stupidity, that in some cases I was reading and writing the same file. My guess is that the error didn't occur with smaller files due to some race condition. Still, might be something to note - I/O in the same folder can cause this error. Thanks for your help , appreciate it :)

    点赞 评论 复制链接分享