2

I'm using jimp to resize an image in node.js, I'm successfully able to degrade the image quality, but a bit confused how to get the path of new image

Jimp.read("test.jpg", function (err, test) {
        if (err) throw err;
        test.resize(256, 256)
             .quality(50)                 
             .write("new.jpg"); 
    });
0aslam0
  • 1,797
  • 5
  • 25
  • 42
learner
  • 143
  • 2
  • 3
  • 11
  • 1
    Reading the doc (https://github.com/oliver-moran/jimp#writing-to-files-and-buffers) tells me you can specify the absolute path in `write` function – 0aslam0 Aug 01 '16 at 05:05

3 Answers3

9

try something like:

Jimp.read("test.jpg", function (err, test) {
        if (err) throw err;
        test.resize(256, 256)
             .quality(50)                 
             .write(__dirname + "./new.jpg"); 
    });

This should save the file to your project root.

More info on __dirname

Kyle Finley
  • 11,842
  • 6
  • 43
  • 64
0

You can increase your image resizing performance by 20 times. Just use another module for image processing. Check this out:

https://github.com/ivanoff/images-manipulation-performance

As you can see there, jimp module is the slowest module.

Try sharp or canvas.

Dimitry Ivanov
  • 213
  • 2
  • 4
  • Thank you, Jimp is slowest, ram consumer and looks like out of the game. Add a line of pixel on a simple rotate, I needed to patch the rotate version. Avoid it – Mathias Osterhagen Nov 27 '20 at 10:31
0

I am using app-root-path npm to get rootPath:

var appRoot = require('app-root-path');
var imgName = new Date().getTime().toString();
var savePath = appRoot + '/uploads/' + imgName;

Jimp.read("test.jpg", function (err, image) {
    if (err) throw err;
    image.resize(256, 256)
         .quality(50)                 
         .write(savePath);

    // save savePath to database

});
Dũng IT
  • 2,751
  • 30
  • 29