上传后,在Azure blob上调整图像大小

时间:2018-04-05 13:19:10

标签: c# azure azure-functions

我正在使用Web API将图像上传到Azure Blob,如果尺寸大于900 * 900,我想在同一容器中调整上传的图像,然后在另一个容器中创建缩略图图像。

我创建了一个Azure功能,它创建了一个缩略图,但我不知道如果它大于900 * 900,如何调整原始图像的大小。

using ImageResizer;

public static void Run(
Stream image,                           // input blob, large size
Stream imageSmall,
Stream imageMedium)  // output blobs
{
    var imageBuilder = ImageResizer.ImageBuilder.Current;
    var size = imageDimensionsTable[ImageSize.Small];

    imageBuilder.Build(
       image, imageSmall,
       new ResizeSettings(size.Item1, size.Item2, FitMode.Max, null), 
       false);

image.Position = 0;
size = imageDimensionsTable[ImageSize.Medium];

imageBuilder.Build(
    image, imageMedium,
    new ResizeSettings(size.Item1, size.Item2, FitMode.Max, null), false);
}

public enum ImageSize
{
  ExtraSmall, Small, Medium
}

private static Dictionary<ImageSize, Tuple<int, int>> imageDimensionsTable = new Dictionary<ImageSize, Tuple<int, int>>()
    {
        { ImageSize.ExtraSmall, Tuple.Create(320, 200) },
        { ImageSize.Small,      Tuple.Create(640, 400) },
        { ImageSize.Medium,     Tuple.Create(800, 600) }
    };

1 个答案:

答案 0 :(得分:0)

我在azure portal上尝试过并使用BlobTrigger。但是,当我尝试将输出路径添加为输入路径时,它始终不会执行。当我在输出路径中使用blob名称的随机名称时,我发现它将执行循环。

所以,我建议你可以尝试在函数中使用动态输出绑定来避免它。

对于动态输出绑定,您可以利用以下代码段:

var attributes = new Attribute[]
{    
    new BlobAttribute("{container-name}/{blob-name}"),
    new StorageAccountAttribute("joeystorage") //connection string name for storage connection
};
using (var writer = await binder.BindAsync<TextWriter>(attributes))
{
    writer.Write(userBlobText);
}

enter image description here 注意:上面的代码会创建目标blob(如果不存在),并覆盖现有的blob(如果存在)。此外,如果您未指定StorageAccountAttribute,则会根据应用设置AzureWebJobsStorage将您的目标blob创建到存储帐户中。

此外,您可以关注Azure Functions imperative bindings了解详情。

详细代码:

#r "Microsoft.WindowsAzure.Storage"
using ImageResizer;
using Microsoft.WindowsAzure.Storage.Blob;

public static async Task Run(
Stream image,   // input blob, large size
Binder binder,string blobname,string blobextension,TraceWriter log)  // output blobs
{
    var attributes = new Attribute[]
    {    
        new BlobAttribute($"container/{blobname}.{blobextension}"),

        new StorageAccountAttribute("joeystorage") //connection string name for storage connection
    };
    CloudBlockBlob blob =await binder.BindAsync<CloudBlockBlob>(attributes);
    var imageBuilder = ImageResizer.ImageBuilder.Current;
    var size = imageDimensionsTable[ImageSize.Small];

    Stream outStream=new MemoryStream();
    imageBuilder.Build(
       image,outStream,
       new ResizeSettings(size.Item1, size.Item2, FitMode.Max, null),false);
      blob.UploadFromStream(outStream);
}

public enum ImageSize
{
  ExtraSmall, Small, Medium
}

private static Dictionary<ImageSize, Tuple<int, int>> imageDimensionsTable = new Dictionary<ImageSize, Tuple<int, int>>()
    {
        { ImageSize.ExtraSmall, Tuple.Create(320, 200) },
        { ImageSize.Small,      Tuple.Create(640, 400) },
        { ImageSize.Medium,     Tuple.Create(800, 600) }
    };