/// <summary> /// Saves a stream as images of multiple assorted sizes /// </summary> /// <param name="stream">The Stream containing the image data to be saved</param> /// <param name="name">The base image name</param> /// <param name="sizes">The list of image widths to be created</param> /// <returns>The URL by which the base file is accessible</returns> public async Task <string> SaveImageMultipleSizes(Stream stream, string name, List <int> sizes = null, string timeStamp = "") { if (timeStamp != "") { // Timestamp the filename to prevent collisions name = GetTimeStampedFileName(name, timeStamp); } if (sizes == null) { sizes = _imageSizes; } List <string> srcSetItems = new List <string>(); // For small images (smaller than largest desired width), remove target sizes that are larger than original image, // so as not to attempt expanding image. int imageWidth = new ImageResizer(stream).GetImageWidth(); sizes = GetAdjustedSizeList(sizes, imageWidth); MemoryStream memoryStream = stream.CloneToMemoryStream(); for (var i = 0; i < sizes.Count; i++) { // timeStamped = false, so not to double timestamp string url = await SaveFile(memoryStream, name, sizes[i]); srcSetItems.Add(url + " " + sizes[i] + "w"); } string srcset = string.Join(", ", srcSetItems); return(srcset); }
/// <summary> /// Saves an image stream /// </summary> /// <param name="stream">The Stream object containing the image data</param> /// <param name="name">The name under which to save the images</param> /// <param name="maxWidth">The maximum width of the image in pixels</param> /// <returns>The URL by which the file is accessible</returns> public async Task <string> SaveFile(Stream stream, string name, int?maxWidth, string timeStamp = "") { if (timeStamp != "") { // Timestamp the filename to prevent collisions name = GetTimeStampedFileName(name, timeStamp); } if (maxWidth != null) { ImageResizer imageResizer = new ImageResizer(stream); int imageWidth = imageResizer.GetImageWidth(); if (imageWidth > maxWidth) { Stream resizedStream = imageResizer.GetResizedImageStream((int)(maxWidth)); stream = resizedStream; imageWidth = (int)(maxWidth); } name = GetResizedFileName(name, (int)(imageWidth)); } // timeStamped = false, so not to double timestamp return(await base.SaveFile(stream, name)); }