mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-16 10:55:54 -07:00
fix(#106): allow custom area extraction without x/y axis
This commit is contained in:
parent
522cf71c73
commit
3c158381b0
2 changed files with 35 additions and 1 deletions
|
|
@ -242,7 +242,7 @@ func extractOrEmbedImage(image *C.VipsImage, o Options) (*C.VipsImage, error) {
|
||||||
left, top := (o.Width-inWidth)/2, (o.Height-inHeight)/2
|
left, top := (o.Width-inWidth)/2, (o.Height-inHeight)/2
|
||||||
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend, o.Background)
|
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend, o.Background)
|
||||||
break
|
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 {
|
if o.AreaWidth == 0 {
|
||||||
o.AreaHeight = o.Width
|
o.AreaHeight = o.Width
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -284,6 +284,40 @@ func TestSharpen(t *testing.T) {
|
||||||
Write("fixtures/test_sharpen.jpg", newImg)
|
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) {
|
func TestConvert(t *testing.T) {
|
||||||
width, height := 300, 240
|
width, height := 300, 240
|
||||||
formats := [3]ImageType{PNG, WEBP, JPEG}
|
formats := [3]ImageType{PNG, WEBP, JPEG}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue