mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-07 21:48:13 -08:00
Merge pull request #281 from pohang/skip_smartcrop
Add min dimension logic to smartcrop
This commit is contained in:
commit
b653789679
3 changed files with 81 additions and 1 deletions
12
resizer.go
12
resizer.go
|
|
@ -263,9 +263,19 @@ func extractOrEmbedImage(image *C.VipsImage, o Options) (*C.VipsImage, error) {
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case o.Gravity == GravitySmart, o.SmartCrop:
|
case o.Gravity == GravitySmart, o.SmartCrop:
|
||||||
image, err = vipsSmartCrop(image, o.Width, o.Height)
|
// it's already at an appropriate size, return immediately
|
||||||
|
if inWidth <= o.Width && inHeight <= o.Height {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
width := int(math.Min(float64(inWidth), float64(o.Width)))
|
||||||
|
height := int(math.Min(float64(inHeight), float64(o.Height)))
|
||||||
|
image, err = vipsSmartCrop(image, width, height)
|
||||||
break
|
break
|
||||||
case o.Crop:
|
case o.Crop:
|
||||||
|
// it's already at an appropriate size, return immediately
|
||||||
|
if inWidth <= o.Width && inHeight <= o.Height {
|
||||||
|
break
|
||||||
|
}
|
||||||
width := int(math.Min(float64(inWidth), float64(o.Width)))
|
width := int(math.Min(float64(inWidth), float64(o.Width)))
|
||||||
height := int(math.Min(float64(inHeight), float64(o.Height)))
|
height := int(math.Min(float64(inHeight), float64(o.Height)))
|
||||||
left, top := calculateCrop(inWidth, inHeight, o.Width, o.Height, o.Gravity)
|
left, top := calculateCrop(inWidth, inHeight, o.Width, o.Height, o.Gravity)
|
||||||
|
|
|
||||||
|
|
@ -590,6 +590,76 @@ func TestIfBothSmartCropOptionsAreIdentical(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSkipCropIfTooSmall(t *testing.T) {
|
||||||
|
testCases := [] struct {
|
||||||
|
name string
|
||||||
|
options Options
|
||||||
|
} {
|
||||||
|
{
|
||||||
|
name: "smart crop",
|
||||||
|
options: Options{
|
||||||
|
Width: 140,
|
||||||
|
Height: 140,
|
||||||
|
Crop: true,
|
||||||
|
Gravity: GravitySmart,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "centre crop",
|
||||||
|
options: Options{
|
||||||
|
Width: 140,
|
||||||
|
Height: 140,
|
||||||
|
Crop: true,
|
||||||
|
Gravity: GravityCentre,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "embed",
|
||||||
|
options: Options{
|
||||||
|
Width: 140,
|
||||||
|
Height: 140,
|
||||||
|
Embed: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "extract",
|
||||||
|
options: Options{
|
||||||
|
Top: 0,
|
||||||
|
Left: 0,
|
||||||
|
AreaWidth: 140,
|
||||||
|
AreaHeight: 140,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
testImg, err := os.Open("testdata/test_bad_extract_area.jpg")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer testImg.Close()
|
||||||
|
|
||||||
|
testImgByte, err := ioutil.ReadAll(testImg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
|
||||||
|
croppedImage, err := Resize(testImgByte, tc.options)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
size, _ := Size(croppedImage)
|
||||||
|
if tc.options.Height-size.Height > 1 || tc.options.Width-size.Width > 1 {
|
||||||
|
t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
|
||||||
|
}
|
||||||
|
t.Logf("size for %s is %dx%d", tc.name, size.Width, size.Height)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func runBenchmarkResize(file string, o Options, b *testing.B) {
|
func runBenchmarkResize(file string, o Options, b *testing.B) {
|
||||||
buf, _ := Read(path.Join("testdata", file))
|
buf, _ := Read(path.Join("testdata", file))
|
||||||
|
|
||||||
|
|
|
||||||
BIN
testdata/test_bad_extract_area.jpg
vendored
Normal file
BIN
testdata/test_bad_extract_area.jpg
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 312 KiB |
Loading…
Add table
Add a link
Reference in a new issue