From ba2da751028b39619e47f0e96263c3b8b9e6ce1d Mon Sep 17 00:00:00 2001 From: Will Norris Date: Wed, 27 Sep 2017 00:04:04 +0000 Subject: [PATCH] parse crop and resize params before transforms --- transform.go | 12 +++++++++--- transform_test.go | 7 +++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/transform.go b/transform.go index 0f666c5..4d4c5e6 100644 --- a/transform.go +++ b/transform.go @@ -251,12 +251,18 @@ func exifOrientation(r io.Reader) (opt Options) { // transformImage modifies the image m based on the transformations specified // in opt. func transformImage(m image.Image, opt Options) image.Image { + // Parse crop and resize parameters before applying any transforms. + // This is to ensure that any percentage-based values are based off the + // size of the original image. + rect := cropParams(m, opt) + w, h, resize := resizeParams(m, opt) + // crop if needed - if r := cropParams(m, opt); !m.Bounds().Eq(r) { - m = imaging.Crop(m, r) + if !m.Bounds().Eq(rect) { + m = imaging.Crop(m, rect) } // resize if needed - if w, h, ok := resizeParams(m, opt); ok { + if resize { if opt.Fit { m = imaging.Fit(m, w, h, resampleFilter) } else { diff --git a/transform_test.go b/transform_test.go index 0e99f85..7ca8311 100644 --- a/transform_test.go +++ b/transform_test.go @@ -351,6 +351,13 @@ func TestTransformImage(t *testing.T) { Options{CropHeight: 2, CropWidth: 2, CropX: 2, CropY: 2}, newImage(2, 2, yellow, yellow, yellow, yellow), }, + + // percentage-based resize in addition to rectangular crop + { + newImage(12, 12, red), + Options{Width: 0.5, Height: 0.5, CropWidth: 8, CropHeight: 8}, + newImage(6, 6, red), + }, } for _, tt := range tests {