【问题标题】:Resize an image in Node.js using jimp and get the path the new image使用 jimp 在 Node.js 中调整图像大小并获取新图像的路径
【发布时间】:2016-08-01 05:01:59
【问题描述】:

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


【解决方案1】:

尝试类似:

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

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

更多信息__dirname

【讨论】:

    【解决方案2】:

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

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

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

    尝试锐利或帆布。

    【讨论】:

    • 谢谢你,Jimp 是最慢的,ram 消费者,看起来就像不在游戏中一样。在简单的旋转上添加一行像素,我需要修补旋转版本。避免它
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 2019-01-25
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2014-07-24
      • 2017-03-25
      相关资源
      最近更新 更多