node.js - 设置缩略图内容类型

标签 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吗?

附:我不是 Nodejs 开发人员。

最佳答案

编辑:

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

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

类似express的“res”对象可以通过content.res访问,因此您需要context.res,而不是stream.set。设置/context.res.typegetBuffer 回调中返回的 stream 对象是缓冲区,而不是流,与 http 响应无关。

需要注意的一件事是,azure 函数尚不支持从 Node 返回流 - 您需要拥有整个缓冲区(幸运的是,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);
    }
});

关于node.js - 设置缩略图内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41858321/

相关文章:

node.js - MongoDB API 分页

node.js - 如何使用 Node http-proxy 代理到根路径

ios - 设置绘制到图像时文本的描边宽度

android - 如何在 Android 应用程序中使用 java.awt.image 包

typescript - 为什么未收到我的 x-functions-key Azure Function header ?

c - 如何跟踪 C Linux 中 system() 运行的后台进程?

javascript - 如何使用 browserify 模块为浏览器适配 xml2js Node 模块?

c# - 如何从图像/位图对象返回图像作为 IActionResult

azure - Azure AD Openid Connect 提供程序的多个指纹

c# - 使用 Visual Studio 2017 调试时为 "Request is not available in this context"