-
-
Notifications
You must be signed in to change notification settings - Fork 460
/
options.go
172 lines (148 loc) · 4.17 KB
/
options.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"strconv"
"strings"
"github.com/h2non/bimg"
)
// ImageOptions represent all the supported image transformation params as first level members
type ImageOptions struct {
IsDefinedField
Width int
Height int
AreaWidth int
AreaHeight int
Quality int
Compression int
Rotate int
Top int
Left int
Margin int
Factor int
DPI int
TextWidth int
Flip bool
Flop bool
Force bool
Embed bool
NoCrop bool
NoReplicate bool
NoRotation bool
NoProfile bool
StripMetadata bool
Opacity float32
Sigma float64
MinAmpl float64
Text string
Image string
Font string
Type string
AspectRatio string
Color []uint8
Background []uint8
Interlace bool
Speed int
Extend bimg.Extend
Gravity bimg.Gravity
Colorspace bimg.Interpretation
Operations PipelineOperations
}
// IsDefinedField holds boolean ImageOptions fields. If true it means the field was specified in the request. This
// metadata allows for sane usage of default (false) values.
type IsDefinedField struct {
Flip bool
Flop bool
Force bool
Embed bool
NoCrop bool
NoReplicate bool
NoRotation bool
NoProfile bool
StripMetadata bool
Interlace bool
Palette bool
}
// PipelineOperation represents the structure for an operation field.
type PipelineOperation struct {
Name string `json:"operation"`
IgnoreFailure bool `json:"ignore_failure"`
Params map[string]interface{} `json:"params"`
ImageOptions ImageOptions `json:"-"`
Operation Operation `json:"-"`
}
// PipelineOperations defines the expected interface for a list of operations.
type PipelineOperations []PipelineOperation
func transformByAspectRatio(params map[string]interface{}) (width, height int) {
width, _ = coerceTypeInt(params["width"])
height, _ = coerceTypeInt(params["height"])
aspectRatio, ok := params["aspectratio"].(map[string]int)
if !ok {
return
}
if width != 0 {
height = width / aspectRatio["width"] * aspectRatio["height"]
} else {
width = height / aspectRatio["height"] * aspectRatio["width"]
}
return
}
func parseAspectRatio(val string) map[string]int {
val = strings.TrimSpace(strings.ToLower(val))
slicedVal := strings.Split(val, ":")
if len(slicedVal) < 2 {
return nil
}
width, _ := strconv.Atoi(slicedVal[0])
height, _ := strconv.Atoi(slicedVal[1])
return map[string]int{
"width": width,
"height": height,
}
}
func shouldTransformByAspectRatio(height, width int) bool {
// override aspect ratio parameters if width and height is given or not given at all
if (width != 0 && height != 0) || (width == 0 && height == 0) {
return false
}
return true
}
// BimgOptions creates a new bimg compatible options struct mapping the fields properly
func BimgOptions(o ImageOptions) bimg.Options {
opts := bimg.Options{
Width: o.Width,
Height: o.Height,
Flip: o.Flip,
Flop: o.Flop,
Quality: o.Quality,
Compression: o.Compression,
NoAutoRotate: o.NoRotation,
NoProfile: o.NoProfile,
Force: o.Force,
Gravity: o.Gravity,
Embed: o.Embed,
Extend: o.Extend,
Interpretation: o.Colorspace,
StripMetadata: o.StripMetadata,
Type: ImageType(o.Type),
Rotate: bimg.Angle(o.Rotate),
Interlace: o.Interlace,
Palette: o.Palette,
Speed: o.Speed,
}
if len(o.Background) != 0 {
opts.Background = bimg.Color{R: o.Background[0], G: o.Background[1], B: o.Background[2]}
}
if shouldTransformByAspectRatio(opts.Height, opts.Width) && o.AspectRatio != "" {
params := make(map[string]interface{})
params["height"] = opts.Height
params["width"] = opts.Width
params["aspectratio"] = parseAspectRatio(o.AspectRatio)
opts.Width, opts.Height = transformByAspectRatio(params)
}
if o.Sigma > 0 || o.MinAmpl > 0 {
opts.GaussianBlur = bimg.GaussianBlur{
Sigma: o.Sigma,
MinAmpl: o.MinAmpl,
}
}
return opts
}