Skip to content

Commit abcd2cd

Browse files
authored
Merge pull request #103 from duergner/101-limit-min-width-to-original-image-width
#101 Limit min width to original image width
2 parents dec90b0 + 7e8eb04 commit abcd2cd

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

filters/cropbyfocalpoint.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ type cropByFocalPoint struct {
1717
minWidth int
1818
}
1919

20+
func Min(x, y int) int {
21+
if x < y {
22+
return x
23+
}
24+
return y
25+
}
26+
2027
// NewCropByFocalPoint creates a new filter of this type
2128
func NewCropByFocalPoint() filters.Spec {
2229
return &cropByFocalPoint{}
@@ -57,10 +64,10 @@ func (f *cropByFocalPoint) CreateOptions(imageContext *ImageFilterContext) (*bim
5764
if f.minWidth != -1 {
5865
minHeight := int(f.aspectRatio * float64(f.minWidth))
5966

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))
6471

6572
if x < minX {
6673
x = minX

filters/cropbyfocalpoint_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ func TestCropByFocalPoint_CreateOptions_MinWidth(t *testing.T) {
7070
assert.Equal(t, 250, options.AreaHeight)
7171
assert.Equal(t, 209, options.Top)
7272
assert.Equal(t, 0, options.Left)
73+
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+
options, _ = c.CreateOptions(buildParameters(fc, image))
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)
7391
}
7492

7593
func TestCropByFocalPoint_CreateOptions_MissingPathParam(t *testing.T) {

0 commit comments

Comments
 (0)