diff --git a/image.go b/image.go index 85f0b6d..75889fc 100644 --- a/image.go +++ b/image.go @@ -14,6 +14,17 @@ func (i *Image) Resize(width, height int) ([]byte, error) { return i.Process(options) } +// Resize the image to fixed width and height with additional crop transformation +func (i *Image) ResizeAndCrop(width, height int) ([]byte, error) { + options := Options{ + Width: width, + Height: height, + Embed: true, + Crop: true, + } + return i.Process(options) +} + // Extract area from the by X/Y axis func (i *Image) Extract(top, left, width, height int) ([]byte, error) { options := Options{ @@ -25,7 +36,7 @@ func (i *Image) Extract(top, left, width, height int) ([]byte, error) { return i.Process(options) } -// Enlarge the image from the by X/Y axis +// Enlarge the image by width and height. Aspect ratio is maintained func (i *Image) Enlarge(width, height int) ([]byte, error) { options := Options{ Width: width, @@ -35,6 +46,17 @@ func (i *Image) Enlarge(width, height int) ([]byte, error) { return i.Process(options) } +// Enlarge the image by width and height with additional crop transformation +func (i *Image) EnlargeAndCrop(width, height int) ([]byte, error) { + options := Options{ + Width: width, + Height: height, + Enlarge: true, + Crop: true, + } + return i.Process(options) +} + // Crop the image to the exact size specified func (i *Image) Crop(width, height int, gravity Gravity) ([]byte, error) { options := Options{ diff --git a/image_test.go b/image_test.go index 29e3bd4..d097f21 100644 --- a/image_test.go +++ b/image_test.go @@ -20,13 +20,27 @@ func TestImageResize(t *testing.T) { Write("fixtures/test_resize_out.jpg", buf) } +func TestImageResizeAndCrop(t *testing.T) { + buf, err := initImage("test.jpg").ResizeAndCrop(300, 200) + if err != nil { + t.Errorf("Cannot process the image: %#v", err) + } + + err = assertSize(buf, 300, 200) + if err != nil { + t.Error(err) + } + + Write("fixtures/test_resize_crop_out.jpg", buf) +} + func TestImageExtract(t *testing.T) { - buf, err := initImage("test.jpg").Extract(100, 100, 300, 300) + buf, err := initImage("test.jpg").Extract(100, 100, 300, 200) if err != nil { t.Errorf("Cannot process the image: %s", err) } - err = assertSize(buf, 300, 300) + err = assertSize(buf, 300, 200) if err != nil { t.Error(err) } @@ -48,6 +62,20 @@ func TestImageEnlarge(t *testing.T) { Write("fixtures/test_enlarge_out.jpg", buf) } +func TestImageEnlargeAndCrop(t *testing.T) { + buf, err := initImage("test.png").EnlargeAndCrop(800, 480) + if err != nil { + t.Errorf("Cannot process the image: %#v", err) + } + + err = assertSize(buf, 800, 480) + if err != nil { + t.Error(err) + } + + Write("fixtures/test_enlarge_crop_out.jpg", buf) +} + func TestImageCrop(t *testing.T) { buf, err := initImage("test.jpg").Crop(800, 600, NORTH) if err != nil {