/
main.go
135 lines (110 loc) · 2.93 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"bufio"
"flag"
"fmt"
"github.com/bradberger/optimizer"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/font"
_ "golang.org/x/image/riff"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/vp8"
_ "golang.org/x/image/vp8l"
_ "golang.org/x/image/webp"
_ "golang.org/x/image/webp/nycbcra"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io"
"mime"
"os"
"path"
)
func main() {
var img image.Image
var err error
var origFmt string
// Process flags.
mimeFlag := flag.String("format", "", "The mime type to output. If none specified, will default to the output file format or original format.")
qualityFlag := flag.Int("quality", 0, "The quality level for the final image")
widthFlag := flag.Uint("width", 0, "The width of the final image")
heightFlag := flag.Uint("height", 0, "The height of the final image")
dprFlag := flag.Float64("dpr", 1.0, "The Viewport DPR to optimize for")
downlinkFlag := flag.Float64("downlink", 0.384, "The downlink speed to optimize for")
saveDataFlag := flag.Bool("savedata", false, "Optimize to save data")
flag.Parse()
args := flag.Args()
numArgs := len(args)
stat, _ := os.Stdin.Stat()
// @todo If using stdin, decode stdin into image, otherwise decode file into image.
if (stat.Mode() & os.ModeCharDevice) == 0 {
img, origFmt, err = image.Decode(bufio.NewReader(os.Stdin))
} else {
// If no stdin and no file, return error.
if numArgs < 1 {
fmt.Println("You must supply a file via stdin or the first argument")
os.Exit(1)
}
// Try to open and read the file.
f, e := os.Open(args[0])
if e != nil {
fmt.Printf("Error reading file: %s\n", e)
os.Exit(1)
}
defer f.Close()
img, origFmt, err = image.Decode(bufio.NewReader(f))
}
// Check to see decoding suceeded.
if err != nil {
fmt.Printf("Could not decode image from stdin: %s\n", err)
os.Exit(1)
}
// Check for mime output flag.
target := ""
mimeType := *mimeFlag
if mimeType == "" {
// If using output file, get from extension.
if numArgs > 1 {
target = args[1]
} else if numArgs > 1 {
target = args[0]
}
if target != "" {
mimeType = mime.TypeByExtension(path.Ext(target))
} else {
// Still no mime? Set to input image.
mimeType = origFmt
}
}
// Output. Check if target out, otherwise write to stdout.
var w io.Writer
if numArgs > 1 {
// Output to file.
f, err := os.Create(args[1])
if err != nil {
fmt.Printf("Could not write to %s\n", args[1])
os.Exit(1)
}
defer f.Close()
w = bufio.NewWriter(f)
} else {
// Output to stdout.
w = bufio.NewWriter(os.Stdout)
}
// If here, have an io.Writer in w and are read to rock.
o := optimizer.Options{
Mime: mimeType,
Width: *widthFlag,
Height: *heightFlag,
Dpr: *dprFlag,
Quality: *qualityFlag,
SaveData: *saveDataFlag,
Downlink: *downlinkFlag,
}
err = optimizer.Encode(w, img, o)
if err != nil {
fmt.Printf("Error encoding image: %s", err)
os.Exit(1)
}
}