public async Task <bool> WriteAndCropFile(string fileHash, OffsetModel offsetData, int sourceWidth, int sourceHeight, FileIndexItem.Rotation rotation, string reference = null) { try { using (var thumbnailStream = new MemoryStream(offsetData.Data, offsetData.Index, offsetData.Count)) using (var smallImage = await Image.LoadAsync(thumbnailStream)) using (var outputStream = new MemoryStream()) { var smallImageWidth = smallImage.Width; var smallImageHeight = smallImage.Height; var result = NewImageSize.NewImageSizeCalc(smallImageWidth, smallImageHeight, sourceWidth, sourceHeight); smallImage.Mutate( i => i.Resize(smallImageWidth, smallImageHeight, KnownResamplers.Lanczos3) .Crop(new Rectangle(result.DestX, result.DestY, result.DestWidth, result.DestHeight))); var larger = (int)Math.Round(result.DestWidth * 1.2, 0); smallImage.Mutate( i => i.Resize(larger, 0, KnownResamplers.Lanczos3)); var rotate = RotateEnumToDegrees(rotation); smallImage.Mutate( i => i.Rotate(rotate)); await smallImage.SaveAsJpegAsync(outputStream); await _thumbnailStorage.WriteStreamAsync(outputStream, ThumbnailNameHelper.Combine(fileHash, ThumbnailSize.TinyMeta)); if (_appSettings.ApplicationType == AppSettings.StarskyAppType.WebController) { _logger.LogInformation($"[WriteAndCropFile] fileHash: {fileHash} is written"); } } return(true); } catch (Exception ex) { var message = ex.Message; if (message.StartsWith("Image cannot be loaded")) { message = "Image cannot be loaded"; } _logger.LogError($"[WriteFile@meta] Exception {reference} {message}", ex); return(false); } }
public void NewImageSize_Portrait() { var sourceWidth = 2832; var sourceHeight = 4240; var smallWidth = 120; var smallHeight = 160; var rNewImageSizeCalc = NewImageSize.NewImageSizeCalc(smallWidth, smallHeight, sourceWidth, sourceHeight); Assert.AreEqual(160, rNewImageSizeCalc.DestHeight); Assert.AreEqual(106, rNewImageSizeCalc.DestWidth); Assert.AreEqual(7, rNewImageSizeCalc.DestX); Assert.AreEqual(0, rNewImageSizeCalc.DestY); }