我如何使用缓冲区将其作为图像上传到Cloudinary。在使用Sharp的缓冲区中,我将其指定为jpeg。我不想将其保存在服务器中,因此我正在处理图像,并通过req对象发送它。
exports.resizeUserPhoto = async (req, res, next) => {
if (!req.file) return next();
req.file.filename = `user-${req.user.id}-${Date.now()}.jpeg`;
const processedImage = await sharp(req.file.buffer)
.resize(150, 150)
.toFormat("jpeg")
.jpeg({ quality: 90 })
.toBuffer();
//saving the buffer to a new file object
req.file.processedImage = processedImage;
next();
};
exports.updateMe = catchAsync(async (req, res, next) => {
if (req.file) {
console.log(req.file.processedImage); //returns <Buffer ....... >
const result = await cloudinary.uploader.upload(req.file.processedImage, {
use_filename: true,
folder: `${req.user.id}/profile/`
});
filteredBody.photo = result.url;
}
}
// Converting buffer to base64 encoding
console.log(req.file.processedImage.toString('base64'))
[Base64
的想法是使用可读字符表示字节可以容纳的任何值(0-255),因此编码被广泛用于将原始数据表示为文本。