From 7b120519538734e22c26943a100846b74686f2b9 Mon Sep 17 00:00:00 2001 From: Salih Date: Fri, 26 May 2023 19:09:51 +0300 Subject: [PATCH] Reformatting --- .../Imaging/IImageCompressorContributor.cs | 11 +- .../Volo/Abp/Imaging/IImageResizer.cs | 11 +- .../Abp/Imaging/IImageResizerContributor.cs | 13 +- .../Volo/Abp/Imaging/ImageCompressor.cs | 1 + .../Volo/Abp/Imaging/ImageResizer.cs | 15 +- .../Abp/Imaging/CompressImageAttribute.cs | 35 +-- .../Volo/Abp/Imaging/ResizeImageAttribute.cs | 35 +-- .../Abp/Imaging/ImageSharpCompressOptions.cs | 2 + .../ImageSharpImageCompressorContributor.cs | 13 +- .../ImageSharpImageResizerContributor.cs | 18 +- .../MagickImageCompressorContributor.cs | 9 +- .../Imaging/MagickImageResizerContributor.cs | 236 ++++++++++-------- 12 files changed, 249 insertions(+), 150 deletions(-) diff --git a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageCompressorContributor.cs b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageCompressorContributor.cs index 417e81ba2c..84087d983a 100644 --- a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageCompressorContributor.cs +++ b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageCompressorContributor.cs @@ -1,11 +1,18 @@ using System.IO; using System.Threading; using System.Threading.Tasks; +using JetBrains.Annotations; namespace Volo.Abp.Imaging; public interface IImageCompressorContributor { - Task> TryCompressAsync(Stream stream, string mimeType = null, CancellationToken cancellationToken = default); - Task> TryCompressAsync(byte[] bytes, string mimeType = null, CancellationToken cancellationToken = default); + Task> TryCompressAsync( + Stream stream, + [CanBeNull] string mimeType = null, + CancellationToken cancellationToken = default); + Task> TryCompressAsync( + byte[] bytes, + [CanBeNull] string mimeType = null, + CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageResizer.cs b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageResizer.cs index e3de33dfd2..edcc911ff6 100644 --- a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageResizer.cs +++ b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageResizer.cs @@ -7,7 +7,14 @@ namespace Volo.Abp.Imaging; public interface IImageResizer { - Task> ResizeAsync(Stream stream, ImageResizeArgs resizeArgs, [CanBeNull]string mimeType = null, CancellationToken cancellationToken = default); + Task> ResizeAsync(Stream stream, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, + CancellationToken cancellationToken = default); - Task> ResizeAsync(byte[] bytes, ImageResizeArgs resizeArgs, [CanBeNull] string mimeType = null, CancellationToken cancellationToken = default); + Task> ResizeAsync( + byte[] bytes, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, + CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageResizerContributor.cs b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageResizerContributor.cs index 2e189fea1d..fed4ce4a4c 100644 --- a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageResizerContributor.cs +++ b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/IImageResizerContributor.cs @@ -1,12 +1,21 @@ using System.IO; using System.Threading; using System.Threading.Tasks; +using JetBrains.Annotations; namespace Volo.Abp.Imaging; public interface IImageResizerContributor { - Task> TryResizeAsync(Stream stream, ImageResizeArgs resizeArgs, string mimeType = null, CancellationToken cancellationToken = default); + Task> TryResizeAsync( + Stream stream, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, + CancellationToken cancellationToken = default); - Task> TryResizeAsync(byte[] bytes, ImageResizeArgs resizeArgs, string mimeType = null, CancellationToken cancellationToken = default); + Task> TryResizeAsync( + byte[] bytes, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, + CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs index fc968e872c..a2c46b1c21 100644 --- a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs +++ b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageCompressor.cs @@ -41,6 +41,7 @@ public class ImageCompressor : IImageCompressor, ITransientDependency foreach (var imageCompressorContributor in ImageCompressorContributors) { var result = await imageCompressorContributor.TryCompressAsync(bytes, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken)); + if (result.State == ProcessState.Unsupported) { continue; diff --git a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs index 59e3da0d8d..c75aa8bf23 100644 --- a/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs +++ b/framework/src/Volo.Abp.Imaging.Abstractions/Volo/Abp/Imaging/ImageResizer.cs @@ -2,6 +2,7 @@ using System.IO; using System.Threading; using System.Threading.Tasks; +using JetBrains.Annotations; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.Threading; @@ -26,13 +27,18 @@ public class ImageResizer : IImageResizer, ITransientDependency ImageResizeOptions = imageResizeOptions.Value; } - public virtual async Task> ResizeAsync(Stream stream, ImageResizeArgs resizeArgs, string mimeType = null, CancellationToken cancellationToken = default) + public virtual async Task> ResizeAsync( + Stream stream, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, + CancellationToken cancellationToken = default) { ChangeDefaultResizeMode(resizeArgs); foreach (var imageResizerContributor in ImageResizerContributors) { var result = await imageResizerContributor.TryResizeAsync(stream, resizeArgs, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken)); + if (result.State == ProcessState.Unsupported) { continue; @@ -44,13 +50,18 @@ public class ImageResizer : IImageResizer, ITransientDependency return new ImageResizeResult(stream, ProcessState.Unsupported); } - public virtual async Task> ResizeAsync(byte[] bytes, ImageResizeArgs resizeArgs, string mimeType = null, CancellationToken cancellationToken = default) + public virtual async Task> ResizeAsync( + byte[] bytes, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, + CancellationToken cancellationToken = default) { ChangeDefaultResizeMode(resizeArgs); foreach (var imageResizerContributor in ImageResizerContributors) { var result = await imageResizerContributor.TryResizeAsync(bytes, resizeArgs, mimeType, CancellationTokenProvider.FallbackToProvider(cancellationToken)); + if (result.State == ProcessState.Unsupported) { continue; diff --git a/framework/src/Volo.Abp.Imaging.AspNetCore/Volo/Abp/Imaging/CompressImageAttribute.cs b/framework/src/Volo.Abp.Imaging.AspNetCore/Volo/Abp/Imaging/CompressImageAttribute.cs index 4563d4976e..7d5ff1be1a 100644 --- a/framework/src/Volo.Abp.Imaging.AspNetCore/Volo/Abp/Imaging/CompressImageAttribute.cs +++ b/framework/src/Volo.Abp.Imaging.AspNetCore/Volo/Abp/Imaging/CompressImageAttribute.cs @@ -53,13 +53,14 @@ public class CompressImageAttribute : ActionFilterAttribute } var result = await imageCompressor.CompressAsync(file.OpenReadStream(), file.ContentType); - - if (result.State == ProcessState.Done) + + if (result.State != ProcessState.Done) { - return new FormFile(result.Result, 0, result.Result.Length, file.Name, file.FileName); + return file; } - return file; + return new FormFile(result.Result, 0, result.Result.Length, file.Name, file.FileName); + } protected virtual async Task CompressImageAsync(IRemoteStreamContent remoteStreamContent, IImageCompressor imageCompressor) @@ -70,29 +71,31 @@ public class CompressImageAttribute : ActionFilterAttribute } var result = await imageCompressor.CompressAsync(remoteStreamContent.GetStream(), remoteStreamContent.ContentType); - - if (result.State == ProcessState.Done) + + if (result.State != ProcessState.Done) { - var fileName = remoteStreamContent.FileName; - var contentType = remoteStreamContent.ContentType; - remoteStreamContent.Dispose(); - return new RemoteStreamContent(result.Result, fileName, contentType); + return remoteStreamContent; } - return remoteStreamContent; + var fileName = remoteStreamContent.FileName; + var contentType = remoteStreamContent.ContentType; + remoteStreamContent.Dispose(); + return new RemoteStreamContent(result.Result, fileName, contentType); + } protected virtual async Task CompressImageAsync(Stream stream, IImageCompressor imageCompressor) { var result = await imageCompressor.CompressAsync(stream); - - if (result.State == ProcessState.Done) + + if (result.State != ProcessState.Done) { - await stream.DisposeAsync(); - return result.Result; + return stream; } - return stream; + await stream.DisposeAsync(); + return result.Result; + } protected virtual async Task CompressImageAsync(byte[] bytes, IImageCompressor imageCompressor) diff --git a/framework/src/Volo.Abp.Imaging.AspNetCore/Volo/Abp/Imaging/ResizeImageAttribute.cs b/framework/src/Volo.Abp.Imaging.AspNetCore/Volo/Abp/Imaging/ResizeImageAttribute.cs index a6708b97bf..067b4280c5 100644 --- a/framework/src/Volo.Abp.Imaging.AspNetCore/Volo/Abp/Imaging/ResizeImageAttribute.cs +++ b/framework/src/Volo.Abp.Imaging.AspNetCore/Volo/Abp/Imaging/ResizeImageAttribute.cs @@ -66,13 +66,14 @@ public class ResizeImageAttribute : ActionFilterAttribute } var result = await imageResizer.ResizeAsync(file.OpenReadStream(), new ImageResizeArgs(Width, Height, Mode), file.ContentType); - - if (result.State == ProcessState.Done) + + if (result.State != ProcessState.Done) { - return new FormFile(result.Result, 0, result.Result.Length, file.Name, file.FileName); + return file; } - return file; + return new FormFile(result.Result, 0, result.Result.Length, file.Name, file.FileName); + } protected virtual async Task ResizeImageAsync(IRemoteStreamContent remoteStreamContent, IImageResizer imageResizer) @@ -83,29 +84,31 @@ public class ResizeImageAttribute : ActionFilterAttribute } var result = await imageResizer.ResizeAsync(remoteStreamContent.GetStream(), new ImageResizeArgs(Width, Height, Mode), remoteStreamContent.ContentType); - - if (result.State == ProcessState.Done) + + if (result.State != ProcessState.Done) { - var fileName = remoteStreamContent.FileName; - var contentType = remoteStreamContent.ContentType; - remoteStreamContent.Dispose(); - return new RemoteStreamContent(result.Result, fileName, contentType); + return remoteStreamContent; } - return remoteStreamContent; + var fileName = remoteStreamContent.FileName; + var contentType = remoteStreamContent.ContentType; + remoteStreamContent.Dispose(); + return new RemoteStreamContent(result.Result, fileName, contentType); + } protected virtual async Task ResizeImageAsync(Stream stream, IImageResizer imageResizer) { var result = await imageResizer.ResizeAsync(stream, new ImageResizeArgs(Width, Height, Mode)); - - if (result.State == ProcessState.Done) + + if (result.State != ProcessState.Done) { - await stream.DisposeAsync(); - return result.Result; + return stream; } - return stream; + await stream.DisposeAsync(); + return result.Result; + } protected virtual async Task ResizeImageAsync(byte[] bytes, IImageResizer imageResizer) diff --git a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpCompressOptions.cs b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpCompressOptions.cs index ff7fd69739..3fc291da6f 100644 --- a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpCompressOptions.cs +++ b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpCompressOptions.cs @@ -19,10 +19,12 @@ public class ImageSharpCompressOptions JpegEncoder = new JpegEncoder { Quality = DefaultQuality }; + PngEncoder = new PngEncoder { CompressionLevel = PngCompressionLevel.BestCompression, IgnoreMetadata = true }; + WebpEncoder = new WebpEncoder { Quality = DefaultQuality }; diff --git a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageCompressorContributor.cs b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageCompressorContributor.cs index ab7f64540e..ace303ce25 100644 --- a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageCompressorContributor.cs +++ b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageCompressorContributor.cs @@ -2,6 +2,7 @@ using System.IO; using System.Threading; using System.Threading.Tasks; +using JetBrains.Annotations; using Microsoft.Extensions.Options; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Advanced; @@ -23,7 +24,9 @@ public class ImageSharpImageCompressorContributor : IImageCompressorContributor, Options = options.Value; } - public virtual async Task> TryCompressAsync(Stream stream, string mimeType = null, + public virtual async Task> TryCompressAsync( + Stream stream, + [CanBeNull] string mimeType = null, CancellationToken cancellationToken = default) { if (!string.IsNullOrWhiteSpace(mimeType) && !CanCompress(mimeType)) @@ -49,7 +52,9 @@ public class ImageSharpImageCompressorContributor : IImageCompressorContributor, return new ImageCompressResult(stream, ProcessState.Canceled); } - public virtual async Task> TryCompressAsync(byte[] bytes, string mimeType = null, + public virtual async Task> TryCompressAsync( + byte[] bytes, + [CanBeNull] string mimeType = null, CancellationToken cancellationToken = default) { if (!string.IsNullOrWhiteSpace(mimeType) && !CanCompress(mimeType)) @@ -80,7 +85,9 @@ public class ImageSharpImageCompressorContributor : IImageCompressorContributor, }; } - protected virtual async Task GetStreamFromImageAsync(Image image, IImageFormat format, + protected virtual async Task GetStreamFromImageAsync( + Image image, + IImageFormat format, CancellationToken cancellationToken = default) { var memoryStream = new MemoryStream(); diff --git a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageResizerContributor.cs b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageResizerContributor.cs index e3920b0c6d..ed7dd08783 100644 --- a/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageResizerContributor.cs +++ b/framework/src/Volo.Abp.Imaging.ImageSharp/Volo/Abp/Imaging/ImageSharpImageResizerContributor.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; +using JetBrains.Annotations; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Processing; using Volo.Abp.DependencyInjection; @@ -12,8 +13,10 @@ namespace Volo.Abp.Imaging; public class ImageSharpImageResizerContributor : IImageResizerContributor, ITransientDependency { - public virtual async Task> TryResizeAsync(Stream stream, ImageResizeArgs resizeArgs, - string mimeType = null, + public virtual async Task> TryResizeAsync( + Stream stream, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, CancellationToken cancellationToken = default) { if (!string.IsNullOrWhiteSpace(mimeType) && !CanResize(mimeType)) @@ -52,8 +55,10 @@ public class ImageSharpImageResizerContributor : IImageResizerContributor, ITran } } - public virtual async Task> TryResizeAsync(byte[] bytes, ImageResizeArgs resizeArgs, - string mimeType = null, + public virtual async Task> TryResizeAsync( + byte[] bytes, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, CancellationToken cancellationToken = default) { if (!string.IsNullOrWhiteSpace(mimeType) && !CanResize(mimeType)) @@ -99,10 +104,11 @@ public class ImageSharpImageResizerContributor : IImageResizerContributor, ITran { ImageResizeMode.Crop, ResizeMode.Crop }, { ImageResizeMode.Pad, ResizeMode.Pad } }; - - private Size GetSize(ImageResizeArgs resizeArgs) + + private static Size GetSize(ImageResizeArgs resizeArgs) { var size = new Size(); + if (resizeArgs.Width > 0) { size.Width = resizeArgs.Width; diff --git a/framework/src/Volo.Abp.Imaging.MagickNet/Volo/Abp/Imaging/MagickImageCompressorContributor.cs b/framework/src/Volo.Abp.Imaging.MagickNet/Volo/Abp/Imaging/MagickImageCompressorContributor.cs index 92663b76fa..8242645eef 100644 --- a/framework/src/Volo.Abp.Imaging.MagickNet/Volo/Abp/Imaging/MagickImageCompressorContributor.cs +++ b/framework/src/Volo.Abp.Imaging.MagickNet/Volo/Abp/Imaging/MagickImageCompressorContributor.cs @@ -3,6 +3,7 @@ using System.IO; using System.Threading; using System.Threading.Tasks; using ImageMagick; +using JetBrains.Annotations; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.Http; @@ -25,7 +26,9 @@ public class MagickImageCompressorContributor : IImageCompressorContributor, ITr }; } - public virtual async Task> TryCompressAsync(Stream stream, string mimeType = null, + public virtual async Task> TryCompressAsync( + Stream stream, + [CanBeNull] string mimeType = null, CancellationToken cancellationToken = default) { if (!string.IsNullOrWhiteSpace(mimeType) && !CanCompress(mimeType)) @@ -58,7 +61,9 @@ public class MagickImageCompressorContributor : IImageCompressorContributor, ITr } } - public virtual async Task> TryCompressAsync(byte[] bytes, string mimeType = null, + public virtual async Task> TryCompressAsync( + byte[] bytes, + [CanBeNull] string mimeType = null, CancellationToken cancellationToken = default) { if (!string.IsNullOrWhiteSpace(mimeType) && !CanCompress(mimeType)) diff --git a/framework/src/Volo.Abp.Imaging.MagickNet/Volo/Abp/Imaging/MagickImageResizerContributor.cs b/framework/src/Volo.Abp.Imaging.MagickNet/Volo/Abp/Imaging/MagickImageResizerContributor.cs index 7fbb2682df..1d221355d1 100644 --- a/framework/src/Volo.Abp.Imaging.MagickNet/Volo/Abp/Imaging/MagickImageResizerContributor.cs +++ b/framework/src/Volo.Abp.Imaging.MagickNet/Volo/Abp/Imaging/MagickImageResizerContributor.cs @@ -3,6 +3,7 @@ using System.IO; using System.Threading; using System.Threading.Tasks; using ImageMagick; +using JetBrains.Annotations; using Volo.Abp.DependencyInjection; using Volo.Abp.Http; @@ -10,8 +11,12 @@ namespace Volo.Abp.Imaging; public class MagickImageResizerContributor : IImageResizerContributor, ITransientDependency { - public virtual async Task> TryResizeAsync(Stream stream, ImageResizeArgs resizeArgs, - string mimeType = null, + private const int Min = 1; + + public virtual async Task> TryResizeAsync( + Stream stream, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, CancellationToken cancellationToken = default) { if (!mimeType.IsNullOrWhiteSpace() && !CanResize(mimeType)) @@ -46,15 +51,17 @@ public class MagickImageResizerContributor : IImageResizerContributor, ITransien } } - public virtual Task> TryResizeAsync(byte[] bytes, ImageResizeArgs resizeArgs, - string mimeType = null, + public virtual Task> TryResizeAsync( + byte[] bytes, + ImageResizeArgs resizeArgs, + [CanBeNull] string mimeType = null, CancellationToken cancellationToken = default) { if (!mimeType.IsNullOrWhiteSpace() && !CanResize(mimeType)) { return Task.FromResult(new ImageResizeResult(bytes, ProcessState.Unsupported)); } - + using var image = new MagickImage(bytes); if (mimeType.IsNullOrWhiteSpace() && !CanResize(image.FormatInfo?.MimeType)) @@ -80,148 +87,119 @@ public class MagickImageResizerContributor : IImageResizerContributor, ITransien }; } - protected virtual void Resize(MagickImage image, ImageResizeArgs resizeParameter) + protected virtual void Resize(MagickImage image, ImageResizeArgs resizeArgs) { - const int min = 1; - - var sourceWidth = image.Width; - var sourceHeight = image.Height; - - var targetWidth = GetTargetWidth(resizeParameter.Width, resizeParameter.Height, min, sourceWidth, sourceHeight); - var targetHeight = GetTargetHeight(resizeParameter.Height, targetWidth, min, sourceHeight, sourceWidth); - - ApplyResizeMode(image, resizeParameter, targetWidth, targetHeight); + ApplyResizeMode(image, resizeArgs); } - protected virtual void ApplyResizeMode(MagickImage image, ImageResizeArgs resizeParameter, int targetWidth, - int targetHeight) + protected virtual void ApplyResizeMode(MagickImage image, ImageResizeArgs resizeArgs) { - switch (resizeParameter.Mode) + switch (resizeArgs.Mode) { case ImageResizeMode.None: - ResizeModeNone(image, targetWidth, targetHeight); + ResizeModeNone(image, resizeArgs); break; case ImageResizeMode.Stretch: - ResizeStretch(image, targetWidth, targetHeight); + ResizeStretch(image, resizeArgs); break; case ImageResizeMode.Pad: - ResizePad(image, targetWidth, targetHeight); + ResizePad(image, resizeArgs); break; case ImageResizeMode.BoxPad: - ResizeBoxPad(image, targetWidth, targetHeight); + ResizeBoxPad(image, resizeArgs); break; case ImageResizeMode.Max: - ResizeMax(image, targetWidth, targetHeight); + ResizeMax(image, resizeArgs); break; case ImageResizeMode.Min: - ResizeMin(image, targetWidth, targetHeight); + ResizeMin(image, resizeArgs); break; case ImageResizeMode.Crop: - ResizeCrop(image, targetWidth, targetHeight); + ResizeCrop(image, resizeArgs); break; default: - throw new NotSupportedException("Resize mode " + resizeParameter.Mode + "is not supported!"); + throw new NotSupportedException("Resize mode " + resizeArgs.Mode + "is not supported!"); } } - protected virtual int GetTargetHeight(int targetHeight, int targetWidth, int min, int sourceHeight, int sourceWidth) + protected virtual int GetTargetHeight(ImageResizeArgs resizeArgs, int min, int sourceWidth, int sourceHeight) { - if (targetHeight == 0 && targetWidth > 0) + if (resizeArgs.Height == 0 && resizeArgs.Width > 0) { - targetHeight = Math.Max(min, (int)Math.Round(sourceHeight * targetWidth / (float)sourceWidth)); + return Math.Max(min, (int)Math.Round(sourceHeight * resizeArgs.Width / (float)sourceWidth)); } - return targetHeight; + return resizeArgs.Height; } - protected virtual int GetTargetWidth(int targetWidth, int targetHeight, int min, int sourceWidth, int sourceHeight) + protected virtual int GetTargetWidth(ImageResizeArgs resizeArgs, int min, int sourceWidth, int sourceHeight) { - if (targetWidth == 0 && targetHeight > 0) + if (resizeArgs.Width == 0 && resizeArgs.Height > 0) { - targetWidth = Math.Max(min, (int)Math.Round(sourceWidth * targetHeight / (float)sourceHeight)); + return Math.Max(min, (int)Math.Round(sourceWidth * resizeArgs.Height / (float)sourceHeight)); } - return targetWidth; + return resizeArgs.Width; } - protected virtual void ResizeCrop(MagickImage image, int targetWidth, int targetHeight) + protected virtual void ResizeModeNone(IMagickImage image, ImageResizeArgs resizeArgs) { - var defaultMagickGeometry = new MagickGeometry(targetWidth, targetHeight) { IgnoreAspectRatio = true }; - image.Crop(defaultMagickGeometry, Gravity.Center); + var sourceWidth = image.Width; + var sourceHeight = image.Height; + + image.Resize( + GetTargetWidth(resizeArgs, Min, sourceWidth, sourceHeight), + GetTargetHeight(resizeArgs, Min, sourceWidth, sourceHeight) + ); } - protected virtual void ResizeMin(MagickImage image, int targetWidth, int targetHeight) + protected virtual void ResizeStretch(IMagickImage image, ImageResizeArgs resizeArgs) { var sourceWidth = image.Width; var sourceHeight = image.Height; - var imageRatio = CalculateRatio(sourceWidth, sourceHeight); - - var percentWidth = CalculatePercent(sourceWidth, targetWidth); - - if (targetWidth > sourceWidth || targetHeight > sourceHeight) - { - targetWidth = sourceWidth; - targetHeight = sourceHeight; - } - else - { - var widthDiff = sourceWidth - targetWidth; - var heightDiff = sourceHeight - targetHeight; - - if (widthDiff > heightDiff) - { - targetWidth = (int)Math.Round(targetHeight / imageRatio); - } - else if (widthDiff < heightDiff) - { - targetHeight = (int)Math.Round(targetWidth * imageRatio); - } - else - { - if (targetHeight > targetWidth) - { - targetWidth = (int)Math.Round(sourceHeight * percentWidth); - } - else - { - targetHeight = (int)Math.Round(sourceHeight * percentWidth); - } - } - } - - image.Resize(targetWidth, targetHeight); + image.Resize( + new MagickGeometry( + GetTargetWidth(resizeArgs, Min, sourceWidth, sourceHeight), + GetTargetHeight(resizeArgs, Min, sourceWidth, sourceHeight)) { IgnoreAspectRatio = true }); } - protected virtual void ResizeMax(IMagickImage image, int targetWidth, int targetHeight) + protected virtual void ResizePad(MagickImage image, ImageResizeArgs resizeArgs) { var sourceWidth = image.Width; var sourceHeight = image.Height; - var imageRatio = CalculateRatio(sourceWidth, sourceHeight); - var ratio = CalculateRatio(targetWidth, targetHeight); + var targetWidth = GetTargetWidth(resizeArgs, Min, sourceWidth, sourceHeight); + var targetHeight = GetTargetHeight(resizeArgs, Min, sourceWidth, sourceHeight); var percentHeight = CalculatePercent(sourceHeight, targetHeight); var percentWidth = CalculatePercent(sourceWidth, targetWidth); - if (imageRatio < ratio) + var newWidth = targetWidth; + var newHeight = targetHeight; + + if (percentHeight < percentWidth) { - targetHeight = (int)(sourceHeight * percentWidth); + newWidth = (int)Math.Round(sourceWidth * percentHeight); } else { - targetWidth = (int)(sourceWidth * percentHeight); + newHeight = (int)Math.Round(sourceHeight * percentWidth); } - image.Resize(targetWidth, targetHeight); + image.Resize(newWidth, newHeight); + image.Extent(targetWidth, targetHeight, Gravity.Center, MagickColors.Transparent); } - protected virtual void ResizeBoxPad(MagickImage image, int targetWidth, int targetHeight) + protected virtual void ResizeBoxPad(MagickImage image, ImageResizeArgs resizeArgs) { var sourceWidth = image.Width; var sourceHeight = image.Height; + var targetWidth = GetTargetWidth(resizeArgs, Min, sourceWidth, sourceHeight); + var targetHeight = GetTargetHeight(resizeArgs, Min, sourceWidth, sourceHeight); + var percentHeight = CalculatePercent(sourceHeight, targetHeight); var percentWidth = CalculatePercent(sourceWidth, targetWidth); @@ -241,47 +219,107 @@ public class MagickImageResizerContributor : IImageResizerContributor, ITransien image.Extent(targetWidth, targetHeight, Gravity.Center, MagickColors.Transparent); } - protected virtual void ResizePad(MagickImage image, int targetWidth, int targetHeight) + protected virtual void ResizeMax(IMagickImage image, ImageResizeArgs resizeArgs) { var sourceWidth = image.Width; var sourceHeight = image.Height; + var imageRatio = CalculateRatio(sourceWidth, sourceHeight); + + var targetWidth = GetTargetWidth(resizeArgs, Min, sourceWidth, sourceHeight); + var targetHeight = GetTargetHeight(resizeArgs, Min, sourceWidth, sourceHeight); + + var ratio = CalculateRatio(targetWidth, targetHeight); + var percentHeight = CalculatePercent(sourceHeight, targetHeight); var percentWidth = CalculatePercent(sourceWidth, targetWidth); - var newWidth = targetWidth; - var newHeight = targetHeight; - - if (percentHeight < percentWidth) + if (imageRatio < ratio) { - newWidth = (int)Math.Round(sourceWidth * percentHeight); + targetHeight = (int)(sourceHeight * percentWidth); } else { - newHeight = (int)Math.Round(sourceHeight * percentWidth); + targetWidth = (int)(sourceWidth * percentHeight); } - image.Resize(newWidth, newHeight); - image.Extent(targetWidth, targetHeight, Gravity.Center, MagickColors.Transparent); + image.Resize(targetWidth, targetHeight); } - protected virtual float CalculatePercent(int imageHeightOrWidth, int heightOrWidth) + protected virtual void ResizeMin(MagickImage image, ImageResizeArgs resizeArgs) { - return heightOrWidth / (float)imageHeightOrWidth; + var sourceWidth = image.Width; + var sourceHeight = image.Height; + + var imageRatio = CalculateRatio(sourceWidth, sourceHeight); + + var targetWidth = GetTargetWidth(resizeArgs, Min, sourceWidth, sourceHeight); + var targetHeight = GetTargetHeight(resizeArgs, Min, sourceWidth, sourceHeight); + + var percentWidth = CalculatePercent(sourceWidth, targetWidth); + + if (targetWidth > sourceWidth || targetHeight > sourceHeight) + { + targetWidth = sourceWidth; + targetHeight = sourceHeight; + } + else + { + var widthDiff = sourceWidth - targetWidth; + var heightDiff = sourceHeight - targetHeight; + + if (widthDiff > heightDiff) + { + targetWidth = (int)Math.Round(targetHeight / imageRatio); + } + else if (widthDiff < heightDiff) + { + targetHeight = (int)Math.Round(targetWidth * imageRatio); + } + else + { + if (targetHeight > targetWidth) + { + targetWidth = (int)Math.Round(sourceHeight * percentWidth); + } + else + { + targetHeight = (int)Math.Round(sourceHeight * percentWidth); + } + } + } + + image.Resize(targetWidth, targetHeight); } - protected virtual float CalculateRatio(int width, int height) + protected virtual void ResizeCrop(MagickImage image, ImageResizeArgs resizeArgs) { - return height / (float)width; + var sourceWidth = image.Width; + var sourceHeight = image.Height; + + var targetWidth = GetTargetWidth(resizeArgs, Min, sourceWidth, sourceHeight); + var targetHeight = GetTargetHeight(resizeArgs, Min, sourceWidth, sourceHeight); + + image.Extent( + targetWidth, + targetHeight, + Gravity.Center, + MagickColors.Transparent); + + image.Crop( + new MagickGeometry( + targetWidth, + targetHeight) { IgnoreAspectRatio = true }, + Gravity.Center); } - protected virtual void ResizeStretch(IMagickImage image, int targetWidth, int targetHeight) + protected virtual float CalculatePercent(int imageHeightOrWidth, int heightOrWidth) { - image.Resize(new MagickGeometry(targetWidth, targetHeight) { IgnoreAspectRatio = true }); + return heightOrWidth / (float)imageHeightOrWidth; } - protected virtual void ResizeModeNone(IMagickImage image, int targetWidth, int targetHeight) + protected virtual float CalculateRatio(int width, int height) { - image.Resize(targetWidth, targetHeight); + return height / (float)width; } } \ No newline at end of file