public JsonResult JSUpload(int postId) { bool success = false; var image = WebImage.GetImageFromRequest(); var imgFormat = image.ImageFormat.ToString().ToLower(); var post = _ctx.Post.Where(p => p.Id == postId).SingleOrDefault(); var imgQuantity = _ctx.Image.Where(m => m.Post.Id == post.Id && m.IsThumbnail == false && m.IsDeleted == false).ToList<ImageModel>().Count; if (imgFormat == "jpeg" || imgFormat == "png" || imgFormat == "jpg" ) { if (imgQuantity >= 4) { return Json(new { success, testforurl = MakeThumbnail(image, post.Id, true) }); } if (image.Width > 250) { image.Resize(250, ((250 * image.Height) / image.Width)); } var filename = Path.GetFileName(image.FileName).Replace(" ", ""); image.Save(Path.Combine("~/Images", filename)); var postImage = new ImageModel { ImageUrl = ("/Images/" + filename), IsDeleted = false, Post = post, IsThumbnail = false }; _ctx.Image.Add(postImage); _ctx.SaveChanges(); success = true; return Json(new { success, testforurl = MakeThumbnail(image, post.Id, false) }); } else { return Json(new { success, testforurl = "/Images/wrongFileType.png" }); } }
public string MakeThumbnail(WebImage image, int? postId, bool reachedPicLimit) { var post = _ctx.Post.Where(p => p.Id == postId).SingleOrDefault(); image.Resize(110, 110, true, true); var filename = Path.GetFileName(image.FileName).Replace(" ", ""); image.Save(Path.Combine("~/Thumbnails", filename)); var postImage = new ImageModel { ImageUrl = ("/Thumbnails/" + filename), IsDeleted = false, Post = post, IsThumbnail = true }; if (reachedPicLimit) { postImage.ImageUrl = "/Images/picLimit.png"; } _ctx.Image.Add(postImage); _ctx.SaveChanges(); return postImage.ImageUrl; }