我正在使用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的更多信息
我使用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
});
您可以将图像大小调整性能提高20倍。只需使用另一个模块进行图像处理。看一下这个:
https://github.com/ivanoff/images-manipulation-performance
正如你在那里看到的那样,jimp模块是最慢的模块。
尝试锋利或帆布。