设置缩略图图像Content-Type

时间:2017-01-25 17:53:17

标签: node.js image azure thumbnails azure-functions

我需要为缩略图设置Content-Type。我尝试过如下所示。但它没有工作。但它仍然存储为流。

enter image description here

Azure功能:

index.json

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);

                }

            });

    });

};

function.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吗?

P.S。我不是Nodejs开发人员。

1 个答案:

答案 0 :(得分:1)

修改

不幸的是,节点的blob输出绑定不支持设置内容类型。一种选择是删除输出绑定并在节点函数中本地使用azure storage sdk,这将为您提供所需的控件。

如果使用Http触发器和输出绑定:

可以通过library(dplyr) df <- data_frame(Level1 = rep(1:3, each = 3), Level2 = letters[1:9], x = c(0.12,0.08,0.22,0.32,0.17,0.20,0.11,0.05,0.01), y = c(1,1,1,0,0,1,1,1,1), z = c(rep("debt",4),rep("credit",5)), k = c(0,0,0,1,1,1,1,1,1)) df %>% count(Level1, k) Source: local data frame [3 x 3] Groups: Level1 [?] Level1 k n <int> <dbl> <int> 1 1 0 3 2 2 1 3 3 3 1 3 访问类似快递的“res”对象,因此您需要content.res / stream.set代替context.res.setcontext.res.type回调中返回的stream对象是缓冲区,而不是流,与http响应无关。

有一点需要注意的是,azure函数不支持从节点返回流 - 你需要拥有整个缓冲区(幸运的是,getBuffer似乎会返回!)

以下是getBuffer回调:

getBuffer