diff --git a/go.mod b/go.mod index 4e1cd68..8acefde 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,7 @@ require ( github.com/jtolds/gls v4.2.1+incompatible // indirect github.com/kr/pretty v0.1.0 // indirect github.com/marstr/guid v0.0.0-20170427235115-8bdf7d1a087c // indirect - github.com/muesli/smartcrop v0.0.0-20171215203440-9032446b30f6 + github.com/muesli/smartcrop v0.2.1-0.20181030220600-548bbf0c0965 github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 // indirect github.com/peterbourgon/diskv v0.0.0-20171120014656-2973218375c3 github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index 2ec9b6a..10221d3 100644 --- a/go.sum +++ b/go.sum @@ -49,6 +49,10 @@ github.com/marstr/guid v0.0.0-20170427235115-8bdf7d1a087c h1:N7uWGS2fTwH/4BwxbHi github.com/marstr/guid v0.0.0-20170427235115-8bdf7d1a087c/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho= github.com/muesli/smartcrop v0.0.0-20171215203440-9032446b30f6 h1:m5kCp2hHRMu7z95f+aIK80l8N1tx+UXAqNoYpRS9v3o= github.com/muesli/smartcrop v0.0.0-20171215203440-9032446b30f6/go.mod h1:i2fCI/UorTfgEpPPLWiFBv4pye+YAG78RwcQLUkocpI= +github.com/muesli/smartcrop v0.2.0 h1:HV/xtsW5sc5xuZY39sxJXtTBwoF8RUJzQNhwwZhyGjE= +github.com/muesli/smartcrop v0.2.0/go.mod h1:i2fCI/UorTfgEpPPLWiFBv4pye+YAG78RwcQLUkocpI= +github.com/muesli/smartcrop v0.2.1-0.20181030220600-548bbf0c0965 h1:BdOnvj+P+06ZFwYd07iFWXHPfRyrJd5sAXpX9+E8bxM= +github.com/muesli/smartcrop v0.2.1-0.20181030220600-548bbf0c0965/go.mod h1:i2fCI/UorTfgEpPPLWiFBv4pye+YAG78RwcQLUkocpI= github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 h1:BvoENQQU+fZ9uukda/RzCAL/191HHwJA5b13R6diVlY= github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= github.com/peterbourgon/diskv v0.0.0-20171120014656-2973218375c3 h1:ZKRE3mqKoxObHs5oWjLnA1WxXhmlDDAVuE0VsuLIoNk= diff --git a/transform.go b/transform.go index 402cbe9..e81488f 100644 --- a/transform.go +++ b/transform.go @@ -27,6 +27,7 @@ import ( "github.com/disintegration/imaging" "github.com/muesli/smartcrop" + "github.com/muesli/smartcrop/nfnt" "github.com/rwcarlsen/goexif/exif" "golang.org/x/image/tiff" // register tiff format _ "golang.org/x/image/webp" // register webp format @@ -156,6 +157,8 @@ func resizeParams(m image.Image, opt Options) (w, h int, resize bool) { return w, h, true } +var smartcropAnalyzer = smartcrop.NewAnalyzer(nfnt.NewDefaultResizer()) + // cropParams calculates crop rectangle parameters to keep it in image bounds func cropParams(m image.Image, opt Options) image.Rectangle { if !opt.SmartCrop && opt.CropX == 0 && opt.CropY == 0 && opt.CropWidth == 0 && opt.CropHeight == 0 { @@ -169,12 +172,10 @@ func cropParams(m image.Image, opt Options) image.Rectangle { if opt.SmartCrop { w := evaluateFloat(opt.Width, imgW) h := evaluateFloat(opt.Height, imgH) - log.Printf("smartcrop input: %dx%d", w, h) - r, err := smartcrop.SmartCrop(m, w, h) + r, err := smartcropAnalyzer.FindBestCrop(m, w, h) if err != nil { - log.Printf("error with smartcrop: %v", err) + log.Printf("smartcrop error finding best crop: %v", err) } else { - log.Printf("smartcrop rectangle: %v", r) return r } }