public string CropImage(CropperSetting setting, string additionalFolder = "", string[] allowedExtensions = null) { var extension = System.IO.Path.GetExtension(setting.Image.FileName).ToLower(); string filename = System.IO.Path.GetFileNameWithoutExtension(setting.Image.FileName); filename = filename + "_" + Guid.NewGuid().ToString("N"); filename = FixName(filename) + extension; var dirPath = System.IO.Path.Combine(_staticFilePath, additionalFolder); CreateDirectoryIfNotExists(dirPath); var imagePath = System.IO.Path.Combine(additionalFolder, filename); var fullPath = System.IO.Path.Combine(_staticFilePath, imagePath); using Image image = SixLabors.ImageSharp.Image.Load(setting.Image.OpenReadStream()); image.Mutate(x => x.Crop(new Rectangle(setting.X, setting.Y, setting.Width, setting.Height))); var resizeWidth = 0; var resizeHeight = 0; if (setting.Width >= setting.Height && setting.Width >= setting.MaxSize) { var dif = setting.Width / (1.0 * setting.MaxSize); resizeWidth = setting.MaxSize; resizeHeight = Convert.ToInt32(setting.Height / dif); } else if (setting.Height > setting.Width && setting.Height >= setting.MaxSize) { var dif = setting.Height / (1.0 * setting.MaxSize); resizeHeight = setting.MaxSize; resizeWidth = Convert.ToInt32(setting.Width / dif); } if (resizeWidth > 0 && resizeHeight > 0) { image.Mutate(x => x.Resize(resizeWidth, resizeHeight)); } if (extension == ".jpg" || extension == ".jpeg") { var encoder = new JpegEncoder() { Quality = 90, }; image.Save(fullPath, encoder); } else { image.Save(fullPath); } return(imagePath); }
public IActionResult CropImage(CropperSetting cropperData, string path = "") { var imagePath = _fileService.CropImage(cropperData, path); return(Ok(Result <string> .Success(imagePath))); }