【问题标题】:Resize Images on Azure blob after uploading them上传后在 Azure blob 上调整图像大小
【发布时间】:2018-04-05 13:19:10
【问题描述】:

我正在使用 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) }
    };

【问题讨论】:

  • 请更好地格式化您的代码。
  • 另外,ImageResizer 是付费库,所以无法测试您的代码。
  • 你的代码现在做什么?你想让它做什么呢?
  • 这是一个 Azure 函数模板。
  • 它在一个新的blob中调整原来的大小,如果大小大于900*900,我想替换同一个blob中的原始大小

标签: c# azure azure-functions


【解决方案1】:

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

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

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

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

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

另外,您可以关注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) }
    }; 

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 1970-01-01
    • 2013-02-01
    • 2015-11-18
    • 1970-01-01
    • 2015-06-08
    • 2013-02-09
    • 2013-10-29
    • 2023-03-18
    相关资源
    最近更新 更多