0

I have a azure function app with one input and two outputs. In this case whenever an image is uploaded to a container: originals, the function app will be triggered which will generate two thumbnail images.

I developed the following function app using VS2017 and deployed to Azure portal.

Code:

using ImageResizer;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using System;
using System.Collections.Generic;
using System.IO;

namespace FunctionApp1
{
    public static class Function1
    {

        [FunctionName("Function1")]
        public static void Run(
                                [BlobTrigger("originals/{name}", Connection = "xxxxxxx")]Stream image,
                                [Blob("thumbs/s-{name}", FileAccess.ReadWrite, Connection = "xxxxxxx")]Stream imageSmall,
                                [Blob("thumbs/m-{name}", FileAccess.ReadWrite, Connection = "xxxxxxx")]Stream imageMedium,
                            TraceWriter log)
        {
            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) }
        };

    }
}

On validating it, I found that it is generating two different images as per requirement, but I see one of the file is corrupted.

CorrectImage:

enter image description here

CorruptedImage:

enter image description here

I did the validation for multiple images but see the same issue. The image with medium size configuration always gets corrupted.

Any rectifications to the above code is much helpful.

Can anyone help me to fix this issue?

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • It looks like you are creating many similar questions... – Mikhail Shilkov Sep 07 '18 at 15:21
  • I am trying all possible approaches to resolve this issue. I tried with Azure Cognitive Service and BlobTrigger in the post: https://stackoverflow.com/questions/52202426/resized-image-getting-corrupted-using-azure-functions and then tried with ImageResizer and BlobTrgger in the post: https://stackoverflow.com/questions/52225151/image-dimensions-getting-corrupted-using-imageresizer-with-azure-function-app . With both the approaches, I am seeing the same issue. Any help on this is much appreciated. – santosh kumar patro Sep 07 '18 at 15:32
  • What is the aspect ratio of the input image? The sizes in `imageDimensionsTable` have varying aspect ratios. `ExtraSmall` is 1.6, `Small` is 1.6, `Medium` is 1.3. Not sure if that's an issue. – Dan Wilson Sep 07 '18 at 15:38
  • Input image details , Type: PNG: Width:1914px , Height:1123px and aspect ratio is: 1914 : 1123 . Any correction to the code will help me. – santosh kumar patro Sep 07 '18 at 15:48
  • Can you open that corrupted PNG with a text editor and check what's inside? May provide some clues. – evilSnobu Sep 07 '18 at 17:41
  • This is content of the corrupted PNG file : {"code":"InvalidImageFormat","requestId":"8c6db10a-71b7-422e-b1d0-b68fa04849b5","message":"Input data is not a valid image."} – santosh kumar patro Sep 07 '18 at 17:46
  • How to rectify this? Do you see any issues with the code – santosh kumar patro Sep 07 '18 at 17:46
  • I am guessing you can't just reset the stream position to 0. you 'll have to copy the image stream to a new stream in order to generate the second thumbnail. Have you tried commenting the 1st thumbnail generation ? Just wanted to check if it is a stream problem or something different – Thomas Sep 09 '18 at 09:01
  • Yes I commented the first one and then tested it and found the second image gets generated properly. The issue occurs when I am trying to create two images. Any rectification to the code is much helpful. I have been trying a lot but no help until now. Any help on this issue is much appreciated. – santosh kumar patro Sep 09 '18 at 13:25

1 Answers1

1

Can you please check is there any other function app already in running status. In short I would like to say that check all the function apps that you have developed in this process, which is monitoring the blob storage container. I suspect that some other function app is getting triggered and causing the issue here. Please stop all the function apps and only run the required function app to see if it resolves your issue. Please let me know in case you need any further help on this.

Viral Jain
  • 56
  • 3
  • Thanks very much for your reply Viral. You are absolutely correct in this case. I have multiple azure function app in running state, which are getting triggered whenever any image is getting uploaded to the container. Out of these the corrupted function app fires every time and results into the generation of corrupted image. I have disabled all the function apps and now just enabled the correct one and tested it and found it be to working fine. Thanks again for all your help :). – santosh kumar patro Sep 10 '18 at 15:03
  • It's also possible that the input stream is not resetting to byte 0 correctly. Not all streams are seekable. – Lilith River Sep 11 '18 at 19:14
  • @LilithRiver Could you please explain it with details? I'm having problem with a corrupted the image file uploaded from a azure function. Some random hexes changed to `3f`. https://stackoverflow.com/questions/54473126/azure-functions-how-to-use-the-multiparthttpservletrequest-class-from-the-de – Deckard Feb 11 '19 at 01:11