[英]Set Thumbnail image Content-Type
我需要為縮略圖設置Content-Type
。 我已經嘗試如下所示。但它不工作。仍然,它存儲為流。
天藍色功能:
索引文件
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) => {
if (error) {
context.log(`There was an error processing the image.`);
context.done(error);
}
else {
context.log(`Successfully processed the image`);
stream.set("Content-Type", Jimp.MIME_JPEG); // here need to set the Content-Type
context.done(null, stream);
}
});
});
};
函數.json
{
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"direction": "in",
"path": "project2-photos-original/{name}",
"connection": "thumbnailfunction_STORAGE",
"dataType": "binary"
},
{
"type": "blob",
"name": "$return",
"path": "project2-photos-thumbnail/{name}",
"connection": "thumbnailfunction_STORAGE",
"direction": "out"
}
],
"disabled": false
}
我在 NodeJs 上看到過類似的實現
var Jimp = require("jimp");
var express = require("express");
var app = express();
app.get("/my-dynamic-image", function(req, res){
Jimp.read("lenna.png", function(err, lenna) {
lenna.resize(64, 64).quality(60).getBuffer(Jimp.MIME_JPEG, function(err, buffer){
res.set("Content-Type", Jimp.MIME_JPEG);
res.send(buffer);
});
});
});
app.listen(3000);
問:能否告訴我如何在 Azure 函數上設置Content-Type
?
ps 我不是 Nodejs 開發人員。
編輯:
不幸的是,節點的 blob 輸出綁定不支持設置內容類型。 一種選擇是刪除輸出綁定並在您的節點函數中本地使用azure 存儲 sdk ,這應該為您提供所需的控制。
如果使用 Http 觸發器和輸出綁定:
可以通過content.res
訪問類似 express 的“res”對象,因此您需要context.res.set
/ context.res.type
而不是stream.set
。 getBuffer
回調中返回的stream
對象是緩沖區,不是流,與http響應無關。
需要注意的一件事是,azure 函數尚不支持從節點返回流 - 您需要擁有整個緩沖區(幸運的是,getBuffer 似乎返回了!)
這是一個getBuffer
回調:
function(err, buffer){
if (err) {
context.log("There was an error processing the image.");
context.done(err);
}
else {
context.log("Successfully processed the image");
// set content type to Jimp.MIME_JPEG
context.res.type(Jimp.MIME_JPEG)
// send the raw response (don't apply any content negotiation)
context.res.raw(buffer);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.