We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents dec90b0 + 7e8eb04 commit abcd2cdCopy full SHA for abcd2cd
filters/cropbyfocalpoint.go
@@ -17,6 +17,13 @@ type cropByFocalPoint struct {
17
minWidth int
18
}
19
20
+func Min(x, y int) int {
21
+ if x < y {
22
+ return x
23
+ }
24
+ return y
25
+}
26
+
27
// NewCropByFocalPoint creates a new filter of this type
28
func NewCropByFocalPoint() filters.Spec {
29
return &cropByFocalPoint{}
@@ -57,10 +64,10 @@ func (f *cropByFocalPoint) CreateOptions(imageContext *ImageFilterContext) (*bim
57
64
if f.minWidth != -1 {
58
65
minHeight := int(f.aspectRatio * float64(f.minWidth))
59
66
60
- minX := int(float64(f.minWidth) * f.targetX)
61
- maxX := imageSize.Width - int(float64(f.minWidth) * (1 - f.targetX))
62
- minY := int(float64(minHeight) * f.targetY)
63
- maxY := imageSize.Height - int(float64(minHeight) * (1 - f.targetY))
67
+ minX := int(float64(Min(f.minWidth, imageSize.Width)) * f.targetX)
68
+ maxX := imageSize.Width - int(float64(Min(f.minWidth, imageSize.Width)) * (1 - f.targetX))
69
+ minY := int(float64(Min(minHeight, imageSize.Height)) * f.targetY)
70
+ maxY := imageSize.Height - int(float64(Min(minHeight, imageSize.Height)) * (1 - f.targetY))
71
72
if x < minX {
73
x = minX
filters/cropbyfocalpoint_test.go
@@ -70,6 +70,24 @@ func TestCropByFocalPoint_CreateOptions_MinWidth(t *testing.T) {
assert.Equal(t, 250, options.AreaHeight)
assert.Equal(t, 209, options.Top)
assert.Equal(t, 0, options.Left)
74
+ c = cropByFocalPoint{targetX: 0.5, targetY: 0.5, aspectRatio: 0.5, minWidth: 1500.0}
75
76
+ options, _ = c.CreateOptions(buildParameters(fc, image))
77
78
+ assert.Equal(t, 1000, options.AreaWidth)
79
+ assert.Equal(t, 500, options.AreaHeight)
80
+ assert.Equal(t, 84, options.Top)
81
+ assert.Equal(t, 0, options.Left)
82
83
+ c = cropByFocalPoint{targetX: 0.5, targetY: 0.5, aspectRatio: 1.0, minWidth: 1500.0}
84
85
86
87
+ assert.Equal(t, 668, options.AreaWidth)
88
+ assert.Equal(t, 668, options.AreaHeight)
89
+ assert.Equal(t, 0, options.Top)
90
+ assert.Equal(t, 166, options.Left)
91
92
93
func TestCropByFocalPoint_CreateOptions_MissingPathParam(t *testing.T) {
0 commit comments