Search code examples
imagemagickgraphicsmagickcollectionfs

CollectionFS Graphicsmagick resize width only if larger than


On collectionfs I have following method to resize large images to width of 1200px and height remains proportional in order to reduce file size.

var createPic = function(fileObj, readStream, writeStream) {
    gm(readStream, fileObj.name()).resize(1200).quality(100).autoOrient().stream().pipe(writeStream);
}

Images with lets say 2000x2000 dimension actually end up uploaded 1200x1200 but larger in file size. Can you explain why? And how can I modify the method to only resize image if width exceeds for example 2000px?


Solution

  • This works:

    gm(readStream, fileObj.name()).resize(1200, 1200, '>').quality(100).autoOrient().stream().pipe(writeStream);
    

    However, after using it with the gridfs package you may get this error: Error: <ID> does not exist. This is a package's bug, so this topic would go separately.