mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-16 02:45:54 -07:00
Adding a test, verifying both ways of enabling SmartCrop work
This commit is contained in:
parent
855de7ca55
commit
33bf9bdff6
4 changed files with 63 additions and 13 deletions
|
|
@ -456,20 +456,23 @@ func TestFluentInterface(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestImageSmartCrop(t *testing.T) {
|
func TestImageSmartCrop(t *testing.T) {
|
||||||
if VipsMajorVersion >= 8 && VipsMinorVersion > 4 {
|
|
||||||
i := initImage("northern_cardinal_bird.jpg")
|
|
||||||
buf, err := i.SmartCrop(300, 300)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Cannot process the image: %#v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = assertSize(buf, 300, 300)
|
if !(VipsMajorVersion >= 8 && VipsMinorVersion > 4) {
|
||||||
if err != nil {
|
t.Skipf("Skipping this test, libvips doesn't meet version requirement %s > 8.4", VipsVersion)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
Write("fixtures/test_smart_crop.jpg", buf)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i := initImage("northern_cardinal_bird.jpg")
|
||||||
|
buf, err := i.SmartCrop(300, 300)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Cannot process the image: %#v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = assertSize(buf, 300, 300)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
Write("fixtures/test_smart_crop.jpg", buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initImage(file string) *Image {
|
func initImage(file string) *Image {
|
||||||
|
|
|
||||||
|
|
@ -195,6 +195,7 @@ type Options struct {
|
||||||
Compression int
|
Compression int
|
||||||
Zoom int
|
Zoom int
|
||||||
Crop bool
|
Crop bool
|
||||||
|
SmartCrop bool // Deprecated
|
||||||
Enlarge bool
|
Enlarge bool
|
||||||
Embed bool
|
Embed bool
|
||||||
Flip bool
|
Flip bool
|
||||||
|
|
|
||||||
|
|
@ -249,7 +249,7 @@ func extractOrEmbedImage(image *C.VipsImage, o Options) (*C.VipsImage, error) {
|
||||||
inHeight := int(image.Ysize)
|
inHeight := int(image.Ysize)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case o.Gravity == GravitySmart:
|
case o.Gravity == GravitySmart, o.SmartCrop:
|
||||||
image, err = vipsSmartCrop(image, o.Width, o.Height)
|
image, err = vipsSmartCrop(image, o.Width, o.Height)
|
||||||
break
|
break
|
||||||
case o.Crop:
|
case o.Crop:
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package bimg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/md5"
|
||||||
"image"
|
"image"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
@ -392,6 +393,51 @@ func TestResizePngWithTransparency(t *testing.T) {
|
||||||
Write("fixtures/transparent_out.png", newImg)
|
Write("fixtures/transparent_out.png", newImg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIfBothSmartCropOptionsAreIdentical(t *testing.T) {
|
||||||
|
if !(VipsMajorVersion >= 8 && VipsMinorVersion > 4) {
|
||||||
|
t.Skipf("Skipping this test, libvips doesn't meet version requirement %s > 8.4", VipsVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
benchmarkOptions := Options{Width: 100, Height: 100, Crop: true}
|
||||||
|
smartCropOptions := Options{Width: 100, Height: 100, Crop: true, SmartCrop: true}
|
||||||
|
gravityOptions := Options{Width: 100, Height: 100, Crop: true, Gravity: GravitySmart}
|
||||||
|
|
||||||
|
testImg, err := os.Open("fixtures/northern_cardinal_bird.jpg")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer testImg.Close()
|
||||||
|
|
||||||
|
testImgByte, err := ioutil.ReadAll(testImg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
scImg, err := Resize(testImgByte, smartCropOptions)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
gImg, err := Resize(testImgByte, gravityOptions)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
benchmarkImg, err := Resize(testImgByte, benchmarkOptions)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sch, gh, bh := md5.Sum(scImg), md5.Sum(gImg), md5.Sum(benchmarkImg)
|
||||||
|
if gh == bh || sch == bh {
|
||||||
|
t.Error("Expected both options produce a different result from a standard crop.")
|
||||||
|
}
|
||||||
|
|
||||||
|
if sch != gh {
|
||||||
|
t.Errorf("Expected both options to result in the same output, %x != %x", sch, gh)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func runBenchmarkResize(file string, o Options, b *testing.B) {
|
func runBenchmarkResize(file string, o Options, b *testing.B) {
|
||||||
buf, _ := Read(path.Join("fixtures", file))
|
buf, _ := Read(path.Join("fixtures", file))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue