@@ -112,9 +112,9 @@ internal static ResizeOptions GetResizeOptions(
112112 return null ;
113113 }
114114
115- bool rotated = ShouldHandleExifRotation ( image , commands , parser , culture , out ushort orientation ) ;
115+ ushort orientation = GetExifOrientation ( image , commands , parser , culture ) ;
116116
117- Size size = ParseSize ( rotated , commands , parser , culture ) ;
117+ Size size = ParseSize ( orientation , commands , parser , culture ) ;
118118
119119 if ( size . Width <= 0 && size . Height <= 0 )
120120 {
@@ -137,15 +137,18 @@ internal static ResizeOptions GetResizeOptions(
137137 }
138138
139139 private static Size ParseSize (
140- bool rotated ,
140+ ushort orientation ,
141141 CommandCollection commands ,
142142 CommandParser parser ,
143143 CultureInfo culture )
144144 {
145145 // The command parser will reject negative numbers as it clamps values to ranges.
146146 int width = ( int ) parser . ParseValue < uint > ( commands . GetValueOrDefault ( Width ) , culture ) ;
147147 int height = ( int ) parser . ParseValue < uint > ( commands . GetValueOrDefault ( Height ) , culture ) ;
148- return rotated ? new Size ( height , width ) : new Size ( width , height ) ;
148+
149+ return IsExifOrientationRotated ( orientation )
150+ ? new Size ( height , width )
151+ : new Size ( width , height ) ;
149152 }
150153
151154 private static PointF ? GetCenter (
@@ -345,23 +348,32 @@ private static IResampler GetSampler(CommandCollection commands)
345348 return KnownResamplers . Bicubic ;
346349 }
347350
348- private static bool ShouldHandleExifRotation ( FormattedImage image , CommandCollection commands , CommandParser parser , CultureInfo culture , out ushort orientation )
351+ private static ushort GetExifOrientation ( FormattedImage image , CommandCollection commands , CommandParser parser , CultureInfo culture )
349352 {
350353 // Browsers now implement 'image-orientation: from-image' by default.
351354 // https://developer.mozilla.org/en-US/docs/web/css/image-orientation
352355 // This makes orientation handling confusing for users who expect images to be resized in accordance
353356 // to what they observe rather than pure (and correct) methods.
354357 //
355- // To accomodate this we parse the dimensions to use based upon decoded EXIF orientation values, switching
356- // the width/height parameters when images are rotated (not flipped).
358+ // To accomodate this we parse the dimensions to use based upon decoded EXIF orientation values.
357359 // We default to 'true' for EXIF orientation handling. By passing 'false' it can be turned off.
358360 if ( commands . Contains ( Orient ) && ! parser . ParseValue < bool > ( commands . GetValueOrDefault ( Orient ) , culture ) )
359361 {
360- orientation = ExifOrientationMode . Unknown ;
361- return false ;
362+ return ExifOrientationMode . Unknown ;
362363 }
363364
364- return image . IsExifRotated ( out orientation ) ;
365+ image . TryGetExifOrientation ( out ushort orientation ) ;
366+ return orientation ;
365367 }
368+
369+ private static bool IsExifOrientationRotated ( ushort orientation )
370+ => orientation switch
371+ {
372+ ExifOrientationMode . LeftTop
373+ or ExifOrientationMode . RightTop
374+ or ExifOrientationMode . RightBottom
375+ or ExifOrientationMode . LeftBottom => true ,
376+ _ => false ,
377+ } ;
366378 }
367379}
0 commit comments