weixin_39636411
weixin_39636411
2020-12-02 19:34

0.7.0 - Warning: Command failed: Premature end of JPEG file

I'm getting the following error message using 0.7.0:

 bash
Warning: Command failed: Premature end of JPEG file

Using --force results in the following error:

 bash
Warning: Command failed: Premature end of JPEG file
     Used --force, continuing.
    Fatal error: Cannot read property 'contents' of undefined

Looking at my images I can see that the one it's failing on is corrupted, so I'm not surprised by the error. However, I would like the task to continue and just skip the image in question rather than throw a fatal error.

I've noticed this issue #155, which indicated that the problem had been resolved in 0.6.0-pre, but I'm on 0.7.0 and still getting the error.

FYI, I'm using Grunt 0.4.4.

该提问来源于开源项目:gruntjs/grunt-contrib-imagemin

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

18条回答

  • weixin_39636411 weixin_39636411 4月前

    Sure, so in my finder the corrupt image looks like this:

    screenshot 2014-05-06 11 04 17

    You can download the file here if you want to test it.

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

    Could you give me the source image? That's the optimized one, right?

    点赞 评论 复制链接分享
  • weixin_39636411 weixin_39636411 4月前

    No that's the correct source image. Corrupted source images are causing the task to fail - the task isn't making the images corrupted. Hope that makes sense?

    点赞 评论 复制链接分享
  • weixin_39885683 weixin_39885683 4月前

    Corrupted source images are causing the task to fail

    :cry:

    点赞 评论 复制链接分享
  • weixin_39636411 weixin_39636411 4月前

    Unfortunately the directory I'm using imagemin on contains user generated content. Corrupt images shouldn't happen, but this can't be guaranteed. Ideally the task would skip an image if it's corrupt.

    My 'fix' above seems to work for me, but I'm no expert and don't know if this is the right way to do it or if this modification could/should be merged into the codebase?

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

    -bailey, we can't just skip errors produced by the optimizers.

    点赞 评论 复制链接分享
  • weixin_39636411 weixin_39636411 4月前

    Hmmm, I'm not sure I understand. Shouldn't the task be able to skip over errors (i.e. unreadable files) and continue?

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

    Well, it's not the only error the binaries produces.

    点赞 评论 复制链接分享
  • weixin_39636411 weixin_39636411 4月前

    So am I right in thinking you're saying the error I'm getting could be for one of many reasons and not just because of a corrupt image? If so I understand now. I guess in my case I can continue to use my 'fix' as it's an edge case. Thanks for all your help guys, the work you do for grunt is amazing!

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

    Yep, exactly. Wouldn't be ideal to just strip all errors.

    点赞 评论 复制链接分享
  • weixin_39636411 weixin_39636411 4月前

    Unfortunately with the updated imagemin lib I'm still getting the following message:

     bash
    Warning: Command failed: Premature end of JPEG file
         Used --force, continuing.
        Fatal error: Cannot read property 'contents' of undefined
    

    I've tried with and without --force, same error.

    The error occurs when the script encounters a corrupt file. Because of the nature of the project I'm working on (i.e. user generated content), I can't guarantee that files won't be corrupted.

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

    Could you paste a example image?

    点赞 评论 复制链接分享
  • weixin_39636411 weixin_39636411 4月前

    Just in case it helps, here's my task config:

     javascript
    module.exports = {
    
        options: {
            cache: false,
            optimizationLevel: 7,
            progressive: true,
            interlaced: true,
        },
    
        dist: {
            files: [{
                expand: true,
                cwd: 'src/',
                src: ["**/*.{png,jpg,gif}"],
                dest: 'src/'
            }]
        }
    };
    

    Yes, I know my destination directory is the same as my source, that's what I wanted. I've tried it with a different destination though, and I get the same error.

    点赞 评论 复制链接分享
  • weixin_39636411 weixin_39636411 4月前

    Ok. I'm no expert, but simply adding a 'check' that skips images when they return 'undefined' seems to work. Here's my modification (added onwards from line 44 of imagemin.js):

     javascript
    imagemin.optimize(function (err, data) {
        if (typeof data === 'undefined') {
            grunt.log.writeln(chalk.red('✘ ') + file.src[0] + chalk.gray(' (Skipping corrupt file)'));
            return;
        }
    
        if (err) {
            grunt.warn(err);
        }
    
        // etc...
    

    I have no idea if this is the best way to do it, or if it's best practice? I'd love opinions on this and I'm happy to do a pull request if you want me to?

    点赞 评论 复制链接分享
  • weixin_39603609 weixin_39603609 4月前

    data should never be undefined without an error.

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

    Yeah, I'll add a check for it in imagemin. Or rather, it shouldn't read dirs, just files.

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

    Fixed in https://github.com/kevva/imagemin/commit/290eb651ab3739f15fe4b1aa1e893f0a715fd874.

    点赞 评论 复制链接分享
  • weixin_39636411 weixin_39636411 4月前

    I'm going to be doing some more testing today, so I'll let you know if that's worked.

    点赞 评论 复制链接分享