add smartcrop feature

fixes #55
This commit is contained in:
Will Norris 2017-09-27 00:54:15 +00:00
parent 20c0a50a31
commit afbd254cdc
4 changed files with 48 additions and 14 deletions

15
data.go
View file

@ -39,6 +39,7 @@ const (
optCropY = "cy"
optCropWidth = "cw"
optCropHeight = "ch"
optSmartCrop = "sc"
)
// URLError reports a malformed URL error.
@ -86,6 +87,9 @@ type Options struct {
CropY float64
CropWidth float64
CropHeight float64
// Automatically find good crop points based on image content.
SmartCrop bool
}
func (o Options) String() string {
@ -126,6 +130,9 @@ func (o Options) String() string {
if o.CropHeight != 0 {
opts = append(opts, fmt.Sprintf("%s%v", string(optCropHeight), o.CropHeight))
}
if o.SmartCrop {
opts = append(opts, optSmartCrop)
}
return strings.Join(opts, ",")
}
@ -159,6 +166,12 @@ func (o Options) transform() bool {
// crop width or height will be adjusted, preserving the specified cx and cy
// values. Rectangular crop is applied before any other transformations.
//
// Smart Crop
//
// The "sc" option will perform a content-aware smart crop to fit the
// requested image width and height dimensions (see Size and Cropping below).
// The smart crop option will override any requested rectangular crop.
//
// Size and Cropping
//
// The size option takes the general form "{width}x{height}", where width and
@ -246,6 +259,8 @@ func ParseOptions(str string) Options {
options.ScaleUp = true
case opt == optFormatJPEG, opt == optFormatPNG, opt == optFormatTIFF:
options.Format = opt
case opt == optSmartCrop:
options.SmartCrop = true
case strings.HasPrefix(opt, optRotatePrefix):
value := strings.TrimPrefix(opt, optRotatePrefix)
options.Rotate, _ = strconv.Atoi(value)