Vladimir37
Vladimir37

Reputation: 568

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?

Upvotes: 0

Views: 891

Answers (2)

Mike Petterson
Mike Petterson

Reputation: 1

Using Sascha's answer, in case someone searches for this, you can use strings in the options and specify the exclamation point on the height as follows

var resize_options = {
    srcPath: path,
    dstPath: path,
    width: "100",
    height: "50!",
};

Because: "In order to resize an image to arbitrary dimensions while ignoring the aspect ratio, you can append ! to the dimensions, like 100x100!" So adding the exclamation mark to the height, will add it to the command used, such as 100x50!

https://www.digitalocean.com/community/tutorials/workflow-resizing-images-with-imagemagick

Upvotes: 0

Sascha
Sascha

Reputation: 1158

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

Upvotes: 1

Related Questions