Azure Serverless Computing Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. Create a new Azure Function by choosing the Samples in the Scenario drop-down as shown in the following screenshot:
  1. Select the ImageResizer-CSharp template as shown in the preceding screenshot.
  2. Once you have selected the template, the portal prompts you to choose the following parameters:
    • Name your Function: Provide a meaningful name. For this example, I have provided CropProfilePictures.
    • Azure Blob Storage trigger (image):
      • Path: Provide the path of the container (in our case userprofileimagecontainer) which contains all the blobs that are created by the Queue trigger. CreateProfilePictures in the previous recipe
      • Storage account connection: Select the connection string of the storage account where the container and Blobs are stored
    • Azure Blob Storage output (imageMedium):
      • Path: Please provide the name of the container where the resized images of size medium 200*200 are to be stored. In this case, userprofileimagecontainer-md.
      • Storage account connection: Select the connection string of the storage account where the Blobs are stored.
    • Azure Blob Storage output (imageSmall):
      • Path: Please provide the name of the container where the resized images of size small 100*100 are to be stored. In this case, userprofileimagecontainer-sm.
      • Storage account connection: Select the connection string of the storage account where the Blobs are stored.
  1. Review all the details and click on Create as shown in the following screenshot:
  1. Fortunately, the ImageResizer Azure Function template provides most of the necessary code for our requirement of resizing the image. I just made a few minor tweaks. Replace the default code with the following code and the code should be self-explanatory:
        using ImageResizer;

public static void Run(
Stream image, Stream imageSmall, Stream imageMedium)
{
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
{
Small, Medium
}

private static Dictionary<ImageSize, Tuple<int, int>>
imageDimensionsTable = new Dictionary<ImageSize, Tuple<int,
int>>()
{
{ ImageSize.Small, Tuple.Create(100, 100) },
{ ImageSize.Medium, Tuple.Create(200, 200) }
};
  1. Let's run a test on the RegisterUser function by submitting a sample request with firstname, lastname, and a ProfilePicUrl. I have used the same inputs that we have used in our previous recipes.
  1. In the Azure Storage Explorer, I can see two new Blob containers userprofileimagecontainer-md and userprofileimagecontainer-sm as shown in the following screenshot:
  1. I can even view the corresponding cropped versions in each of those containers. Following are the three versions of the image that we have used as input: