/
resizebyheight.go
87 lines (66 loc) · 1.79 KB
/
resizebyheight.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package filters
import (
log "github.com/sirupsen/logrus"
"github.com/zalando-stups/skrop/parse"
"github.com/zalando/skipper/filters"
"github.com/h2non/bimg"
)
// ResizeByHeightName is the name of the filter
const ResizeByHeightName = "height"
type resizeByHeight struct {
height int
enlarge bool
}
// NewResizeByHeight creates a new filter of this type
func NewResizeByHeight() filters.Spec {
return &resizeByHeight{}
}
func (f *resizeByHeight) Name() string {
return ResizeByHeightName
}
func (f *resizeByHeight) CreateOptions(imageContext *ImageFilterContext) (*bimg.Options, error) {
log.Debug("Create options for resize by width ", f)
if !f.enlarge {
size, err := imageContext.Image.Size()
if err != nil {
return nil, err
}
// enlargement not allowed here
if size.Height <= f.height {
return &bimg.Options{}, nil
}
}
return &bimg.Options{
Height: f.height}, nil
}
func (f *resizeByHeight) CanBeMerged(other *bimg.Options, self *bimg.Options) bool {
return other.Height == 0 || other.Height == self.Height
}
func (f *resizeByHeight) Merge(other *bimg.Options, self *bimg.Options) *bimg.Options {
other.Height = self.Height
return other
}
func (f *resizeByHeight) CreateFilter(args []interface{}) (filters.Filter, error) {
var err error
if len(args) != 1 && len(args) != 2 {
return nil, filters.ErrInvalidFilterParameters
}
c := &resizeByHeight{}
c.height, err = parse.EskipIntArg(args[0])
if err != nil {
return nil, err
}
c.enlarge = true
if len(args) == 2 {
cons, err := parse.EskipStringArg(args[1])
if err != nil {
return nil, err
}
c.enlarge = !(cons == doNotEnlarge)
}
return c, nil
}
func (f *resizeByHeight) Request(ctx filters.FilterContext) {}
func (f *resizeByHeight) Response(ctx filters.FilterContext) {
HandleImageResponse(ctx, f)
}