简体   繁体   中英

Resize an image in Node.js using jimp and get the path the new image

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"); 
    });

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

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

});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM