@@ -30,6 +30,7 @@ const (
3030 optCropWidth = "cw"
3131 optCropHeight = "ch"
3232 optSmartCrop = "sc"
33+ optTrim = "trim"
3334)
3435
3536// URLError reports a malformed URL error.
@@ -80,6 +81,9 @@ type Options struct {
8081
8182 // Automatically find good crop points based on image content.
8283 SmartCrop bool
84+
85+ // If true, automatically trim pixels of the same color around the edges
86+ Trim bool
8387}
8488
8589func (o Options ) String () string {
@@ -123,6 +127,9 @@ func (o Options) String() string {
123127 if o .SmartCrop {
124128 opts = append (opts , optSmartCrop )
125129 }
130+ if o .Trim {
131+ opts = append (opts , optTrim )
132+ }
126133 sort .Strings (opts )
127134 return strings .Join (opts , "," )
128135}
@@ -132,7 +139,7 @@ func (o Options) String() string {
132139// the presence of other fields (like Fit). A non-empty Format value is
133140// assumed to involve a transformation.
134141func (o Options ) transform () bool {
135- return o .Width != 0 || o .Height != 0 || o .Rotate != 0 || o .FlipHorizontal || o .FlipVertical || o .Quality != 0 || o .Format != "" || o .CropX != 0 || o .CropY != 0 || o .CropWidth != 0 || o .CropHeight != 0
142+ return o .Width != 0 || o .Height != 0 || o .Rotate != 0 || o .FlipHorizontal || o .FlipVertical || o .Quality != 0 || o .Format != "" || o .CropX != 0 || o .CropY != 0 || o .CropWidth != 0 || o .CropHeight != 0 || o . Trim
136143}
137144
138145// ParseOptions parses str as a list of comma separated transformation options.
@@ -207,7 +214,7 @@ func (o Options) transform() bool {
207214//
208215// # Format
209216//
210- // The "jpeg", "png", and "tiff" options can be used to specify the desired
217+ // The "jpeg", "png", and "tiff" options can be used to specify the desired
211218// image format of the proxied image.
212219//
213220// # Signature
@@ -219,6 +226,13 @@ func (o Options) transform() bool {
219226// See https://github.com/willnorris/imageproxy/blob/master/docs/url-signing.md
220227// for examples of generating signatures.
221228//
229+ // # Trim
230+ //
231+ // The "trim" option will automatically trim pixels of the same color around
232+ // the edges of the image. This is useful for removing borders from images
233+ // that have been resized or cropped. The trim option is applied before other
234+ // options such as cropping or resizing.
235+ //
222236// Examples
223237//
224238// 0x0 - no resizing
@@ -251,6 +265,8 @@ func ParseOptions(str string) Options {
251265 options .Format = opt
252266 case opt == optSmartCrop :
253267 options .SmartCrop = true
268+ case opt == optTrim :
269+ options .Trim = true
254270 case strings .HasPrefix (opt , optRotatePrefix ):
255271 value := strings .TrimPrefix (opt , optRotatePrefix )
256272 options .Rotate , _ = strconv .Atoi (value )
0 commit comments