Skip to content

Commit

Permalink
Merge pull request #38 from baskarap/master
Browse files Browse the repository at this point in the history
Resize the image in case of cropping request does NOT pass one of w/h
  • Loading branch information
baskarap authored Oct 4, 2019
2 parents 1c09da8 + e923e94 commit 907236b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pkg/processor/native/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ type ProcessorOption func(*BildProcessor)

// Crop takes an input image, width, height and a CropPoint and returns the cropped image
func (bp *BildProcessor) Crop(img image.Image, width, height int, point processor.CropPoint) image.Image {
if width == 0 || height == 0 {
if width == 0 && height == 0 {
return img
}
return bp.Resize(img, width, height)
}

w, h := getResizeWidthAndHeightForCrop(width, height, img.Bounds().Dx(), img.Bounds().Dy())
img = transform.Resize(img, w, h, transform.Linear)
x0, y0 := getStartingPointForCrop(w, h, width, height, point)
Expand Down
41 changes: 37 additions & 4 deletions pkg/processor/native/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,45 @@ func (s *BildProcessorSuite) TestBildProcessor_ResizeWithSameWidthAndHeight() {
}

func (s *BildProcessorSuite) TestBildProcessor_Crop() {
out := s.processor.Crop(s.srcImage, 500, 500, processor.CropCenter)
cases := []struct {
w int
h int
expectedW int
expectedH int
}{
{
w: 500,
h: 500,
expectedW: 500,
expectedH: 500,
},
{
w: 500,
h: 0,
expectedW: 500,
expectedH: 375,
},
{
w: 0,
h: 500,
expectedW: 666,
expectedH: 500,
},
{
w: 0,
h: 0,
expectedW: 500,
expectedH: 375,
},
}
for _, c := range cases {
out := s.processor.Crop(s.srcImage, c.w, c.h, processor.CropCenter)

assert.NotNil(s.T(), out)
assert.NotNil(s.T(), out)

assert.Equal(s.T(), 500, out.Bounds().Dx())
assert.Equal(s.T(), 500, out.Bounds().Dy())
assert.Equal(s.T(), c.expectedW, out.Bounds().Dx())
assert.Equal(s.T(), c.expectedH, out.Bounds().Dy())
}
}

func (s *BildProcessorSuite) TestBildProcessor_Grayscale() {
Expand Down

0 comments on commit 907236b

Please sign in to comment.