Search code examples
javascriptnode.jsimageimagemagick

Resize image without aspect ratio in Node.js


I need resize image to fixed size in Node.js through ImageMagick. Example: images 200x140, 500x200 and 130x100 converted to 100x50. The image must be stretched or compressed without aspect ratio and fill the entire space. I do this so:

var im = require('imagemagick');

var resize_options = {
    srcPath: path,
    dstPath: path,
    width: 100,
    height: 50
};
im.resize(resize_options, function (err) {
    if (err) {
        console.log(err);
        res.end('Error!');
    }
    else {
        res.end('Success!');
    }
});

But image converten by only height, example - 1440x900 converted to 80x50 instead 100x50. What am I doing wrong?


Solution

  • var im = require('imagemagick');
    
    var width = 800;
    var height = 123;
    im.convert(['./in.jpg', '-resize', width + 'x' + height + '\!', './out.png'],
      function (err, stdout) {
        if (err) throw err;
      });
    

    would do the job. It resizes your image independantly of its original size to the given width & height