3

I'm working on a personal project to manage users of my club, it's hosted on the free Azure package (for now at least), partly as an experiment to try out Azure. Part of creating their records is to add a photo, so I've got a Contact Card view that lets me see who they are, when they came and a photo.

I have installed ImageResizer and it's really easy to resize the 10MP photos from my camera and save them to the file system locally, but it seems that for Azure I need to use their Blobs to Upload Pictures to Windows Azure Web Sites, and that's new to me. The documentation on ImageResizer says that I need to use AzureReader2 in order to work with Azure blobs but it isn't free. It also says in their best practices #5 to

Use dynamic resizing instead of pre-resizing your images.

Which is not what I was thinking, I was going to resize to 300x300 and 75x75 (for thumbnail) when creating the users record. But if I should be storing full size images as blobs and dynamically resizing on the way out then can I just use standard means to Upload a blob into a container to save it to Azure, then when I want to display the images use the ImageResizer and pass it each image to resize as required. That way not needing to use the AzureReader2, or have I misunderstood what it does / how it works?

Is there another way to consider?

I've not yet implemented cropping, but that's next to tackle when I've worked out how to actually store the images properly

Community
  • 1
  • 1
Simon Martin
  • 4,203
  • 7
  • 56
  • 93

3 Answers3

11

With some trepidation, I'm going to disagree with astaykov here. I believe you CAN use ImageResizer with Azure WITHOUT needing AzureReader2. Maybe I should qualify that by saying 'It works on my setup' :)

I'm using ImageResizer in an MVC 3 application. I have a standard Azure account with an images container.

Here's my test code for the view:

@using (Html.BeginForm( "UploadPhoto", "BasicProfile", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file" name="file" />
    <input type="submit" value="OK" />
}

And here's the corresponding code in the Post Action method:

// This action handles the form POST and the upload
[HttpPost]
public ActionResult UploadPhoto(HttpPostedFileBase file)
{
    // Verify that the user selected a file
    if (file != null && file.ContentLength > 0)
    {
        string newGuid = Guid.NewGuid().ToString();

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference("images");

        // Retrieve reference to the blob we want to create            
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(newGuid + ".jpg");

        // Populate our blob with contents from the uploaded file.
        using (var ms = new MemoryStream())
        {
            ImageResizer.ImageJob i = new ImageResizer.ImageJob(file.InputStream,
                    ms, new ImageResizer.ResizeSettings("width=800;height=600;format=jpg;mode=max"));
            i.Build();

            blockBlob.Properties.ContentType = "image/jpeg";
            ms.Seek(0, SeekOrigin.Begin);
            blockBlob.UploadFromStream(ms);
        }
    }

    // redirect back to the index action to show the form once again
    return RedirectToAction("UploadPhoto");
}

This is 'rough and ready' code to test the theory and could certainly stand improvement but, it does work both locally and when deployed on Azure. I can also view the images I've uploaded, which are correctly re-sized.

Hope this helps someone.

1

The answer to the concrete question:

If using ImageResizer with Azure blobs do I need the AzureReader2 plugin?

is YES. And as described in the Image Resizer's documentation - that plugin is used to read/process/serve images out of Blob Storage. So there is no doubt - if you are going to use Image Resizer, AzureReader2 is your needed plugin to make things right. It will take care of Blob uploads/serve.

Although I question Image Resizer's team competency on Windows Azure, since they are referencing Azure SDK v.2, while the most current version for Azure SDK is 1.8. What they mean is the Azure Storage Client Library, which has versions 1.7 and 2.x. Whereas version 2.x is recommended one to use and comes with Azure SDK 1.8. So, do not search for Azure SDK 2.0, install the latest one, which is 1.8. And by the way, use the Nuget Package Manager to install the Azure Storage Library v. 2.0.x.

astaykov
  • 30,768
  • 3
  • 70
  • 86
  • The `AzureReader2` plugin takes care of all the Azure related work for you, however it is possible to use `ImageResizer` and Azure blobs **without** the plugin - you just need to follow these instructions http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/ – Simon Martin Apr 22 '13 at 08:03
1

You can also upload resized versions to azure. So, you first upload the original image as a blob, say with the name /original/xxx.jpg; then you create a resize of the image and upload that to azure with the name say /thumbnail/xxx.jpg. If you want to create the resized versions on the fly or on a separate thread, you may need to temporarily save the original to disk.

staccata
  • 664
  • 1
  • 7
  • 17