0
votes

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?

1
See if there is a replaceExact method which only takes output size parameters, or if you can slip in an exclamation mark amongst options. Please read documentation for gm on github - it covers this issue exactly. Noting the imagemagick module is abandoned in favor of gm. If this solves your issue please post an answer describing how to do it.traktor

1 Answers

1
votes
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