Gm: resize image if it is larger than 1000 pixels

I am trying to do some image manipulation through gm in collectionFS , because I need to read the stream and write it back to the same file, I have to use a temporary file - as shown below.

I want to check if the image exceeds 1000 pixels. In this case, it must be changed to 1000 pixels.

Unfortunately, this does not work, because I got TypeError: Object [object Object] has no method 'pipe' errors TypeError: Object [object Object] has no method 'pipe' and Error: gm().stream() or gm().write() with a non-readable stream.

 var fs = Npm.require('fs'), file = Images.findOne({ _id: fileId }), read = file.createReadStream('public'), filename = '/tmp/gm_' + Date.now(), temp = fs.createWriteStream(filename); if (method == 'resize') { // resize to 1000px, if image is bigger gmread = gm(read); gmread.size(function(err, size){ if(size.width > 1000) { gmread.resize('1000').stream(); } }); } gmread.on('end', Meteor.bindEnvironment(function (error, result) { if (error) console.warn(error); var tmpread = fs.createReadStream(filename); write = file.createWriteStream('public'); tmpread.on('end', Meteor.bindEnvironment(function (error, result) { if (error) console.warn(error); })); tmpread.pipe(write); })); gmread.pipe(temp); 
+6
source share
2 answers

I think the correct way in the FS collection is something like this:

 var gmread = gm(readStream, fileObj.name()); gmread.size({bufferStream: true}, FS.Utility.safeCallback(function (error, size) { if (error) console.warn(error); else { if(size.width > 1000) gmread.resize('1000').stream().pipe(writeStream); } })); 

Mybe you want to put this in a transformWrite function for your store ...

+2
source

You can let GraphicsMagick run the test. Try

 gmread.resize('1000x50000>'); 

instead

 gmread.size(function(err, size){ if(size.width > 1000) { gmread.resize('1000').stream(); } 

See the GraphicsMagick "geometry" page.

0
source

All Articles