diff --git a/image_test.go b/image_test.go index 37bb4bd4..1a236927 100644 --- a/image_test.go +++ b/image_test.go @@ -129,17 +129,29 @@ func TestImageExtractZero(t *testing.T) { } func TestImageEnlarge(t *testing.T) { - buf, err := initImage("test.png").Enlarge(500, 375) - if err != nil { - t.Errorf("Cannot process the image: %#v", err) + // test.png is 400x300 + tests := []struct { + input []int + expected []int + }{ + {[]int{500, 375}, []int{500, 375}}, + {[]int{577, 1250}, []int{577, 433}}, + {[]int{412, 3460}, []int{412, 309}}, } - err = assertSize(buf, 500, 375) - if err != nil { - t.Error(err) - } + for c, test := range tests { + buf, err := initImage("test.png").Enlarge(test.input[0], test.input[1]) + if err != nil { + t.Errorf("Cannot process the image: %#v", err) + } - Write("testdata/test_enlarge_out.jpg", buf) + err = assertSize(buf, test.expected[0], test.expected[1]) + if err != nil { + t.Error(err) + } + + Write(fmt.Sprintf("testdata/test_enlarge_out_%d.jpg", c), buf) + } } func TestImageEnlargeAndCrop(t *testing.T) { diff --git a/resizer.go b/resizer.go index 4b78b477..c9ad3b2c 100644 --- a/resizer.go +++ b/resizer.go @@ -453,7 +453,15 @@ func imageCalculations(o *Options, inWidth, inHeight int) float64 { switch { // Fixed width and height case o.Width > 0 && o.Height > 0: - factor = math.Min(xfactor, yfactor) + // Use minimum factor for croping or downscaling + // use maximum factor for upscaling + // Maybe this should be: + // if o.Crop || (o.Width <= inWidth || o.Height <= inHeight) { + if o.Crop || (o.Width < inWidth || o.Height < inHeight) { + factor = math.Min(xfactor, yfactor) + } else { + factor = math.Max(xfactor, yfactor) + } // Fixed width, auto height case o.Width > 0: if o.Crop {