private CropResizeResultDTO CropFile(IFileInfo file, string newFilename, CropDTO crop, ResizeDTO resize, IFolderInfo userFolder) { var cropresult = new CropResizeResultDTO(); if (crop != null) { cropresult.crop = crop; } else { cropresult.crop = new CropDTO(); } var folderManager = FolderManager.Instance; var fileManager = FileManager.Instance; var folder = folderManager.GetFolder(file.FolderId); var image = Image.FromFile(file.PhysicalPath); Image imageCropped = null; if (crop == null || crop.x < 0 || crop.y < 0) // center { int left = 0; int top = 0; int width = 0; int height = 0; imageCropped = ImageHelper.SaveCroppedImage(image, resize.width, resize.height, out left, out top, out width, out height); cropresult.crop.x = left; cropresult.crop.y = top; cropresult.crop.width = width; cropresult.crop.height = height; } else if (crop.width > 0 && crop.width > 0) { imageCropped = ImageHelper.Crop(image, crop.x, crop.y, crop.width, crop.height); if (resize != null && resize.width > 0 && resize.height > 0) { imageCropped = ImageHelper.Resize(imageCropped, resize.width, resize.height); } } Stream content = new MemoryStream(); ImageFormat imgFormat = ImageFormat.Bmp; if (file.Extension.ToLowerInvariant() == "png") { imgFormat = ImageFormat.Png; } else if (file.Extension.ToLowerInvariant() == "gif") { imgFormat = ImageFormat.Gif; } else if (file.Extension.ToLowerInvariant() == "jpg") { imgFormat = ImageFormat.Jpeg; } if (imageCropped != null) { imageCropped.Save(content, imgFormat); var newFile = fileManager.AddFile(userFolder, newFilename, content, true); cropresult.url = newFile.ToUrl(); return(cropresult); } return(null); }
public HttpResponseMessage CropImage(CropResizeDTO cropData) { FilesStatus fs = null; try { var res = new CropResizeResultDTO() { crop = new CropDTO() { x = cropData.crop.x, y = cropData.crop.y, width = cropData.crop.width, height = cropData.crop.height } }; var folderManager = FolderManager.Instance; var fileManager = FileManager.Instance; string rawImageUrl = cropData.url; if (rawImageUrl.IndexOf('?') > 0) { rawImageUrl = rawImageUrl.Substring(0, rawImageUrl.IndexOf('?')); } rawImageUrl = rawImageUrl.Replace(PortalSettings.HomeDirectory, ""); var file = fileManager.GetFile(ActiveModule.PortalID, rawImageUrl); string cropfolder = "OpenContent/Cropped/" + ActiveModule.ModuleID; if (!string.IsNullOrEmpty(cropData.itemKey)) { cropfolder += "/" + cropData.itemKey; } if (!string.IsNullOrEmpty(cropData.cropfolder)) { cropfolder = cropData.cropfolder; } var userFolder = folderManager.GetFolder(PortalSettings.PortalId, cropfolder); if (userFolder == null) { userFolder = folderManager.AddFolder(PortalSettings.PortalId, cropfolder); } string newFilename = Path.GetFileNameWithoutExtension(file.FileName) + "-" + cropData.id + Path.GetExtension(file.FileName); if (file != null) { //var folder = folderManager.GetFolder(file.FolderId); var image = Image.FromFile(file.PhysicalPath); Image imageCropped; //int x = cropData.crop.x; //int y = cropData.crop.y; if (cropData.crop.x < 0 && cropData.crop.y < 0) // center { int left = 0; int top = 0; int width = 0; int height = 0; imageCropped = ImageHelper.SaveCroppedImage(image, cropData.crop.width, cropData.crop.height, out left, out top, out width, out height); res.crop.x = left; res.crop.y = top; res.crop.width = width; res.crop.height = height; } else { imageCropped = ImageHelper.Crop(image, cropData.crop.x, cropData.crop.y, cropData.crop.width, cropData.crop.height); if (cropData.resize != null && cropData.resize.width > 0 && cropData.resize.height > 0) { imageCropped = ImageHelper.Resize(imageCropped, cropData.resize.width, cropData.resize.height); } } Stream content = new MemoryStream(); ImageFormat imgFormat = ImageFormat.Bmp; if (file.Extension.ToLowerInvariant() == "png") { imgFormat = ImageFormat.Png; } else if (file.Extension.ToLowerInvariant() == "gif") { imgFormat = ImageFormat.Gif; } else if (file.Extension.ToLowerInvariant() == "jpg") { imgFormat = ImageFormat.Jpeg; } imageCropped.Save(content, imgFormat); var newFile = fileManager.AddFile(userFolder, newFilename, content, true); fs = new FilesStatus() { success = true, name = newFile.FileName, extension = newFile.Extension, type = newFile.ContentType, size = newFile.Size, progress = "1.0", url = FileManager.Instance.GetUrl(newFile), //thumbnail_url = fileIcon, message = "success", id = newFile.FileId, }; res.url = fs.url.RemoveCachebuster(); } return(Request.CreateResponse(HttpStatusCode.OK, res)); } catch (Exception exc) { Logger.Error(exc); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc)); } }