public JsonResult Crop(bool overwriteFile, string targetFileUrl, string sourceFileUrl, double resize, int?top, int?left, int?width, int?height) { var sourcePath = PathInfo.ConvertToPath(sourceFileUrl); var targetPath = overwriteFile ? sourcePath : PathInfo.ConvertToPath(targetFileUrl); if (!PathInfo.CheckSecurity(targetPath).Result) { return(Json(new { ok = false, message = LibraryStrings.AccessDenied })); } int sourceWidth = 0, sourceHeight = 0, sourceTop = 0, sourceLeft = 0; if (!GetImageSize(sourcePath, ref sourceWidth, ref sourceHeight)) { return(Json(new { ok = false, message = LibraryStrings.ExtensionIsNotAllowed })); } var targetWidth = width ?? (int)(sourceWidth * resize); var targetHeight = height ?? (int)(sourceHeight * resize); const int targetTop = 0; const int targetLeft = 0; sourceWidth = width.HasValue ? (int)(width.Value / resize) : sourceWidth; sourceHeight = height.HasValue ? (int)(height.Value / resize) : sourceHeight; sourceTop = top.HasValue ? (int)(top.Value / resize) : sourceTop; sourceLeft = left.HasValue ? (int)(left.Value / resize) : sourceLeft; if (System.IO.File.Exists(sourcePath)) { using (var output = new Bitmap(targetWidth, targetHeight)) { var resizer = Graphics.FromImage(output); resizer.InterpolationMode = resize < 1 ? InterpolationMode.HighQualityBicubic : InterpolationMode.HighQualityBilinear; using (var input = new Bitmap(sourcePath)) { resizer.DrawImage(input, new Rectangle(targetLeft, targetTop, targetWidth, targetHeight), sourceLeft, sourceTop, sourceWidth, sourceHeight, GraphicsUnit.Pixel); } // ReSharper disable once AssignNullToNotNullAttribute Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); if (System.IO.File.Exists(targetPath)) { System.IO.File.SetAttributes(targetPath, FileAttributes.Normal); System.IO.File.Delete(targetPath); } using (var fs = System.IO.File.OpenWrite(targetPath)) { var ext = Path.GetExtension(targetPath); output.Save(fs, GetImageCodecInfo(ext), GetEncoderParameters(ext)); } } } return(Json(new { ok = true, message = string.Empty })); }
public JsonResult Crop([FromBody] CropViewModel model) { var sourcePath = PathInfo.ConvertToPath(model.SourceFileUrl); var targetPath = model.OverwriteFile ? sourcePath : PathInfo.ConvertToPath(model.TargetFileUrl); if (!PathInfo.CheckSecurity(targetPath).Result) { return(Json(new { ok = false, message = LibraryStrings.AccessDenied })); } int sourceWidth = 0, sourceHeight = 0, sourceTop = 0, sourceLeft = 0; if (!GetImageSize(sourcePath, ref sourceWidth, ref sourceHeight)) { return(Json(new { ok = false, message = LibraryStrings.ExtensionIsNotAllowed })); } var targetWidth = model.Width ?? (int)(sourceWidth * model.Resize); var targetHeight = model.Height ?? (int)(sourceHeight * model.Resize); sourceWidth = model.Width.HasValue ? (int)(model.Width.Value / model.Resize) : sourceWidth; sourceHeight = model.Height.HasValue ? (int)(model.Height.Value / model.Resize) : sourceHeight; sourceTop = model.Top.HasValue ? (int)(model.Top.Value / model.Resize) : sourceTop; sourceLeft = model.Left.HasValue ? (int)(model.Left.Value / model.Resize) : sourceLeft; if (System.IO.File.Exists(sourcePath)) { using (var img = Image.Load(sourcePath)) { img.Mutate(n => n .Crop(new Rectangle(sourceLeft, sourceTop, sourceWidth, sourceHeight)) .Resize(targetWidth, targetHeight) ); // ReSharper disable once AssignNullToNotNullAttribute Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); if (System.IO.File.Exists(targetPath)) { System.IO.File.SetAttributes(targetPath, FileAttributes.Normal); System.IO.File.Delete(targetPath); } img.Save(targetPath); } } return(Json(new { ok = true, message = string.Empty })); }
public JsonResult CheckForCrop(string targetFileUrl) { var path = PathInfo.ConvertToPath(targetFileUrl); var ext = Path.GetExtension(path); if (System.IO.File.Exists(path)) { return(Json(new { ok = false, message = string.Format(LibraryStrings.FileExistsTryAnother, path) })); } if (string.IsNullOrEmpty(ext) || GetImageCodecInfo(ext) == null) { return(Json(new { ok = false, message = string.Format(LibraryStrings.ExtensionIsNotAllowed, ext) })); } if (!PathInfo.CheckSecurity(path).Result) { return(Json(new { ok = false, message = string.Format(LibraryStrings.AccessDenied, path, QPContext.CurrentUserName) })); } return(Json(new { ok = true, message = string.Empty })); }