This project has moved. For the latest updates, please go here.

Image Magick resize on server side taking 3 minutes to process to 10 different sizes… how to boost the performance

Oct 4, 2014 at 6:58 AM
Edited Oct 4, 2014 at 7:07 AM
I am working on a project which involves uploading of Dslr Camera images from user and resizing to 10 different sizes...

I am using Imagemagick to resize on server side.... but it is taking too much time to process images.. which is more than 3 minutes ... end user will be irritated waiting for it to done...

so i want to reduce the time and enhance the performance.... Please help me on what changes to be made..

As i tried same file(4mb--6mb) to upload on flickr,500px and facebook they did it in less time....

I am not a Pro programmer ..... Iam just using simple mechanism to upload file through input and process the images in action of controller on serverside...


I used the following procedure suggested by dlemstra to process resize of image uploaded by the user to 10 different sizes but still the processing time is 2.2 minutes...
void Resize()
{
  using (MagickImage original = new MagickImage("original.jpg"))
  {
    original.AutoOrient();
    original.Strip();
    original.ColorSpace = ColorSpace.Lab;
    original.SetAttribute("density", "72x72");

    int[] sizes = new int[] { 2048, 1600, 1024, 800, 640 };
    Parallel.For(0, sizes.Length, delegate(int index)
    {
      int size = sizes[index];

      if (original.Width <= size && original.Height <= size)
        return;

      using (MagickImage resized = original.Clone())
      {
        if (size == 2048)
          resized.SetDefine(MagickFormat.Jpeg, "sampling-factor", "4:4:4");

        resized.Blur(1, 0.375);

        resized.FilterType = FilterType.LanczosSharp;
        resized.Resize(size, size);

        resized.ColorSpace = ColorSpace.sRGB;
        Unsharpmask(resized, size);

        resized.Quality = 85;

        resized.Write(GetOutputName(size));
      }
    });
  }
}

private void Unsharpmask(MagickImage resized, int size)
{
  if (size == 2048)
    resized.Unsharpmask(2, 1, 1.7, 0.2);
  else if (size == 1600)
    resized.Unsharpmask(1.6, 0.5, 1.7, 0.25);
  else if (size == 1024)
    resized.Unsharpmask(2.8, 1, 0.7, 0.2);
  else if (size == 800)
    resized.Unsharpmask(1.2, 0.8, 0.7, 0.08);
  else if (size == 640)
    resized.Unsharpmask(2, 1, 0.7, 0.02);
  else
    throw new NotImplementedException();
}

string GetOutputName(int size)
{
  return "ModifyThisMethod.jpg";
}
i am using magick.net-q16-anycpu version:7.0.0.0003 nuget package
Coordinator
Oct 4, 2014 at 8:11 AM
The code example I created for you does not resize the image to 10 different sizes. So your code is probably different from what you are showing here. Can you post a ink to an example file and the actual code you are using? Preferably on something like dropbox.
Oct 4, 2014 at 9:55 AM
Edited Oct 4, 2014 at 10:18 AM
here is the code which i was using... almost the same except database storage is not done in this example which i did in my original code...

I mailed you the dropbox link of the example project file
Coordinator
Oct 4, 2014 at 10:00 PM
The example you send me does not contain the Parallel.For loop. That is the part that lets you resize the images parallel.
Oct 5, 2014 at 4:36 AM
Edited Oct 5, 2014 at 5:26 AM
when i used paralellel.For loop it took around 15min on my local system to process so i changed it to for... if needed i will mail you that example too

you can check the working example of the parallel at http://ajaykumarg-001-site6.smarterasp.net/ which took around 1.6 min to process

here the working example of without parallel for http://ajaykumarg-001-site8.smarterasp.net/ whick took around 2.2 min for the same file to process

i am mailing you the dropbox link of the parallel.for ... please have a look at it and suggest me if any changes
Coordinator
Oct 5, 2014 at 12:56 PM
What is you input image? Can you provide me with an example?
Oct 5, 2014 at 4:48 PM
i will mail you the drop box link of the image i used
Coordinator
Oct 6, 2014 at 7:03 AM
Thank you, I will take a look at it during my lunch break today.
Oct 6, 2014 at 8:54 AM
ok thank you for the support you are giving
Coordinator
Oct 6, 2014 at 10:21 PM
I have created a test class that can be found here: https://www.dropbox.com/sh/931vwly297sren5/AACaNjP8kE5wfs-eesCGM_eMa?dl=0. I moved the Blur method outside the resize loop because it only needs to happen once. This is an action that takes a lot of time so it really reduced the execution time. You can see the output from my machine in the dropbox folder. It now only takes 12 seconds on my machine (20-8). I also added a parallel test and it surprisingly is slower. I will investigate this further at some other time.
Oct 7, 2014 at 9:23 AM
Thank you very much...

i tried to run the test in my pc but i got errors so i moved the blur method in my code and tried .... normal and parallel.For both took 43s to process image to diff sizes on my machine...

The difference in processing time on your machine and my machine may be because of hardware variance... i will try to run it online and will get back to you with results...

once again thank you