diff --git a/resize.go b/resize.go index b7658d8..d2a74b6 100644 --- a/resize.go +++ b/resize.go @@ -242,7 +242,7 @@ func extractOrEmbedImage(image *C.VipsImage, o Options) (*C.VipsImage, error) { left, top := (o.Width-inWidth)/2, (o.Height-inHeight)/2 image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend, o.Background) break - case o.Top != 0 || o.Left != 0: + case o.Top > 0 || o.Left > 0 || o.AreaWidth > 0 || o.AreaHeight > 0: if o.AreaWidth == 0 { o.AreaHeight = o.Width } diff --git a/resize_test.go b/resize_test.go index 0e4bb1b..98c47f1 100644 --- a/resize_test.go +++ b/resize_test.go @@ -284,6 +284,40 @@ func TestSharpen(t *testing.T) { Write("fixtures/test_sharpen.jpg", newImg) } +func TestExtractWithDefaultAxis(t *testing.T) { + options := Options{AreaWidth: 200, AreaHeight: 200} + buf, _ := Read("fixtures/test.jpg") + + newImg, err := Resize(buf, options) + if err != nil { + t.Errorf("Resize(imgData, %#v) error: %#v", options, err) + } + + size, _ := Size(newImg) + if size.Height != options.AreaHeight || size.Width != options.AreaWidth { + t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height) + } + + Write("fixtures/test_extract_defaults.jpg", newImg) +} + +func TestExtractCustomAxis(t *testing.T) { + options := Options{Top: 100, Left: 100, AreaWidth: 200, AreaHeight: 200} + buf, _ := Read("fixtures/test.jpg") + + newImg, err := Resize(buf, options) + if err != nil { + t.Errorf("Resize(imgData, %#v) error: %#v", options, err) + } + + size, _ := Size(newImg) + if size.Height != options.AreaHeight || size.Width != options.AreaWidth { + t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height) + } + + Write("fixtures/test_extract_custom_axis.jpg", newImg) +} + func TestConvert(t *testing.T) { width, height := 300, 240 formats := [3]ImageType{PNG, WEBP, JPEG}