/
longeredgeresize.go
91 lines (66 loc) · 1.83 KB
/
longeredgeresize.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
88
89
90
91
package filters
import (
log "github.com/sirupsen/logrus"
"github.com/zalando-stups/skrop/parse"
"github.com/zalando/skipper/filters"
"github.com/h2non/bimg"
)
// LongerEdgeResizeName is the name of the filter
const LongerEdgeResizeName = "longerEdgeResize"
type longerEdgeResize struct {
size int
}
// NewLongerEdgeResize creates a new filter of this type
func NewLongerEdgeResize() filters.Spec {
return &longerEdgeResize{}
}
func (f *longerEdgeResize) Name() string {
return LongerEdgeResizeName
}
func (f *longerEdgeResize) CreateOptions(imageContext *ImageFilterContext) (*bimg.Options, error) {
log.Debug("Create options for longer edge resize ", f)
imageSize, err := imageContext.Image.Size()
if err != nil {
return nil, err
}
if imageSize.Width > imageSize.Height {
return &bimg.Options{
Width: f.size}, nil
}
return &bimg.Options{
Height: f.size}, nil
}
func (f *longerEdgeResize) CanBeMerged(other *bimg.Options, self *bimg.Options) bool {
if self.Width != 0 {
return other.Width == 0 || other.Width == self.Width
}
//if Height was set
return other.Height == 0 || other.Height == self.Height
}
func (f *longerEdgeResize) Merge(other *bimg.Options, self *bimg.Options) *bimg.Options {
//if Width was set
if self.Width != 0 {
other.Width = self.Width
}
//if Height was set
if self.Height != 0 {
other.Height = self.Height
}
return other
}
func (f *longerEdgeResize) CreateFilter(args []interface{}) (filters.Filter, error) {
var err error
if len(args) != 1 {
return nil, filters.ErrInvalidFilterParameters
}
c := &longerEdgeResize{}
c.size, err = parse.EskipIntArg(args[0])
if err != nil {
return nil, err
}
return c, nil
}
func (f *longerEdgeResize) Request(ctx filters.FilterContext) {}
func (f *longerEdgeResize) Response(ctx filters.FilterContext) {
HandleImageResponse(ctx, f)
}