我正在尝试调整上传到我的容器的图像的大小,以便为我的网站创建缩略图和各种其他版本的图像。
我上传的图像必须更正内容类型"image/jpeg",但是当我使用下面的代码创建它们的新版本时,结果是"应用程序/八位字节流"。
我在这里错过了什么?
using ImageResizer;
using ImageResizer.ExtensionMethods;
public static void Run(Stream myBlob, string blobname, string blobextension, Stream outputBlob, TraceWriter log)
{
log.Info($"C# Blob trigger function Processed blobn Name:{blobname} n Size: {myBlob.Length} Bytes");
var instructions = new Instructions
{
Width = 570,
Mode = FitMode.Crop,
Scale = ScaleMode.Both,
};
ImageBuilder.Current.Build(new ImageJob(myBlob, outputBlob, instructions));
}
编辑:解决方案。
#r "Microsoft.WindowsAzure.Storage"
using ImageResizer;
using ImageResizer.ExtensionMethods;
using Microsoft.WindowsAzure.Storage.Blob;
public static void Run(Stream myBlob, string blobname, string blobextension, CloudBlockBlob outputBlob, TraceWriter log)
{
log.Info($"C# Blob trigger function Processed blobn Name:{blobname} n Size: {myBlob.Length} Bytes");
var instructions = new Instructions
{
Width = 570,
Mode = FitMode.Crop,
Scale = ScaleMode.Both
};
Stream stream = new MemoryStream();
ImageBuilder.Current.Build(new ImageJob(myBlob, stream, instructions));
stream.Seek(0, SeekOrigin.Begin);
outputBlob.Properties.ContentType = "image/jpeg";
outputBlob.UploadFromStream(stream);
}
使用流输出时,函数会将内容类型默认为应用程序/八位字节流。
使用 ICloudBlob 类型之一,它应该允许你指定 blob 的内容类型。
以下是可以作为参数绑定到的类型备忘单: https://jhaleyfiles2016.blob.core.windows.net/public/Azure%20WebJobs%20SDK%20Cheat%20Sheet%202014.pdf
调整图像大小的 Azure 函数的完整工作示例:
run.csx
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Blob;
using ImageResizer;
using ImageResizer.ExtensionMethods;
public static void Run(Stream imageStream, string blobName, CloudBlockBlob outputBlob, TraceWriter log)
{
log.Info($"Function triggered by blobn Name:{blobName} n Size: {imageStream.Length} Bytes");
var instructions = new Instructions
{
Width = 400,
Height = 350,
Mode = FitMode.Max,
OutputFormat = OutputFormat.Jpeg,
JpegQuality = 85
};
using (var outputStream = new MemoryStream())
{
ImageBuilder.Current.Build(new ImageJob(imageStream, outputStream, instructions));
outputStream.Position = 0;
outputBlob.Properties.ContentType = "image/jpeg";
outputBlob.UploadFromStream(outputStream);
}
}
function.json
{
"bindings": [
{
"name": "imageStream",
"type": "blobTrigger",
"direction": "in",
"path": "watched-container/{blobName}.jpg",
"connection": "AzureWebJobsDashboard"
},
{
"type": "blob",
"name": "outputBlob",
"path": "output-container/{blobName}.jpg",
"connection": "AzureWebJobsDashboard",
"direction": "inout"
}
],
"disabled": false
}
project.json
{
"frameworks": {
"net46":{
"dependencies": {
"ImageResizer": "4.0.5"
}
}
}
}