-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
61 lines (53 loc) · 1.31 KB
/
engine.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
package engine
import (
"image"
"reflect"
"strconv"
)
type ImageOptions struct {
Width int `cache:"wi"`
Height int `cache:"he"`
Fit bool `cache:"ft"`
Fill bool `cache:"fl"`
Blur float64 `cache:"bl"`
Sharpen float64 `cache:"sh"`
Gamma float64 `cache:"ga"`
Contrast float64 `cache:"co"`
Brightness float64 `cache:"br"`
}
type Engine interface {
Transform(img image.Image, o *ImageOptions) (image.Image, error)
Resize(img image.Image, w, h int, fit bool) (image.Image, error)
}
func New() Engine {
return ImageEngine{}
}
func (opt *ImageOptions) TagMap() map[string]string {
val := reflect.ValueOf(opt).Elem()
tagMap := make(map[string]string, 0)
//loop over fields to extract tags and values
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
var value string
key := typeField.Tag.Get("cache")
switch valueField.Kind() {
case reflect.Int:
value = strconv.Itoa(int(valueField.Int()))
case reflect.Float64, reflect.Float32:
value = strconv.FormatFloat(valueField.Float(), 'f', 1, 64)
case reflect.Bool:
if valueField.Bool() == true {
value = "1"
} else {
value = "0"
}
default:
value = valueField.String()
}
if key != "" {
tagMap[key] = value
}
}
return tagMap
}