繁体   English   中英

设置缩略图图像内容类型

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM