public async Task <IReadOnlyList <ImageSize> > BuildImagesAsync(Photo sourcePhoto, List <string> filesCreated, DateTime creationDate, string url, string shortUrl, IImageSettings imageSettings) { string rawExtension = sourcePhoto.ImageExtension.TrimStart(trimChar: '.') .ToUpperInvariant(); string filename = this._sourceImageFileLocator.GetFilename(sourcePhoto); if (!this._imageLoader.CanLoad(filename)) { this._logging.LogError($"No image converter for {rawExtension}"); return(Array.Empty <ImageSize>()); } List <ImageSize> sizes = new(); IReadOnlyList <int> imageSizes = StandardImageSizesWithThumbnailSize(imageSettings); this._logging.LogInformation($"Loading image: {filename}"); using (Image <Rgba32> sourceBitmap = await this._imageLoader.LoadImageAsync(filename)) { if (sourceBitmap == null) { this._logging.LogWarning($"Could not load : {filename}"); return(null); } this._logging.LogInformation($"Loaded: {filename}"); int sourceImageWidth = sourceBitmap.Width; this._logging.LogInformation($"Using Image Width: {sourceBitmap.Width}"); foreach (int dimension in imageSizes.Where(predicate: size => ResziedImageWillNotBeBigger(size: size, sourceImageWidth: sourceImageWidth))) { this._logging.LogDebug($"Creating Dimension: {dimension}"); using (Image <Rgba32> resized = ResizeImage(image: sourceBitmap, maximumDimension: dimension)) { string resizedFileName = this._resizeImageFileLocator.GetResizedFileName(sourcePhoto: sourcePhoto, new ImageSize { Width = resized.Width, Height = resized.Height }); ApplyWatermark(imageToAddWatermarkTo: resized, url: shortUrl, imageSettings: imageSettings); long quality = imageSettings.JpegOutputQuality; byte[] resizedBytes = this.SaveImageAsJpegBytes(image: resized, compressionQuality: quality, url: url, shortUrl: shortUrl, metadata: sourcePhoto.Metadata, creationDate: creationDate, imageSettings: imageSettings); if (!ImageHelpers.IsValidJpegImage(bytes: resizedBytes, "In memory image to be saved as: " + resizedFileName)) { throw new AbortProcessingException(string.Format(format: "File {0} produced an invalid image", arg0: filename)); } await this.WriteImageAsync(fileName : resizedFileName, data : resizedBytes, creationDate : creationDate); byte[] resizedData = await File.ReadAllBytesAsync(resizedFileName); if (!ImageHelpers.IsValidJpegImage(bytes: resizedData, "Saved resize image: " + resizedFileName)) { this._logging.LogError($"Error: File {resizedFileName} produced an invalid image"); throw new AbortProcessingException(string.Format(format: "File {0} produced an invalid image", arg0: filename)); } filesCreated.Add(HashNaming.PathifyHash(sourcePhoto.PathHash) + "\\" + this._imageFilenameGeneration.IndividualResizeFileName(sourcePhoto: sourcePhoto, resized: resized)); if (resized.Width == imageSettings.ThumbnailSize) { resizedFileName = this._resizeImageFileLocator.GetResizedFileName(sourcePhoto: sourcePhoto, new ImageSize { Width = resized.Width, Height = resized.Height }, extension: "png"); resizedBytes = SaveImageAsPng(image: resized, url: url, metadata: sourcePhoto.Metadata, creationDate: creationDate); await this.WriteImageAsync(fileName : resizedFileName, data : resizedBytes, creationDate : creationDate); filesCreated.Add(HashNaming.PathifyHash(sourcePhoto.PathHash) + "\\" + this._imageFilenameGeneration.IndividualResizeFileName(sourcePhoto: sourcePhoto, resized: resized, extension: "png")); } sizes.Add(new ImageSize { Width = resized.Width, Height = resized.Height }); } } } return(sizes); }
public string GetResizedFileName(Photo sourcePhoto, ImageSize resized, string extension) { return(Path.Combine(path1: this._settings.ImagesOutputPath, HashNaming.PathifyHash(sourcePhoto.PathHash), this._imageFilenameGeneration.IndividualResizeFileName(sourcePhoto: sourcePhoto, resized: resized, extension: extension))); }