Skip to content

Commit 4c6821e

Browse files
Merge pull request #215 from SixLabors/js/auto-orient
Add AutoOrientWebProcessor
2 parents c99f07c + 316434c commit 4c6821e

4 files changed

Lines changed: 96 additions & 3 deletions

File tree

samples/ImageSharp.Web.Sample/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
using SixLabors.ImageSharp.Web.Caching;
1313
using SixLabors.ImageSharp.Web.Commands;
1414
using SixLabors.ImageSharp.Web.DependencyInjection;
15-
using SixLabors.ImageSharp.Web.Middleware;
1615
using SixLabors.ImageSharp.Web.Processors;
1716
using SixLabors.ImageSharp.Web.Providers;
1817

@@ -59,7 +58,8 @@ public void ConfigureServices(IServiceCollection services)
5958
.AddProcessor<ResizeWebProcessor>()
6059
.AddProcessor<FormatWebProcessor>()
6160
.AddProcessor<BackgroundColorWebProcessor>()
62-
.AddProcessor<QualityWebProcessor>();
61+
.AddProcessor<QualityWebProcessor>()
62+
.AddProcessor<AutoOrientWebProcessor>();
6363

6464
// Add the default service and options.
6565
//

src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ private static void AddDefaultServices(
7373
builder.AddProcessor<ResizeWebProcessor>()
7474
.AddProcessor<FormatWebProcessor>()
7575
.AddProcessor<BackgroundColorWebProcessor>()
76-
.AddProcessor<QualityWebProcessor>();
76+
.AddProcessor<QualityWebProcessor>()
77+
.AddProcessor<AutoOrientWebProcessor>();
7778

7879
builder.AddConverter<IntegralNumberConverter<sbyte>>();
7980
builder.AddConverter<IntegralNumberConverter<byte>>();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Collections.Generic;
5+
using System.Globalization;
6+
using Microsoft.Extensions.Logging;
7+
using SixLabors.ImageSharp.Processing;
8+
using SixLabors.ImageSharp.Web.Commands;
9+
10+
namespace SixLabors.ImageSharp.Web.Processors
11+
{
12+
/// <summary>
13+
/// Allows the auto-orientation to ensure that EXIF defined orientation is
14+
/// reflected in the final image.
15+
/// </summary>
16+
public class AutoOrientWebProcessor : IImageWebProcessor
17+
{
18+
/// <summary>
19+
/// The command for changing the orientation.
20+
/// </summary>
21+
public const string AutoOrient = "autoorient";
22+
23+
/// <summary>
24+
/// The reusable collection of commands.
25+
/// </summary>
26+
private static readonly IEnumerable<string> AutoOrientCommands
27+
= new[] { AutoOrient };
28+
29+
/// <inheritdoc/>
30+
public IEnumerable<string> Commands { get; } = AutoOrientCommands;
31+
32+
/// <inheritdoc/>
33+
public FormattedImage Process(
34+
FormattedImage image,
35+
ILogger logger,
36+
CommandCollection commands,
37+
CommandParser parser,
38+
CultureInfo culture)
39+
{
40+
if (parser.ParseValue<bool>(commands.GetValueOrDefault(AutoOrient), culture))
41+
{
42+
image.Image.Mutate(x => x.AutoOrient());
43+
}
44+
45+
return image;
46+
}
47+
}
48+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Globalization;
5+
using SixLabors.ImageSharp.Formats.Png;
6+
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
7+
using SixLabors.ImageSharp.PixelFormats;
8+
using SixLabors.ImageSharp.Web.Commands;
9+
using SixLabors.ImageSharp.Web.Commands.Converters;
10+
using SixLabors.ImageSharp.Web.Processors;
11+
using Xunit;
12+
13+
namespace SixLabors.ImageSharp.Web.Tests.Processors
14+
{
15+
public class AutoOrientWebProcessorTests
16+
{
17+
[Fact]
18+
public void AutoOrientWebProcessor_UpdatesOrientation()
19+
{
20+
CommandParser parser = new(new[] { new SimpleCommandConverter<bool>() });
21+
CultureInfo culture = CultureInfo.InvariantCulture;
22+
23+
CommandCollection commands = new()
24+
{
25+
{ new(AutoOrientWebProcessor.AutoOrient, bool.TrueString) }
26+
};
27+
28+
const ushort tl = 1;
29+
const ushort br = 3;
30+
using var image = new Image<Rgba32>(1, 1);
31+
image.Metadata.ExifProfile = new();
32+
image.Metadata.ExifProfile.SetValue(ExifTag.Orientation, br);
33+
34+
IExifValue<ushort> orientation = image.Metadata.ExifProfile.GetValue(ExifTag.Orientation);
35+
Assert.Equal(br, orientation.Value);
36+
37+
using var formatted = new FormattedImage(image, PngFormat.Instance);
38+
new AutoOrientWebProcessor().Process(formatted, null, commands, parser, culture);
39+
40+
orientation = image.Metadata.ExifProfile.GetValue(ExifTag.Orientation);
41+
Assert.Equal(tl, orientation.Value);
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)