diff --git a/image.go b/image.go index 60b1717..269bda9 100644 --- a/image.go +++ b/image.go @@ -9,6 +9,7 @@ func (i *Image) Resize(width, height int) ([]byte, error) { options := Options{ Width: width, Height: height, + Embed: true, } return i.Process(options) } diff --git a/image_test.go b/image_test.go index 0d9b3be..7095ba1 100644 --- a/image_test.go +++ b/image_test.go @@ -1,6 +1,7 @@ package bimg import ( + "fmt" "path" "testing" ) @@ -11,8 +12,9 @@ func TestImageResize(t *testing.T) { t.Errorf("Cannot process the image: %#v", err) } - if assertSize(buf, 300, 240) { - t.Error("Invalid image size") + err = assertSize(buf, 300, 240) + if err != nil { + t.Error(err) } Write("fixtures/test_resize_out.jpg", buf) @@ -24,21 +26,23 @@ func TestImageExtract(t *testing.T) { t.Errorf("Cannot process the image: %#v", err) } - if assertSize(buf, 300, 300) { - t.Error("Invalid image size") + err = assertSize(buf, 300, 300) + if err != nil { + t.Error(err) } Write("fixtures/test_extract_out.jpg", buf) } func TestImageEnlarge(t *testing.T) { - buf, err := initImage("test.png").Enlarge(500, 380) + buf, err := initImage("test.png").Enlarge(500, 375) if err != nil { t.Errorf("Cannot process the image: %#v", err) } - if assertSize(buf, 500, 380) { - t.Error("Invalid image size") + err = assertSize(buf, 500, 375) + if err != nil { + t.Error(err) } Write("fixtures/test_enlarge_out.jpg", buf) @@ -50,8 +54,9 @@ func TestImageCrop(t *testing.T) { t.Errorf("Cannot process the image: %#v", err) } - if assertSize(buf, 800, 600) { - t.Error("Invalid image size") + err = assertSize(buf, 800, 600) + if err != nil { + t.Error(err) } Write("fixtures/test_crop_out.jpg", buf) @@ -63,8 +68,9 @@ func TestImageCropByWidth(t *testing.T) { t.Errorf("Cannot process the image: %#v", err) } - if assertSize(buf, 600, 375) { - t.Error("Invalid image size") + err = assertSize(buf, 600, 375) + if err != nil { + t.Error(err) } Write("fixtures/test_crop_width_out.jpg", buf) @@ -76,8 +82,9 @@ func TestImageCropByHeight(t *testing.T) { t.Errorf("Cannot process the image: %#v", err) } - if assertSize(buf, 800, 480) { - t.Error("Invalid image size") + err = assertSize(buf, 480, 300) + if err != nil { + t.Error(err) } Write("fixtures/test_crop_height_out.jpg", buf) @@ -89,8 +96,9 @@ func TestImageThumbnail(t *testing.T) { t.Errorf("Cannot process the image: %#v", err) } - if assertSize(buf, 100, 100) { - t.Error("Invalid image size") + err = assertSize(buf, 100, 100) + if err != nil { + t.Error(err) } Write("fixtures/test_thumbnail_out.jpg", buf) @@ -141,13 +149,13 @@ func initImage(file string) *Image { return NewImage(buf) } -func assertSize(buf []byte, width, height int) bool { +func assertSize(buf []byte, width, height int) error { size, err := NewImage(buf).Size() if err != nil { - return false + return err } - if size.Width != 220 || size.Height != 300 { - return false + if size.Width != width || size.Height != height { + return fmt.Errorf("Invalid image size: %dx%d", size.Width, size.Height) } - return true + return nil } diff --git a/options.go b/options.go index d93cfaa..cc8b32c 100644 --- a/options.go +++ b/options.go @@ -62,12 +62,12 @@ type Options struct { AreaWidth int Top int Left int - Crop bool - Enlarge bool Extend int - Embed bool Quality int Compression int + Crop bool + Enlarge bool + Embed bool Type ImageType Rotate Angle Flip Direction diff --git a/resize.go b/resize.go index dcfa723..77cfab9 100644 --- a/resize.go +++ b/resize.go @@ -82,7 +82,10 @@ func Resize(buf []byte, o Options) ([]byte, error) { } // Transform image if necessary - if o.Width != inWidth || o.Height != inHeight { + debug(">>>>> Resolution %dx%d -> %dx%d", o.Width, o.Height, inWidth, inHeight) + + shouldExtract := o.Width != inWidth || o.Height != inHeight || o.AreaWidth > 0 || o.AreaHeight > 0 + if shouldExtract { // Use vips_shrink with the integral reduction if shrink > 1 { image, residual, err = shrinkImage(image, o, residual, shrink)