Skip to content

Commit 49539d3

Browse files
Merge branch 'main' into deanmarcussen-patch-1
2 parents fd57b5d + 02ea785 commit 49539d3

5 files changed

Lines changed: 101 additions & 4 deletions

File tree

samples/ImageSharp.Web.Sample/Startup.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class Startup
3535
/// <summary>
3636
/// This method gets called by the runtime. Use this method to add services to the container.
3737
/// </summary>
38-
/// <param name="services">The collection of service desscriptors.</param>
38+
/// <param name="services">The collection of service descriptors.</param>
3939
public void ConfigureServices(IServiceCollection services)
4040
{
4141
services.AddImageSharp()
@@ -57,7 +57,8 @@ public void ConfigureServices(IServiceCollection services)
5757
.AddProcessor<ResizeWebProcessor>()
5858
.AddProcessor<FormatWebProcessor>()
5959
.AddProcessor<BackgroundColorWebProcessor>()
60-
.AddProcessor<QualityWebProcessor>();
60+
.AddProcessor<QualityWebProcessor>()
61+
.AddProcessor<AutoOrientWebProcessor>();
6162

6263
// Add the default service and options.
6364
//

src/ImageSharp.Web/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ private static void AddDefaultServices(
7171
builder.AddProcessor<ResizeWebProcessor>()
7272
.AddProcessor<FormatWebProcessor>()
7373
.AddProcessor<BackgroundColorWebProcessor>()
74-
.AddProcessor<QualityWebProcessor>();
74+
.AddProcessor<QualityWebProcessor>()
75+
.AddProcessor<AutoOrientWebProcessor>();
7576

7677
builder.AddConverter<IntegralNumberConverter<sbyte>>();
7778
builder.AddConverter<IntegralNumberConverter<byte>>();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 according to the EXIF information.
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+
/// <inheritdoc/>
49+
public bool RequiresTrueColorPixelFormat(CommandCollection commands, CommandParser parser, CultureInfo culture) => false;
50+
}
51+
}

src/ImageSharp.Web/Processors/WebProcessingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static FormattedImage Process(
4848
/// <param name="processors">The collection of available processors.</param>
4949
/// <param name="commands">The parsed collection of processing commands.</param>
5050
/// <returns>
51-
/// The sorted proccessors that supports any of the specified commands.
51+
/// The sorted processors that supports any of the specified commands.
5252
/// </returns>
5353
public static IReadOnlyList<(int Index, IImageWebProcessor Processor)> OrderBySupportedCommands(this IEnumerable<IImageWebProcessor> processors, CommandCollection commands)
5454
{
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)