-
Notifications
You must be signed in to change notification settings - Fork 18
/
exec.go
135 lines (121 loc) · 3.2 KB
/
exec.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
// +build !noexec
package main
import (
"fmt"
"os"
"os/exec"
)
func init() {
// Program checks are supposed to work on all systems
if _, err := exec.LookPath("gm"); err == nil {
RegisterResizer("GraphicsMagick_thumbnail", graphicsMagickThumbnail)
} else {
fmt.Println("Cannot find gm in PATH, will skip GraphicsMagick tests")
}
if _, err := exec.LookPath("convert"); err == nil {
RegisterResizer("ImageMagick_thumbnail", imageMagickThumbnail)
RegisterResizer("ImageMagick_resize", imageMagickResize)
} else {
fmt.Println("Cannot find convert in PATH, will skip ImageMagick tests")
}
if _, err := exec.LookPath("vipsthumbnail"); err == nil {
RegisterResizer("vipsthumbnail", vipsthumbnail)
} else {
fmt.Println("Cannot find vipsthumbnail in PATH, will skip VIPS tests")
}
if _, err := exec.LookPath("epeg"); err == nil {
RegisterResizer("epeg", epegThumbnail)
} else {
fmt.Println("Cannot find epeg in PATH, will skip epeg tests")
}
}
func epegThumbnail(origName, newName string) (int, int64) {
origFileStat, _ := os.Stat(origName)
var args = []string{
"-m", "150",
origName,
newName,
}
var cmd *exec.Cmd
path, _ := exec.LookPath("epeg")
cmd = exec.Command(path, args...)
err := cmd.Run()
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
newFileStat, _ := os.Stat(newName)
return int(newFileStat.Size()), origFileStat.Size()
}
func vipsthumbnail(origName, newName string) (int, int64) {
origFileStat, _ := os.Stat(origName)
var args = []string{
"-s", "150",
"-o", newName,
origName,
}
var cmd *exec.Cmd
path, _ := exec.LookPath("vipsthumbnail")
cmd = exec.Command(path, args...)
err := cmd.Run()
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
newFileStat, _ := os.Stat(newName)
return int(newFileStat.Size()), origFileStat.Size()
}
func imageMagickThumbnail(origName, newName string) (int, int64) {
origFileStat, _ := os.Stat(origName)
var args = []string{
"-define", "jpeg:size=300x300",
"-thumbnail", "150x150>",
origName, newName,
}
var cmd *exec.Cmd
path, _ := exec.LookPath("convert")
cmd = exec.Command(path, args...)
err := cmd.Run()
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
newFileStat, _ := os.Stat(newName)
return int(newFileStat.Size()), origFileStat.Size()
}
func imageMagickResize(origName, newName string) (int, int64) {
origFileStat, _ := os.Stat(origName)
var args = []string{
"-resize", "150x150>",
origName, newName,
}
var cmd *exec.Cmd
path, _ := exec.LookPath("convert")
cmd = exec.Command(path, args...)
err := cmd.Run()
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
newFileStat, _ := os.Stat(newName)
return int(newFileStat.Size()), origFileStat.Size()
}
func graphicsMagickThumbnail(origName, newName string) (int, int64) {
origFileStat, _ := os.Stat(origName)
var args = []string{
"convert",
"-define", "jpeg:size=300x300",
"-thumbnail", "150x150>",
origName, newName,
}
var cmd *exec.Cmd
path, _ := exec.LookPath("gm")
cmd = exec.Command(path, args...)
err := cmd.Run()
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
newFileStat, _ := os.Stat(newName)
return int(newFileStat.Size()), origFileStat.Size()
}