forked from noelyahan/mergi
/
flags.go
139 lines (121 loc) · 3.2 KB
/
flags.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
package main
import (
"fmt"
"github.com/noelyahan/mergi"
"github.com/noelyahan/impexp"
"image"
"net/url"
"strconv"
"strings"
)
type arrFlags []string
func (i *arrFlags) String() string {
return ""
}
func (i *arrFlags) Set(value string) error {
*i = append(*i, value)
return nil
}
func getFlagOrder(arr []string) []string {
res := make([]string, 0)
imgCount := -1
appendInt := func(i int, s string) string {
return strconv.Itoa(i) + "#" + s
}
for _, v := range arr {
v = strings.Replace(v, "-", "", -1)
if v == "c" || v == flagCrop {
res = append(res, appendInt(imgCount, flagCrop))
} else if v == "r" || v == flagResize {
res = append(res, appendInt(imgCount, flagResize))
} else if v == "w" || v == flagWatermark {
res = append(res, appendInt(imgCount, flagWatermark))
} else if v == "f" || v == flagFinal {
imgCount++
res = append(res, appendInt(imgCount, flagFinal))
imgCount++
} else if v == "i" || v == flagImage {
imgCount++
}
}
return res
}
func getWatermarkImageXY(wmStr string) (image.Image, int, int) {
wmImgXY := strings.Split(wmStr, " ")
if len(wmImgXY) == 3 {
x, _ := strconv.Atoi(wmImgXY[1])
y, _ := strconv.Atoi(wmImgXY[2])
var img image.Image
var err error
if isValidURL(wmImgXY[0]) {
img, err = mergi.Import(impexp.NewURLImporter(wmImgXY[0]))
} else {
img, err = mergi.Import(impexp.NewFileImporter(wmImgXY[0]))
}
if err != nil {
return nil, 0, 0
}
return img, x, y
}
x, _ := strconv.Atoi(wmImgXY[4])
y, _ := strconv.Atoi(wmImgXY[5])
w, _ := strconv.Atoi(wmImgXY[2])
h, _ := strconv.Atoi(wmImgXY[3])
var img image.Image
var err error
if isValidURL(wmImgXY[0]) {
img, err = mergi.Import(impexp.NewURLImporter(wmImgXY[0]))
} else {
img, err = mergi.Import(impexp.NewFileImporter(wmImgXY[0]))
}
if err != nil {
return nil, 0, 0
}
img, err = mergi.Resize(img, uint(w), uint(h))
if err != nil {
fmt.Printf("Mergi cannot complete the resize: [%v]", err)
}
return img, x, y
}
func getResizeWH(rFlags arrFlags, resizeStr string) (uint, uint) {
resizeWH := strings.Split(resizeStr, " ")
if len(rFlags) != 0 {
if len(resizeWH) == 2 {
rw, _ := strconv.Atoi(resizeWH[0])
rh, _ := strconv.Atoi(resizeWH[1])
return uint(rw), uint(rh)
}
}
fmt.Println("mergi wants you to enter width and height", resizeStr, ":)")
return 0, 0
}
func getCropPoints(cFlags arrFlags, cropStr string) (image.Point, image.Point) {
cropXYXY := strings.Split(cropStr, " ")
if len(cropXYXY) == 4 {
x1, _ := strconv.Atoi(cropXYXY[0])
y1, _ := strconv.Atoi(cropXYXY[1])
x2, _ := strconv.Atoi(cropXYXY[2])
y2, _ := strconv.Atoi(cropXYXY[3])
p1 := image.Pt(x1, y1)
p2 := image.Pt(x2, y2)
return p1, p2
}
fmt.Println("mergi wants you to enter point X and point Y along with width and height to crop", cFlags[0], ":)")
return image.Pt(-1, -1), image.Pt(-1, -1)
}
func getAnimationParams(animStr string) (string, int) {
splitTypeDelay := strings.Split(animStr, " ")
if len(splitTypeDelay) == 2 {
animType := splitTypeDelay[0]
delay, _ := strconv.Atoi(splitTypeDelay[1])
return animType, delay
}
return "", 0
}
func isValidURL(toTest string) bool {
_, err := url.ParseRequestURI(toTest)
if err != nil {
return false
}
return true
}