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 be done...
So I want to reduce the time and enhance the performance.... Please help me on what changes to be made.
As i tried the same file (4mb--6mb) to upload on Flickr,500px and facebook they did it in less time....
I am not a professional programmer ..... I am just using simple mechanism to upload file through input and process the images in action of controller on server side...
I used the following code to resize each image...
Below is my controller action to process the images
updated the code below as per the suggestions which is taking around 1.6 min to process to the following diff sizes in code
#region Actions
/// <summary>
/// Uploads the file.
/// </summary>
/// <returns></returns>
[HttpPost]
public virtual ActionResult UploadImg()
{
HttpPostedFileBase myFile = Request.Files["UploadImage"];
bool isUploaded = false;
string message = "File upload failed";
var filename = Path.GetExtension(myFile.FileName).ToLowerInvariant();
if ((filename == ".jpg" || filename == ".jpeg") && myFile != null && myFile.ContentLength != 0)
{
// Paths
string Upath = Server.MapPath(@"~/photos/");
string ImgName = "_org.jpg";
string imageTo = "";
//Image names
string OrgImgName = System.IO.Path.GetFileName(myFile.FileName);
myFile.SaveAs(Path.Combine(Upath, myFile.FileName));
if (this.CreateFolderIfNeeded(Upath))
{
try
{
using (MagickImage original = new MagickImage(Upath + OrgImgName))
{
original.AutoOrient();
original.Write(Upath + ImgName);
original.ColorSpace = ColorSpace.Lab;
original.SetAttribute("density", "72x72");
int[] sizes = new int[] { 2048, 1600, 1024, 800, 500, 640, 320, 240, 150, 100, 75, 50 };
Parallel.For(0, sizes.Length, delegate(int index)
{
int size = sizes[index];
if (original.Width > size || original.Height > size)
{
if (size == 150 || size == 75 || size == 50)
{
string gmt = size.ToString() + 'x' + size.ToString();
MagickGeometry g = new MagickGeometry(gmt);
using (MagickImage resized = original.Clone())
{
resized.SetDefine(MagickFormat.Jpeg, "sampling-factor", "4:4:4");
resized.Blur(1, 0.375);
resized.FilterType = FilterType.LanczosSharp;
g.FillArea = true;
resized.Resize(g);
resized.Crop(size, size, Gravity.Center);
resized.ColorSpace = ColorSpace.sRGB;
Unsharpmask(resized, size);
resized.Quality = 85;
if (size == 150)
{
imageTo = Upath + GetOutputName(size);
}
else if (size == 75)
{
imageTo = Upath + GetOutputName(size);
}
else if (size == 50)
{
imageTo = Upath +GetOutputName(size);
}
resized.Write(imageTo);
}
}
else
{
using (MagickImage resized = original.Clone())
{
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;
if (size == 2048)
{
imageTo = Upath + GetOutputName(size);
}
else if (size == 1600)
{
imageTo = Upath + GetOutputName(size);
}
else if (size == 1024)
{
imageTo = Upath + GetOutputName(size);
}
else if (size == 800)
{
imageTo = Upath + GetOutputName(size);
}
else if (size == 640)
{
imageTo = Upath + GetOutputName(size); ;
}
else if (size == 500)
{
imageTo = Upath + GetOutputName(size);
}
else if (size == 320)
{
imageTo = Upath +GetOutputName(size);
}
else if (size == 240)
{
imageTo = Upath +GetOutputName(size);
}
else if (size == 100)
{
imageTo = Upath +GetOutputName(size);
}
else
{
imageTo = "";
}
resized.Write(imageTo);
}
}
}
});
}
isUploaded = true;
message = "File uploaded successfully!";
}
catch (Exception ex)
{
message = string.Format("File upload failed: {0}", ex.Message);
}
}
}
return Json(new { isUploaded = isUploaded, message = message }, "text/html");
}
#endregion
#region Private Methods
/// <summary>
/// Creates the folder if needed.
/// </summary>
/// <param name="path">The path.</param>
/// <returns></returns>
private bool CreateFolderIfNeeded(string path)
{
bool result = true;
if (!Directory.Exists(path))
{
try
{
Directory.CreateDirectory(path);
}
catch (Exception)
{
/*TODO: You must process this exception.*/
result = false;
}
}
return result;
}
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 if (size == 500)
resized.Unsharpmask(1.5, 0.8, 1, 0.02);
else if (size == 320)
resized.Unsharpmask(1.5, 0.6, 0.7, 0.02);
else if (size == 240)
resized.Unsharpmask(1.3, 1.5, 1.9, 0.01);
else if (size == 150)
resized.Unsharpmask(0.5, 1, 0.5, 0.002);
else if (size == 100)
resized.Unsharpmask(0.8, 0.4, 2.5, 0);
else if (size == 75)
resized.Unsharpmask(2, 1, 1.8, 0.05);
else if (size == 50)
resized.Unsharpmask(1, 0.4, 1.8, 0.02);
else
throw new NotImplementedException();
}
private string GetOutputName(int size)
{
string imagename = "";
if (size == 2048)
{
imagename = "_1.jpg";
}
else if (size == 1600)
{
imagename = "_2.jpg";
}
else if (size == 1024)
{
imagename = "_3.jpg";
}
else if (size == 800)
{
imagename = "_4.jpg";
}
else if (size == 640)
{
imagename = "_5.jpg";
}
else if (size == 500)
{
imagename = "_6.jpg";
}
else if (size == 320)
{
imagename = "_7.jpg";
}
else if (size == 240)
{
imagename = "_8.jpg";
}
else if (size == 150)
{
imagename = "_9.jpg";
}
else if (size == 100)
{
imagename = "_10.jpg";
}
else if (size == 75)
{
imagename = "_11.jpg";
}
else if (size == 50)
{
imagename = "_12.jpg";
}
else
{
imagename = "_noimage.jpg";
}
return imagename;
}
#endregion
Here is the working example of the above code: