-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
329 lines (269 loc) · 6.91 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package main
import (
"bytes"
"crypto/md5"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"github.com/anthonynsimon/bild/imgio"
)
// parseOptions returns the transformation options extracted
// from the request
func parseOptions(r *http.Request, extension string, img *Image) *Options {
options := NewOptions()
values := r.URL.Query()
for key := range values {
v, err := strconv.Atoi(values.Get(key))
if err != nil {
options.SetString(key, values.Get(key))
} else {
options.SetInt(key, v)
}
}
// set standard options
options.SetString("extension", extension)
options.img = img
return options
}
// Options holds the transformations settings
// to be applied in the requested image
type Options struct {
values map[string]interface{}
img *Image
}
// NewOptions returns a new options with values
func NewOptions() *Options {
return &Options{
values: make(map[string]interface{}),
}
}
// SetInt inserts an int value given its key in options
func (o *Options) SetInt(key string, value int) {
o.values[key] = value
}
// SetString inserts a string value given its key in options
func (o *Options) SetString(key, value string) {
o.values[key] = value
}
// Int returns an int value.Int
//
// ok returns false if the key does not exist or
// if the value cannot be asserted into int type
func (o Options) Int(key string) (value int, ok bool) {
v, ok := o.values[key]
if !ok {
return 0, false
}
value, ok = v.(int)
return value, ok
}
// String returns a string value
//
// ok returns false if the key does not exist or
// if the value cannot be asserted into string type
func (o Options) String(key string) (value string, ok bool) {
v, ok := o.values[key]
if !ok {
return "", false
}
switch tmp := v.(type) {
case int:
value = string(tmp)
case string:
value = tmp
case bool:
value = fmt.Sprint(tmp)
default:
return "", false
}
return value, true
}
// Image returns the image under modification
func (o Options) Image() *Image {
return o.img
}
// Extension returns the desired extension
func (o Options) Extension() string {
v, _ := o.String("extension")
return v
}
// Hash returns a unique string MD5 encoded that represents the
// transformation options provided.
func (o Options) Hash() string {
b := new(bytes.Buffer)
s := make([]string, 0, len(o.values))
for key := range o.values {
s = append(s, key)
}
sort.Strings(s)
for _, key := range s {
value, _ := o.String(key)
b.WriteString(key + value)
}
return fmt.Sprintf("%x", md5.Sum(b.Bytes()))
}
// Quality returns the picture quality for JPEG images.
//
// If the provided value is an invalid number, less than 1,
// or greater 100, 80 is returned instead.
func (o Options) Quality() int {
number, ok := o.Int("quality")
if !ok || number < 1 || number > 100 {
return 80
}
return number
}
// NumColors returns the maximum number of colors used in GIF images.
//
// It ranges from 1 to 256.
func (o Options) NumColors() int {
number, ok := o.Int("numcolors")
if !ok || number < 1 || number > 256 {
return 256
}
return number
}
// Encoder returns the image encoder accordingly to the desired
// image extension
func (o Options) Encoder() imgio.Encoder {
switch o.Extension() {
case "jpg", "jpeg":
return imgio.JPEGEncoder(o.Quality())
case "png":
return imgio.PNGEncoder()
case "bmp":
return imgio.BMPEncoder()
case "gif":
return GIFEncoder(o.NumColors())
}
return nil
}
// Resize calculates the new values for resizing the image.
func (o Options) Resize() (width, height int, err error) {
width, hasWidth := o.Int("width")
height, hasHeight := o.Int("height")
originalWidth := o.Image().Width()
originalHeight := o.Image().Height()
if !hasWidth && !hasHeight {
return 0, 0, ErrOptionNotProvided
}
// calculate values
if hasWidth && !hasHeight {
height = (width * originalHeight) / originalWidth
} else if !hasWidth && hasHeight {
width = (height * originalWidth) / originalHeight
}
// check boundaries
if width < 0 || height < 0 {
return 0, 0, ErrInvalidDimensions
}
// TODO(salmi): put a max file width/height check here?
return width, height, nil
}
// Crop checks the values for cropping the image.
//
// It has to be applied to the original image, so the execution
// order of transformation functions matters in this case.
func (o Options) Crop() (width, height, x, y int, err error) {
s, ok := o.String("crop")
if !ok {
return 0, 0, 0, 0, ErrOptionNotProvided
}
values, e := parseCropString(s)
if e != nil {
return 0, 0, 0, 0, ErrInvalidOptionValues
}
width, hasWidth := values["w"]
height, hasHeight := values["h"]
x, hasX := values["x"]
y, hasY := values["y"]
if !hasWidth || !hasHeight || !hasX || !hasY {
return 0, 0, 0, 0, ErrInvalidOptionValues
}
originalWidth := o.Image().Width()
originalHeight := o.Image().Height()
// check boundaries
if x > originalWidth || x+width > originalWidth {
return 0, 0, 0, 0, ErrInvalidOptionValues
}
if y > originalHeight || y+height > originalHeight {
return 0, 0, 0, 0, ErrInvalidOptionValues
}
return width, height, x, y, nil
}
// SmartCrop gets the options to apply the smart crop algorithm
//
// It has to be applied to the original image, so the execution
// order of transformation functions matters in this case.
func (o Options) SmartCrop() (width, height int, err error) {
s, ok := o.String("smartcrop")
if !ok {
return 0, 0, ErrOptionNotProvided
}
values, e := parseCropString(s)
if e != nil {
return 0, 0, ErrInvalidOptionValues
}
width, hasWidth := values["w"]
height, hasHeight := values["h"]
if !hasWidth || !hasHeight {
return 0, 0, ErrInvalidOptionValues
}
originalWidth := o.Image().Width()
originalHeight := o.Image().Height()
// check boundaries
if width > originalWidth {
return 0, 0, ErrInvalidOptionValues
}
if height > originalHeight {
return 0, 0, ErrInvalidOptionValues
}
return width, height, nil
}
// FlipH tells whether or not to apply the horizontal
// flip transformation
func (o Options) FlipH() error {
_, ok := o.String("flipH")
if !ok {
return ErrOptionNotProvided
}
return nil
}
// FlipV tells whether or not to apply the vertical
// flip transformation
func (o Options) FlipV() error {
_, ok := o.String("flipV")
if !ok {
return ErrOptionNotProvided
}
return nil
}
// parseCropString parses the crop string and returns
// a map contain the values.
//
// The string looks like: `w:300|h:300|x:20|y:30` and
// it may contain less or more values depending on the
// context it is being applied
func parseCropString(s string) (values map[string]int, err error) {
values = make(map[string]int)
pairs := strings.Split(s, "|")
for _, pair := range pairs {
v := strings.Split(pair, ":")
if len(v) != 2 {
return nil, ErrInvalidOptionValues
}
key := v[0]
value, e := strconv.Atoi(v[1])
if e != nil {
return nil, ErrInvalidOptionValues
}
// check boundaries - fast fail
if value < 0 {
return nil, ErrInvalidOptionValues
}
values[key] = value
}
return values, nil
}