11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
3+
34#nullable disable
45
56using System . Globalization ;
@@ -61,6 +62,26 @@ public class ResizeWebProcessor : IImageWebProcessor
6162 /// </summary>
6263 public const string Compand = "compand" ;
6364
65+ /// <summary>
66+ /// The command constant for the resize TargetRectangle X
67+ /// </summary>
68+ public const string TargetRectangleX = "targetrectanglex" ;
69+
70+ /// <summary>
71+ /// The command constant for the resize TargetRectangle X
72+ /// </summary>
73+ public const string TargetRectangleY = "targetrectangley" ;
74+
75+ /// <summary>
76+ /// The command constant for the resize TargetRectangle X
77+ /// </summary>
78+ public const string TargetRectangleWidth = "targetrectanglewidth" ;
79+
80+ /// <summary>
81+ /// The command constant for the resize TargetRectangle X
82+ /// </summary>
83+ public const string TargetRectangleHeight = "targetrectangleheight" ;
84+
6485 private static readonly IEnumerable < string > ResizeCommands
6586 = new [ ]
6687 {
@@ -126,6 +147,8 @@ internal static ResizeOptions GetResizeOptions(
126147 return null ;
127148 }
128149
150+ Rectangle ? targetRectangle = ParseRectangle ( commands , parser , culture ) ;
151+
129152 return new ( )
130153 {
131154 Size = size ,
@@ -134,7 +157,8 @@ internal static ResizeOptions GetResizeOptions(
134157 Mode = GetMode ( commands , parser , culture ) ,
135158 Compand = GetCompandMode ( commands , parser , culture ) ,
136159 Sampler = GetSampler ( commands ) ,
137- PadColor = parser . ParseValue < Color > ( commands . GetValueOrDefault ( Color ) , culture )
160+ PadColor = parser . ParseValue < Color > ( commands . GetValueOrDefault ( Color ) , culture ) ,
161+ TargetRectangle = targetRectangle
138162 } ;
139163 }
140164
@@ -158,6 +182,31 @@ private static Size ParseSize(
158182 return ExifOrientationUtilities . Transform ( new Size ( width , height ) , orientation ) ;
159183 }
160184
185+ private static Rectangle ? ParseRectangle (
186+ CommandCollection commands ,
187+ CommandParser parser ,
188+ CultureInfo culture )
189+ {
190+ if ( ! commands . Contains ( TargetRectangleWidth ) || ! commands . Contains ( TargetRectangleHeight ) )
191+ {
192+ return null ;
193+ }
194+
195+ // The command parser will reject negative numbers as it clamps values to ranges.
196+ int width = ( int ) parser . ParseValue < uint > ( commands . GetValueOrDefault ( TargetRectangleWidth ) , culture ) ;
197+ int height = ( int ) parser . ParseValue < uint > ( commands . GetValueOrDefault ( TargetRectangleHeight ) , culture ) ;
198+
199+ if ( ! commands . Contains ( TargetRectangleX ) || ! commands . Contains ( TargetRectangleY ) )
200+ {
201+ return new Rectangle ( 0 , 0 , width , height ) ;
202+ }
203+
204+ int x = ( int ) parser . ParseValue < uint > ( commands . GetValueOrDefault ( TargetRectangleX ) , culture ) ;
205+ int y = ( int ) parser . ParseValue < uint > ( commands . GetValueOrDefault ( TargetRectangleY ) , culture ) ;
206+
207+ return new Rectangle ( x , y , width , height ) ;
208+ }
209+
161210 private static PointF ? GetCenter (
162211 ushort orientation ,
163212 CommandCollection commands ,
0 commit comments