我已经使用这篇Background image thumbnail processing with Azure Functions and NodeJS文章来创建缩略图。图像创建成功。但图像的大小已经增加。怎么会发生?它必须非常小吗?我该如何解决这个奇怪的问题?
这是Blob存储空间中的原始图像
完成此过程(缩略图)
这是Azure功能(节点):
var Jimp = require("jimp");
module.exports = (context, myBlob) => {
// Read image with Jimp
Jimp.read(myBlob).then((image) => {
// Manipulate image
image
.resize(200, Jimp.AUTO)
.greyscale()
.getBuffer(Jimp.MIME_JPEG, (error, stream) => {
// Check for errors
if (error) {
context.log(`There was an error processing the image.`);
context.done(error);
}
else {
context.log(`Successfully processed the image`);
// Bind the stream to the output binding to create a new blob
context.done(null, stream);
}
});
});
};
答案 0 :(得分:1)
我找到了解决方案。
JPEG的默认质量是100.您应该将其设置为某些内容 降低以获得压缩:
您可以在此处详细了解:Image is resized down and gets bigger file size
我必须设置.quality(50)
,如下所示。此问题仅适用于JPEGs
。
var Jimp = require("jimp");
module.exports = (context, myBlob) => {
// Read image with Jimp
Jimp.read(myBlob).then((image) => {
// Manipulate image
image
.resize(200, Jimp.AUTO)
.greyscale()
.quality(50) // set the quality for JPEGs
.getBuffer(Jimp.MIME_JPEG, (error, stream) => {
// Check for errors
if (error) {
context.log(`There was an error processing the image.`);
context.done(error);
}
else {
context.log(`Successfully processed the image`);
// Bind the stream to the output binding to create a new blob
context.done(null, stream);
}
});
});
};
答案 1 :(得分:0)
我遇到了同样的问题,我使用了Sharp,它对我有用。如果您需要更多帮助,请告诉我,我将分享完整的功能代码。
const sharp = require('sharp');
const thumbnailWidth = 250
module.exports = async (context, myBlob) => {
let thumbBuffer
const image = await sharp(myBlob)
image.resize({ width: thumbnailWidth })
return thumbBuffer = await image.toBuffer()
}