Search code examples
imagemagickgulpgulp-imagemin

Gulp: lossy recompression of images when some are broken


I have a set of ~18,000 jpg files that I need to optimize/recompress.

I tried almost every gulp image optimization plugin out there, and every plugin at some point gives an error with no suggestion on what file is the reason for it. Here's what gulp-image-resize ends with:

events.js:141
      throw er; // Unhandled 'error' event
      ^
Error: Error: Command failed: gm identify: No decode delegate for this image format (/var/folders/ns/85cnwvcx5ysb7jzr8hh_k4r80000gn/T/gmROZu8m).
gm identify: Request did not return an image.

    at finish (/Users/mvasin/Sites/process images/node_modules/gulp-gm/index.js:40:21)
    at gm. (/Users/mvasin/Sites/process images/node_modules/async/lib/async.js:485:30)
    at emitMany (events.js:108:13)
    at gm.emit (events.js:182:7)
    at gm. (/Users/mvasin/Sites/process images/node_modules/gm/lib/getters.js:70:16)
    at cb (/Users/mvasin/Sites/process images/node_modules/gm/lib/command.js:318:16)
    at ChildProcess.proc.on.onExit (/Users/mvasin/Sites/process images/node_modules/gm/lib/command.js:293:9)
    at emitTwo (events.js:87:13)
    at ChildProcess.emit (events.js:172:7)
    at maybeClose (internal/child_process.js:817:16)

Here's gulp-gm's blue screen:

events.js:141
      throw er; // Unhandled 'error' event
      ^
Error: Stream yields empty buffer
    at Socket. (/Users/mvasin/Sites/process images/node_modules/gm/lib/command.js:57:17)
    at emitNone (events.js:72:20)
    at Socket.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:893:12)
    at doNTCallback2 (node.js:429:9)
    at process._tickCallback (node.js:343:17)
a228:process images mvasin$ gulp GraphicsMagick

gulp-responsive:

events.js:141
      throw er; // Unhandled 'error' event
      ^
Error: Input buffer contains unsupported image format
    at Error (native)

gulp-sharp-resize:

Unhandled rejection Error: Input buffer contains unsupported image format
    at Error (native)

Beautiful! I'll sort through all of my 18,000 images and hopefully will figure out the one with 'unsupported image format'. Stick around, I'll be right back.
Now comes imagemin-jpeg-recompress:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: Unsupported color conversion request

    at ChildProcess. (/Users/mvasin/Sites/process images/node_modules/imagemin-jpeg-recompress/index.js:101:11)
    at emitTwo (events.js:87:13)
    at ChildProcess.emit (events.js:172:7)
    at maybeClose (internal/child_process.js:817:16)
    at Socket. (internal/child_process.js:319:11)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at Pipe._onclose (net.js:469:12)

You got the idea...

gulp-imagemin stops on error as well.

I tried to resort to desktop mac app ImageOptim (it has 'lossy' deep down in the settings), but on really huge image sets it silently stops processing sometime in the middle because of internal error.

And I'd like to preserve gulp workflow anyway.


Solution

  • You could use gulp-plumber to prevent stoping gulp task on incorrect images.

    It can also show which image causes an error.

    var gulp = require('gulp');
    var $ = require('gulp-load-plugins')();
    
    gulp.task('images', function() {
      return gulp.src('src/*.jpg')
        .pipe($.plumber())
        .pipe($.responsive({
          ...
        }))
        .pipe(gulp.dest('dist'));
    });