-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage-gen.go
201 lines (169 loc) · 4.56 KB
/
image-gen.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/disintegration/imaging"
"github.com/stretchr/powerwalk"
//"github.com/urfave/cli"
)
// PathDirective contains instruction for traversing a path of images
type PathDirective struct {
Path string
Destination string
Ignore string
Recursive bool
Resize []ResizeDirective
}
// ResizeDirective contains instructions to resize an images within a path
type ResizeDirective struct {
Algorithm string
Suffix string
Width int
Height int
KeepAspectRatio bool
}
// Config contains a list of PathDirectives to run the program over
type Config struct {
Paths []PathDirective
}
func resampleFilterLookup(name string) imaging.ResampleFilter {
switch name {
default:
case "Box":
return imaging.Box
case "BSpline":
return imaging.BSpline
case "CatmullRom":
return imaging.CatmullRom
case "Lanczos":
return imaging.Lanczos
case "Linear":
return imaging.Linear
case "MitchellNetravali":
return imaging.MitchellNetravali
case "NearestNeighbor":
return imaging.NearestNeighbor
}
// Unreachable, but Go doesn't care...
return imaging.Box
}
func disassemblePaths(p PathDirective, path string) (string, string, string) {
extension := filepath.Ext(path)
_, name := filepath.Split(path)
name = name[0 : len(name)-len(extension)]
relativePath, _ := filepath.Rel(p.Path, path)
destPath := filepath.Dir(p.Destination + string(filepath.Separator) + relativePath)
return destPath, name, extension
}
func calculateSize(r ResizeDirective) (int, int) {
width := 0
height := 0
if r.KeepAspectRatio {
if r.Width > r.Height {
width = r.Width
} else {
height = r.Height
}
} else {
width = r.Width
height = r.Height
}
return width, height
}
func resizeDir(p PathDirective) {
var resizeWalk = func(path string, fileInfo os.FileInfo, _ error) error {
if p.Ignore != "" && strings.HasPrefix(path, p.Ignore) {
log.Printf("skip this directory: %s\n", path)
return nil
}
if fileInfo != nil && fileInfo.Mode().IsRegular() {
destPath, name, extension := disassemblePaths(p, path)
// ignore dot files
if name == "" {
return nil
}
img, err := imaging.Open(path)
if err != nil {
return err
}
err = os.MkdirAll(destPath, 0777)
if err != nil {
return err
}
for _, r := range p.Resize {
width, height := calculateSize(r)
dstImage := imaging.Resize(img, width, height, resampleFilterLookup(r.Algorithm))
err := imaging.Save(dstImage, (destPath + string(filepath.Separator) + name + r.Suffix + extension))
if verbose {
fmt.Printf("%s --> %s\n", path, destPath+string(filepath.Separator)+name+r.Suffix+extension)
}
if err != nil {
log.Println(err)
}
}
}
return nil
}
if p.Recursive {
err := error(nil)
if useConcurrency {
err = powerwalk.WalkLimit(p.Path, resizeWalk, runtime.NumCPU()*2)
} else {
err = filepath.Walk(p.Path, resizeWalk)
}
if err != nil {
log.Println(err)
}
}
}
var verbose = false
var useConcurrency = true
func main() {
flag.String("What", "0.1.1", "0.1.1")
flag.String("Version", "image-gen", "image-gen")
flag.String("Purpose", "build multiple resolution images for a static website", "build multiple resolution images for a static website")
concurrencyLevelPtr := flag.Int("concurrency-level", runtime.NumCPU(),
"Set the number of threads for image-gen to use")
configPathPtr := flag.String("c", "",
"Load configuration from `FILE`")
verbosePtr := flag.Bool("verbose", false,
"Run verbosely")
useConcurrencyPtr := flag.Bool("no-concurrency", false,
"Disable concurrent workers") //might be bad
flag.Parse()
concurrencyLevel := *concurrencyLevelPtr
configPath := *configPathPtr
verbose = *verbosePtr
useConcurrency = *useConcurrencyPtr
if configPath == "" {
fmt.Printf("config path parameter required\n")
}
file, _ := os.Open(configPath)
defer file.Close()
decoder := json.NewDecoder(file)
configuration := Config{}
// Set the runtime concurrency level
runtime.GOMAXPROCS(concurrencyLevel)
err := decoder.Decode(&configuration)
if err != nil {
log.Println(err)
}
for _, path := range configuration.Paths {
path.Path, _ = filepath.Abs(filepath.Clean(path.Path))
path.Destination, _ = filepath.Abs(filepath.Clean(path.Destination))
if path.Ignore != "" {
path.Ignore, _ = filepath.Abs(filepath.Clean(path.Ignore))
}
fmt.Printf("%s --> %s\nIgnoring %s\n\n", path.Path, path.Destination, path.Ignore)
resizeDir(path)
}
//return nil
//}
//app.Run(os.Args)
}