looping through folder of images and resizing using node gm

87 views Asked by At

this code leads to just the a single image being written to output(6175710.jpg). Instead I would like all three. What is incorrect in my code. Im using node gm module. see my implementation:

  fs.readdir(__dirname + '/../images/master/', function(err,contents) {
    if (err) throw err;

    for (var i = 0; i < contents.length; i++) {
        var source = contents[i];
        console.warn('Source: ', source)
        gm(__dirname + '/../images/master/' + source)
          .resize(1280)
          .crop(1280,720,0,0)
          .toBuffer('jpg',function (err, buffer) {
          if (err) console.warn(err);
            console.warn('Source2: ', source)
            gm(buffer)
            .geometry('+0+0')
            .composite(__dirname + '/../public/images/Header.jpg')
            .write(__dirname + '/../images/ready/' + source, function (err) {
            if (err) {console.warn(err)}
              console.warn('done saving')
              console.warn('Source3: ', source)
          })
            })
    }
});

However my console out shows this:

Source:  2620590.jpg
Source:  4720702.jpg
Source:  6175710.jpg
Source2:  6175710.jpg
Source2:  6175710.jpg
Source2:  6175710.jpg
done saving
Source3:  6175710.jpg
done saving
Source3:  6175710.jpg
done saving
Source3:  6175710.jpg

UPDATE: I solved it using a Filehound lib.

0

There are 0 answers