Skip to content

Commit 790a2b1

Browse files
Auto-orient WEBP and preserve encoder options
1 parent 338c719 commit 790a2b1

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

samples/ImageSharp.Web.Sample/Pages/Index.cshtml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,23 @@
125125
<hr />
126126
<section>
127127
<h2>ICC Profiles</h2>
128+
<p>
129+
Demonstrates that ICC Profiles are converted to sRGB by default ensuring correct display of images with embedded profiles.
130+
</p>
128131
<div>
129132
<p>
130-
<code>issue_2723.jpg?width=2000</code>
133+
<code>issue_2723.jpg?width=1000</code>
131134
</p>
132135
<p>
133-
<img src="issue_2723.jpg" imagesharp-width="2000" />
136+
<img src="issue_2723.jpg" imagesharp-width="1000" />
134137
</p>
135138
</div>
136139
<div>
137140
<p>
138-
<code>issue_2723.jpg?width=2000&format=webp</code>
141+
<code>issue_2723.jpg?width=1000&format=webp</code>
139142
</p>
140143
<p>
141-
<img src="issue_2723.jpg" imagesharp-width="2000" imagesharp-format="Format.WebP" />
144+
<img src="issue_2723.jpg" imagesharp-width="1000" imagesharp-format="Format.WebP" />
142145
</p>
143146
</div>
144147
</section>
@@ -308,7 +311,20 @@
308311
</p>
309312
</div>
310313
</section>
311-
314+
<hr />
315+
<section>
316+
<h2>EXIF AutoOrientation</h2>
317+
<p>Demonstrates that by default EXIF orientation is normalized enabling correct display of WEBP images.</p>
318+
<div>
319+
<p>
320+
<code>Exif5-1x.webp?width=128</code>
321+
</p>
322+
<p>
323+
<img src="Exif5-1x.webp" imagesharp-width="128"/>
324+
</p>
325+
</div>
326+
</section>
327+
<hr />
312328
<section>
313329
<h2>EXIF Handling Portrait</h2>
314330
<p>Demonstrates that the middleware handles EXIF orientation correctly for portrait images.</p>
Lines changed: 3 additions & 0 deletions
Loading

src/ImageSharp.Web/Middleware/ImageSharpMiddlewareOptions.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using SixLabors.ImageSharp.Formats.Png;
1010
using SixLabors.ImageSharp.Formats.Webp;
1111
using SixLabors.ImageSharp.Web.Commands;
12+
using SixLabors.ImageSharp.Web.Processors;
1213
using SixLabors.ImageSharp.Web.Providers;
1314

1415
namespace SixLabors.ImageSharp.Web.Middleware;
@@ -31,7 +32,20 @@ public class ImageSharpMiddlewareOptions
3132
return HMACUtilities.ComputeHMACSHA256(uri, secret);
3233
};
3334

34-
private Func<ImageCommandContext, Task> onParseCommandsAsync = _ => Task.CompletedTask;
35+
private Func<ImageCommandContext, Task> onParseCommandsAsync = context =>
36+
{
37+
// WEBP EXIF orientation is ignored by browsers.
38+
// https://zpl.fi/exif-orientation-in-different-formats/
39+
// To ensure that orientation is handled correctly for web use we transparently add the auto-orient command if it is not already present.
40+
// See issues #304, #375, and #381 for more details.
41+
if (!context.Commands.Contains(AutoOrientWebProcessor.AutoOrient))
42+
{
43+
context.Commands.Insert(0, new KeyValuePair<string, string?>(AutoOrientWebProcessor.AutoOrient, bool.TrueString));
44+
}
45+
46+
return Task.CompletedTask;
47+
};
48+
3549
private Func<ImageCommandContext, Configuration, Task<DecoderOptions?>> onBeforeLoadAsync = (_, _) => Task.FromResult<DecoderOptions?>(null);
3650
private Func<FormattedImage, Task> onBeforeSaveAsync = _ => Task.CompletedTask;
3751
private Func<ImageProcessingContext, Task> onProcessedAsync = _ => Task.CompletedTask;

src/ImageSharp.Web/Processors/QualityWebProcessor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public FormattedImage Process(
5656
Quality = quality,
5757
Interleaved = reference.Interleaved,
5858
ColorType = reference.ColorType,
59-
SkipMetadata = reference.SkipMetadata
59+
Progressive = reference.Progressive,
60+
ProgressiveScans = reference.ProgressiveScans,
61+
SkipMetadata = reference.SkipMetadata,
62+
RestartInterval = reference.RestartInterval
6063
};
6164
}
6265
}

0 commit comments

Comments
 (0)