From c066206bdc692c82047704daca33996f2715647f Mon Sep 17 00:00:00 2001 From: Alexey Trofimov Date: Mon, 8 Oct 2018 11:35:52 +0700 Subject: [PATCH] Resize with fit --- options.go | 1 + resizer.go | 8 ++++++-- resizer_test.go | 30 ++++++++++++++++++++++++++++++ testdata/test_fit.png | Bin 0 -> 3018 bytes 4 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 testdata/test_fit.png diff --git a/options.go b/options.go index 17a1cb4c..7755e47c 100644 --- a/options.go +++ b/options.go @@ -197,6 +197,7 @@ type Options struct { Quality int Compression int Zoom int + Fit bool // Fit image into given dimensions Crop bool SmartCrop bool // Deprecated, use: bimg.Options.Gravity = bimg.GravitySmart Enlarge bool diff --git a/resizer.go b/resizer.go index 98a95ae7..88eb96ed 100644 --- a/resizer.go +++ b/resizer.go @@ -170,7 +170,7 @@ func saveImage(image *C.VipsImage, o Options) ([]byte, error) { } func normalizeOperation(o *Options, inWidth, inHeight int) { - if !o.Force && !o.Crop && !o.Embed && !o.Enlarge && o.Rotate == 0 && (o.Width > 0 || o.Height > 0) { + if !o.Force && !o.Fit && !o.Crop && !o.Embed && !o.Enlarge && o.Rotate == 0 && (o.Width > 0 || o.Height > 0) { o.Force = true } } @@ -443,7 +443,11 @@ 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) + if o.Crop { + factor = math.Min(xfactor, yfactor) + } else { + factor = math.Max(xfactor, yfactor) + } // Fixed width, auto height case o.Width > 0: if o.Crop { diff --git a/resizer_test.go b/resizer_test.go index a4b54b08..3be04c8d 100644 --- a/resizer_test.go +++ b/resizer_test.go @@ -161,6 +161,36 @@ func TestResizeCustomSizes(t *testing.T) { } } +func TestResizeFit(t *testing.T) { + // see https://github.com/h2non/bimg/issues/257 + buf, _ := Read("testdata/test_fit.png") + forceOptions := []bool{true, false} + + for _, forceOpt := range forceOptions { + opts := Options{Width: 1000, Height: 20, Fit: true, Force: forceOpt} + newImg, err := Resize(buf, opts) + if err != nil { + t.Fatalf("Resize(imgData, %#v) error: %#v", opts, err) + } + + size, _ := Size(newImg) + if !forceOpt && size.Width != 115 { + t.Fatalf("Invalid width (Force=false): %d, expected 115", size.Width) + } + if forceOpt && size.Width != 1000 { + t.Fatalf("Invalid width (Force=true): %d, expected 1000", size.Width) + } + if size.Height != 20 { + t.Fatalf("Invalid width: %d, expected 20", size.Width) + } + + Write(fmt.Sprintf( + "testdata/test_fit_%v_out.png", + forceOpt, + ), newImg) + } +} + func TestResizePrecision(t *testing.T) { // see https://github.com/h2non/bimg/issues/99 img := image.NewGray16(image.Rect(0, 0, 1920, 1080)) diff --git a/testdata/test_fit.png b/testdata/test_fit.png new file mode 100644 index 0000000000000000000000000000000000000000..5265c4a7509c86a893d680aeb5ca6257e7d9e28c GIT binary patch literal 3018 zcmbVO3p7+~8DY$h?|T7!@-qV~+}VVIklZBVJ7BK}I{Qo^Yu zNrZH9>(JynbRjONxJB^boL0V{#EC1omp$o_wDa{zUO_P_kF*2?PMP>H$5E_ z9W^yIJr8%LFEW-RkGYmQ@_#W*O&=MwCGM+aYHC_UkR^dEd@AY zAr|C{*$_5bC_&I_Y7UOk5|9%P!6-Hq!WYre&ui+?C_a~tUO{2uSrP^m%6DHch5Xig z`E%BXb7)+&<06zpG=LBYAs9qO3k4z>5KTvq@dC(PwTwff#vpJw9qptth+4_=K{3Qq z2t~nCF&qLJkFup<2~<3pLa;#*@dR5Oo`Sc<6I675 z$PqU!Rca&vLkfYgSn4kp3nmifGvP9d!B8c}4z+*SzF-_$%HPz~etdSz<1q7xOJBkp>XRUqO+&;es&uf5BW1z!OV_AflQt1VbR4 zL==KXO~w*nhy`LPVi@s`^yRt-gW)3;^Y{W}L+0z|jPh_{Pzf|D6+^@l#@uDG01uH2 z21OjmgGomt5yJBMT)>V%19@B$4@2T1V#!nj5o5=r5io2LflOj^NOlzBXSv9ErkE3{ zs)LF1T;#kh(T)VM$!rWrfe08fg~Gw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