简体   繁体   English

使用jimp在Node.js中调整图像大小并获取新图像的路径

[英]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来调整node.js中图像的大小,我成功地降低了图像质量,但是有点困惑如何获取新图像的路径

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 有关__dirname的更多信息

I am using app-root-path npm to get rootPath: 我正在使用app-root-path npm来获取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 . 您可以将图像大小调整性能提高20倍 Just use another module for image processing. 只需使用另一个模块进行图像处理即可。 Check this out: 看一下这个:

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

As you can see there, jimp module is the slowest module. 如您所见,jimp模块是最慢的模块。

Try sharp or canvas. 尝试锐利或帆布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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