This repository has been archived by the owner on Dec 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.go
63 lines (51 loc) · 1.45 KB
/
images.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
package funcs
import (
"fmt"
"strconv"
"strings"
"github.com/ernestoalejo/aeimagesflags"
)
func Thumbnail(servingURL string, strFlags string) (string, error) {
if servingURL == "" || strFlags == "" {
return "", nil
}
flags := aeimagesflags.Flags{
ExpiresDays: 365,
}
for _, part := range strings.Split(strFlags, ";") {
strFlag := strings.Split(part, "=")
if len(strFlag) != 2 {
return "", fmt.Errorf("templates: all flags should be in the form key=value")
}
switch strings.TrimSpace(strFlag[0]) {
case "width":
n, err := strconv.ParseUint(strFlag[1], 10, 64)
if err != nil {
return "", fmt.Errorf("templates: cannot parse flag: %s", err)
}
flags.Width = n
case "height":
n, err := strconv.ParseUint(strFlag[1], 10, 64)
if err != nil {
return "", fmt.Errorf("templates: cannot parse flag: %s", err)
}
flags.Height = n
case "square-crop":
flags.SquareCrop = (strFlag[1] == "true")
case "smart-square-crop":
flags.SmartSquareCrop = (strFlag[1] == "true")
case "original":
flags.Original = (strFlag[1] == "true")
case "size":
n, err := strconv.ParseUint(strFlag[1], 10, 64)
if err != nil {
return "", fmt.Errorf("templates: cannot parse flag: %s", err)
}
flags.Size = n
default:
return "", fmt.Errorf("templates: unknown image flag: %s", strFlag[0])
}
}
servingURL = strings.Replace(servingURL, "http://", "https://", 1)
return aeimagesflags.Apply(servingURL, flags), nil
}