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



我正在使用jimp调整node.js中的图像大小,我成功地降低了图像质量,但有点困惑如何获得新图像的路径

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

尝试以下操作:

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

这应该会将文件保存到您的项目根目录中。

有关__dirname 的更多信息

您可以将图像大小调整性能提高20倍。只需使用另一个模块进行图像处理。看看这个:

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

如您所见,jimp模块是最慢的模块。

试试锋利的或帆布的。

我正在使用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
});

最新更新