/// <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(); } }
/// <summary> /// Sends user profile picture, or default image, to the Web Response Output. /// </summary> /// <param name="userID"></param> /// <param name="type"></param> public static void OutputProfilePicture(int userID, string type) { if (type == "2x") { type = "@2x"; } else if (!String.IsNullOrWhiteSpace(type)) { type = "-" + type; } var Response = HttpContext.Current.Response; var userFolder = GetUserPhotoFolder(userID); bool imageAdapted = false; var sizeName = profilePictureFixedSizeWidth.ToString() + "x" + profilePictureFixedSizeHeight.ToString(); // And it happens again, new size change, so the next comment, just two times: // To fix #558, because the change of size (so file name changed too), we do double try // for a while // TODO FUTURE: When there are no more oldSize (or a few, convert it manually), remove this and its use const string veryOldSizeName = "176x184"; const string oldSizeName = "112x118"; // "$avatar" // Standard name of pre-adapted image, just // serve ever the profile adapted photo for now instead the big colored original: var userPhoto = "$avatar-" + sizeName + "-gray" + type + ".jpg"; imageAdapted = true; // Physical image path to userPhoto var path = HttpContext.Current.Request.MapPath(LcUrl.RenderAppPath + userFolder + userPhoto); // Try the OLD size if it doesn't exists the new: if (!System.IO.File.Exists(path)) { imageAdapted = false; // Update name and path userPhoto = "$avatar-" + oldSizeName + "-gray" + type + ".jpg"; path = HttpContext.Current.Request.MapPath(LcUrl.RenderAppPath + userFolder + userPhoto); } // Try the VERY OLD size if it doesn't exists the new: if (!System.IO.File.Exists(path)) { imageAdapted = false; // Update name and path userPhoto = "$avatar-" + veryOldSizeName + "-gray" + type + ".jpg"; path = HttpContext.Current.Request.MapPath(LcUrl.RenderAppPath + userFolder + userPhoto); } // Last fallback: default image if (!System.IO.File.Exists(path)) { imageAdapted = true; if (type == "@2x") { userPhoto = "img/userphotos/u0/avatar-2x.png"; } else { userPhoto = "img/userphotos/u0/avatar.png"; } path = HttpContext.Current.Server.MapPath(LcUrl.RenderAppPath + userPhoto); } try { if (imageAdapted) { new WebImage(path).Write(); } else { // Transform image to Grayscale and profile photo size to avoid big images: // Is inside a using block because ensure close the image ressources and the file using (var img = LcImaging.Grayscale(LcImaging.Resize(new System.Drawing.Bitmap(path), profilePictureFixedSizeWidth, profilePictureFixedSizeHeight, profilePictureSizeMode, LcImaging.AnchorPosition.Center))) { // Telling to the browser that this is a JPG image Response.ContentType = "image/jpg"; // Send the transformed image to the browser img.Save(Response.OutputStream, ImageFormat.Jpeg); } } } catch { } }