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

问题描述 投票:2回答:3

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

Jimp.read("test.jpg", function (err, test) {
        if (err) throw err;
        test.resize(256, 256)
             .quality(50)                 
             .write("new.jpg"); 
    });
javascript node.js image
3个回答
5
投票

尝试类似的东西:

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

这应该将文件保存到项目根目录。

有关__dirname的更多信息


0
投票

我使用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

});

-2
投票

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

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

正如你在那里看到的那样,jimp模块是最慢的模块。

尝试锋利或帆布。

© www.soinside.com 2019 - 2024. All rights reserved.