From 10ef9650b7c1540de8c9382ad8db0c84c934bf96 Mon Sep 17 00:00:00 2001 From: Nattapong Nunpan Date: Wed, 5 Jun 2024 21:15:51 +0700 Subject: [PATCH] Upgrade SixLabors.ImageSharp to v3 --- .../Piranha.ImageSharp/ImageSharpProcessor.cs | 26 +++++++++-- .../Piranha.ImageSharp.csproj | 2 +- .../ImageSharp/ProcessorTests.cs | 44 ++++++++++++------- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/core/Piranha.ImageSharp/ImageSharpProcessor.cs b/core/Piranha.ImageSharp/ImageSharpProcessor.cs index ad7ea407e..06a3f844d 100644 --- a/core/Piranha.ImageSharp/ImageSharpProcessor.cs +++ b/core/Piranha.ImageSharp/ImageSharpProcessor.cs @@ -55,7 +55,7 @@ public void GetSize(byte[] bytes, out int width, out int height) /// The requested height public void Crop(Stream source, Stream dest, int width, int height) { - using (var image = Image.Load(source, out IImageFormat format)) + using (var image = Image.Load(source)) { image.Mutate(x => x.Crop(new Rectangle { @@ -65,6 +65,7 @@ public void Crop(Stream source, Stream dest, int width, int height) Y = height < image.Height ? (image.Height - height) / 2 : 0 })); + var format = GetImageFormat(source); image.Save(dest, format); } } @@ -79,7 +80,7 @@ public void Crop(Stream source, Stream dest, int width, int height) /// The requested width public void Scale(Stream source, Stream dest, int width) { - using (var image = Image.Load(source, out IImageFormat format)) + using (var image = Image.Load(source)) { int height = (int)Math.Round(width * ((float)image.Height / image.Width)); @@ -89,6 +90,7 @@ public void Scale(Stream source, Stream dest, int width) Mode = ResizeMode.Crop })); + var format = GetImageFormat(source); image.Save(dest, format); } } @@ -104,7 +106,7 @@ public void Scale(Stream source, Stream dest, int width) /// The requested height public void CropScale(Stream source, Stream dest, int width, int height) { - using (var image = Image.Load(source, out IImageFormat format)) + using (var image = Image.Load(source)) { var oldRatio = (float)image.Height / image.Width; var newRatio = (float)height / width; @@ -135,6 +137,7 @@ public void CropScale(Stream source, Stream dest, int width, int height) Mode = ResizeMode.Crop })); + var format = GetImageFormat(source); image.Save(dest, format); } } @@ -146,12 +149,27 @@ public void CropScale(Stream source, Stream dest, int width, int height) /// The destination stream public void AutoOrient(Stream source, Stream dest) { - using (var image = Image.Load(source, out IImageFormat format)) + using (var image = Image.Load(source)) { image.Mutate(x => x.AutoOrient()); + + var format = GetImageFormat(source); image.Save(dest, format); dest.Position = 0; } } + + /// + /// Gets an image format from the provided stream. + /// + /// The image data stream + /// The detected format of the image. + /// Thrown if the image format cannot be detected from the provided stream. + private static IImageFormat GetImageFormat(Stream source) + { + // Reset the stream position to the beginning + source.Position = 0; + return Image.DetectFormat(source); + } } diff --git a/core/Piranha.ImageSharp/Piranha.ImageSharp.csproj b/core/Piranha.ImageSharp/Piranha.ImageSharp.csproj index aa5fe9648..a93d22327 100644 --- a/core/Piranha.ImageSharp/Piranha.ImageSharp.csproj +++ b/core/Piranha.ImageSharp/Piranha.ImageSharp.csproj @@ -10,7 +10,7 @@ - + diff --git a/test/Piranha.Tests/ImageSharp/ProcessorTests.cs b/test/Piranha.Tests/ImageSharp/ProcessorTests.cs index 7e49c57b7..4e5385da2 100644 --- a/test/Piranha.Tests/ImageSharp/ProcessorTests.cs +++ b/test/Piranha.Tests/ImageSharp/ProcessorTests.cs @@ -16,8 +16,10 @@ namespace Piranha.Tests.ImageSharp; public class ProcessorTests { [Fact] - public void GetSizeStream() { - using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) { + public void GetSizeStream() + { + using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) + { var processor = new ImageSharpProcessor(); processor.GetSize(file, out var width, out var height); @@ -28,10 +30,13 @@ public void GetSizeStream() { } [Fact] - public void GetSizeBytes() { - using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) { - using (var reader = new BinaryReader(file)) { - var bytes = reader.ReadBytes((int) file.Length); + public void GetSizeBytes() + { + using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) + { + using (var reader = new BinaryReader(file)) + { + var bytes = reader.ReadBytes((int)file.Length); var processor = new ImageSharpProcessor(); @@ -44,11 +49,14 @@ public void GetSizeBytes() { } [Fact] - public void Crop() { - using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) { + public void Crop() + { + using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) + { var processor = new ImageSharpProcessor(); - using (var outStream = new MemoryStream()) { + using (var outStream = new MemoryStream()) + { processor.Crop(file, outStream, 640, 480); outStream.Position = 0; @@ -62,11 +70,14 @@ public void Crop() { } [Fact] - public void Scale() { - using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) { + public void Scale() + { + using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) + { var processor = new ImageSharpProcessor(); - using (var outStream = new MemoryStream()) { + using (var outStream = new MemoryStream()) + { processor.Scale(file, outStream, 960); outStream.Position = 0; @@ -80,11 +91,14 @@ public void Scale() { } [Fact] - public void CropScale() { - using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) { + public void CropScale() + { + using (var file = File.OpenRead("../../../Assets/HLD_Screenshot_01_mech_1080.png")) + { var processor = new ImageSharpProcessor(); - using (var outStream = new MemoryStream()) { + using (var outStream = new MemoryStream()) + { processor.CropScale(file, outStream, 640, 480); outStream.Position = 0;