设置缩略图图像的内容类型

设置缩略图图像的内容类型

本文介绍了设置缩略图图像的内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为缩略图设置Content-Type.我已经尝试过如下图所示.但是它不起作用.仍然将其存储为流.

I need to set Content-Type for the thumbnail image. I have tried as shown below.But it is not working.Still, it stores as a stream.

天蓝色功能

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
}

我在NodeJ上看到了相同的实现方式

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

Question: Can you tell me how to set Content-Type on the Azure function?

p.s.我不是Nodejs开发人员.

p.s. I'm not a Nodejs developer.

推荐答案

不幸的是,节点的Blob输出绑定不支持设置内容类型.一种选择是删除输出绑定,并在您的本机中使用 azure storage sdk 节点功能,应该可以为您提供所需的控制权.

Unfortunately the blob output binding for node does not support setting a content type. One option would be to drop the output binding and use the azure storage sdk natively in your node function which should give you the control you need.

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

可以通过content.res访问类似express的"res"对象,因此需要context.res.set/context.res.type代替stream.set.在getBuffer回调中返回的stream对象是缓冲区,而不是流,并且与http响应无关.

An express-like 'res' object can be accessed via content.res, so instead of stream.set you'll want context.res.set / context.res.type. The stream object returned in the getBuffer callback is a buffer, not a stream, and has nothing to do with the http response.

要注意的一件事是azure函数尚不支持从节点返回流-您需要具有整个缓冲区(幸运的是,getBuffer似乎返回了!)

One thing to note is that azure functions does not support returning of streams from node yet - you'll need to have the entire buffer (which, luckily, getBuffer appears to return!)

这是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);
    }
});

这篇关于设置缩略图图像的内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:52