Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade SixLabors.ImageSharp to v3 #2074

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions core/Piranha.ImageSharp/ImageSharpProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void GetSize(byte[] bytes, out int width, out int height)
/// <param name="height">The requested height</param>
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
{
Expand All @@ -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);
}
}
Expand All @@ -79,7 +80,7 @@ public void Crop(Stream source, Stream dest, int width, int height)
/// <param name="width">The requested width</param>
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));

Expand All @@ -89,6 +90,7 @@ public void Scale(Stream source, Stream dest, int width)
Mode = ResizeMode.Crop
}));

var format = GetImageFormat(source);
image.Save(dest, format);
}
}
Expand All @@ -104,7 +106,7 @@ public void Scale(Stream source, Stream dest, int width)
/// <param name="height">The requested height</param>
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;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -146,12 +149,27 @@ public void CropScale(Stream source, Stream dest, int width, int height)
/// <param name="dest">The destination stream</param>
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;
}
}

/// <summary>
/// Gets an image format from the provided stream.
/// </summary>
/// <param name="source">The image data stream</param>
/// <returns>The detected format of the image.</returns>
/// <exception cref="UnknownImageFormatException">Thrown if the image format cannot be detected from the provided stream.</exception>
private static IImageFormat GetImageFormat(Stream source)
{
// Reset the stream position to the beginning
source.Position = 0;
return Image.DetectFormat(source);
}
}
2 changes: 1 addition & 1 deletion core/Piranha.ImageSharp/Piranha.ImageSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="2.1.8" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.4" />
<ProjectReference Include="..\Piranha\Piranha.csproj" />
</ItemGroup>

Expand Down
44 changes: 29 additions & 15 deletions test/Piranha.Tests/ImageSharp/ProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down