Create a new Azure Function by choosing the Samples in theScenariodrop-down as shown in the following screenshot:
Select the ImageResizer-CSharp template as shown in the preceding screenshot.
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.
Review all the details and click on Create as shown in the following screenshot:
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); }
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.
In the Azure Storage Explorer, I can see two new Blob containers userprofileimagecontainer-md and userprofileimagecontainer-sm as shown in the following screenshot:
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: