/// <summary> /// Create from the 'editable image' processed versions: cropped with the given values and /// optimized sizes. /// </summary> /// <param name="virtualPath"></param> private static string ProcessWorkPhoto(string path, string fileName, int x, int y, int width, int height) { var regularFileName = ""; // fileName could be given by a previous save including suffixes, // we need it without suffixes in order to work properly: fileName = System.IO.Path.GetFileNameWithoutExtension(LcUtils.GetNameWithoutSuffix(fileName)); // Remove previous cropped/sized/adapted photos (except editable one), all start with fileName plus dash // File.Delete doesn't allow wildcards, find and delete each one foreach (var f in Directory.GetFiles(path, fileName + "-*", SearchOption.TopDirectoryOnly)) { File.Delete(f); } Image cropImg = null; // Create optimized files using (var img = System.Drawing.Image.FromFile(path + fileName + ".jpg")) { // Crop, if any size // On cropping: // User used the reduced image (fixed size) to choose what to crop, // while we will crop the 'original', x larger image, so multiply every unit cropImg = width > 0 ? LcImaging.Crop(img, x * WorkPhotoOriginalScale, y * WorkPhotoOriginalScale, width * WorkPhotoOriginalScale, height * WorkPhotoOriginalScale) : img; // Size prefix var sizeName = "-" + FixedSizeWidth.ToString() + "x" + FixedSizeHeight.ToString(); // Save image with regular size using (var modImg = LcImaging.Resize(cropImg, FixedSizeWidth, FixedSizeHeight, LcImaging.SizeMode.Contain, LcImaging.AnchorPosition.Center, DefaultBackground)) { regularFileName = fileName + sizeName + ".jpg"; modImg.Save(path + regularFileName, System.Drawing.Imaging.ImageFormat.Jpeg); } // Same as previous but for hi-res 2x devices: (real pixel sizes is double but preserve the original size name to recognize it better adding the @2x suffix) using (var modImg = LcImaging.Resize(cropImg, FixedSizeWidth * 2, FixedSizeHeight * 2, LcImaging.SizeMode.Contain, LcImaging.AnchorPosition.Center, DefaultBackground)) { modImg.Save(path + fileName + sizeName + "@2x.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } // NOTE Creation of images with more sizes (for small user widgets on reviews/bookings/etc) or filters go here } // if there was a crop: if (width > 0) { // TODO To Review when we enable cropping at UI again, because this gets more complicated with rotation and keeping original orientation // at the 'editable' photo; maybe the cropping must happen when saving the editable version, and not here at each optimized image //// Replace original image with the cropped version, for future 'crop again' tasks //using (var replacedEditableImg = LcImaging.Resize(cropImg, FixedSizeWidth * 4, FixedSizeHeight * 4, LcImaging.SizeMode.Contain, LcImaging.AnchorPosition.Center, DefaultBackground)) //{ // replacedEditableImg.Save(path + fileName + ".jpg"); //} } cropImg.Dispose(); return(regularFileName); }
/// <summary> /// Sets the previously uploaded 'editable avatar' image as the /// current user avatar, cropped with the given values and /// optimized sizes. /// </summary> /// <param name="virtualPath"></param> public static void SaveProfilePicture(int userID, int x, int y, int width, int height) { string virtualPath = LcUrl.RenderAppPath + GetUserPhotoFolder(userID); var folder = System.Web.HttpContext.Current.Server.MapPath(virtualPath); // Remove previous cropped/sized/adapted photos (except editable one), all start with avatarNamePrefix // File.Delete doesn't allow wildcards, find and delete each one foreach (var f in Directory.GetFiles(folder, avatarNamePrefix + "*", SearchOption.TopDirectoryOnly)) { File.Delete(f); } // Create optimized files using (var img = System.Drawing.Image.FromFile(folder + avatarName + ".jpg")) { // Crop var cropImg = LcImaging.Crop(img, x, y, width, height); img.Dispose(); // Size prefix var sizeName = profilePictureFixedSizeWidth.ToString() + "x" + profilePictureFixedSizeHeight.ToString(); // Save image with profile size and original color using (var modImg = LcImaging.Resize(cropImg, profilePictureFixedSizeWidth, profilePictureFixedSizeHeight, LcImaging.SizeMode.Cover, LcImaging.AnchorPosition.Center)) { modImg.Save(folder + avatarNamePrefix + sizeName + ".jpg"); } // Save image with profile size and grayscale (-gray) using (var modImg = LcImaging.Grayscale(LcImaging.Resize(cropImg, profilePictureFixedSizeWidth, profilePictureFixedSizeHeight, LcImaging.SizeMode.Cover, LcImaging.AnchorPosition.Center))) { modImg.Save(folder + avatarNamePrefix + sizeName + "-gray.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); } // Same as previous but for hi-res 2x devices: (real pixel sizes is double but preserve the original size name to recognize it better adding the @2x suffix using (var modImg = LcImaging.Grayscale(LcImaging.Resize(cropImg, profilePictureFixedSizeWidth * 2, profilePictureFixedSizeHeight * 2, LcImaging.SizeMode.Cover, LcImaging.AnchorPosition.Center))) { modImg.Save(folder + avatarNamePrefix + sizeName + "*****@*****.**", System.Drawing.Imaging.ImageFormat.Jpeg); } // NOTE Creation of images with more sizes (for small user widgets on reviews/bookings/etc) or filters go here cropImg.Dispose(); } }