/
main.go
113 lines (93 loc) · 2.64 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/noelyahan/mergi"
"github.com/noelyahan/impexp"
"image"
"image/gif"
"os"
"strings"
)
type mergiResult struct {
image image.Image
animation gif.GIF
}
const (
version = "1.0.0"
flagImage = "image"
flagCrop = "crop"
flagResize = "resize"
flagWatermark = "watermark"
flagFinal = "final"
sprite = "sprite"
smooth = "smooth"
)
func main() {
var iFlags arrFlags
var cFlags arrFlags
var rFlags arrFlags
var wFlags arrFlags
const (
templateMsg = "Enter a merge template string ex: TBTBTB"
imagesMsg = "Enter images that want to merge ex: /path/img1 or url"
resizeMsg = "Enter resize width and height of the output ex: 100 200"
cropMsg = "Enter crop points and height and width ex: x y w h"
wmMsg = "Enter watermark image and points to place it, [-r w h] is optional ex: /path/img -r w h x y"
animMsg = "Enter animation type=[sprite, slide] and the delay to get mergi gif animation ex: smooth 10"
outMsg = "Enter image outputs file ex: out.png or out.jpg"
finalMsg = "Enter true if you want to process the final output"
)
flag.Usage = setFlagUsage()
template := flag.String("t", "T", templateMsg)
animation := flag.String("a", "", animMsg)
out := flag.String("o", "out.png", outMsg)
flag.Var(&iFlags, "i", imagesMsg)
flag.Var(&rFlags, "r", resizeMsg)
flag.Var(&cFlags, "c", cropMsg)
flag.Var(&wFlags, "w", wmMsg)
var final string
flag.StringVar(&final, "f", "false", finalMsg)
flag.Parse()
if len(*animation) > 0 && *out == "out.png" {
*out = "out.gif"
}
// input validation + data preparation
paths := getFilePaths(iFlags)
tokens := strings.Split(*template, "")
newPaths := getPreProcessTemplatePaths(tokens, paths, iFlags)
if len(newPaths) != len(paths) {
paths = newPaths
}
// pre-load images
imgs, err := getImagesFromPaths(paths)
if err != nil {
fmt.Println(err)
return
}
// validate the job and images
tasks := getFlagOrder(os.Args)
jobs := getJobMap(tasks)
res := processFlaggedJobs(imgs, *template, *animation, jobs, cFlags, rFlags, wFlags)
if res.image != nil {
mergi.Export(impexp.NewFileExporter(res.image, *out))
} else {
mergi.Export(impexp.NewAnimationExporter(res.animation, *out))
}
msg := fmt.Sprintf("mergi success !, final image saved to %s", *out)
fmt.Println(msg)
}
func setFlagUsage() func() {
return func() {
banner := `
╔╦╗╔═╗╦═╗╔═╗╦
║║║║╣ ╠╦╝║ ╦║
╩ ╩╚═╝╩╚═╚═╝╩
let's go & make imaging fun
http://mergi.io
version %s
`
fmt.Fprintf(os.Stderr, banner, version)
flag.PrintDefaults()
}
}