-
Notifications
You must be signed in to change notification settings - Fork 15
/
convert.go
80 lines (69 loc) · 1.55 KB
/
convert.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strconv"
)
//worker unique identificator
type Worker struct {
id int
}
//
func (w *Worker) process(c chan string) {
jpg, err := regexp.Compile(`^.*.(JPG|jpeg|JPEG|jpg)$`)
if err != nil {
msg := fmt.Sprintf("Error: There is a problem with your regexp\n")
LogErrChannel <- msg
log.Println(msg)
os.Exit(1)
}
for {
imagePath := <-c
fi, _ := os.Stat(imagePath)
beforeSize := fi.Size()
quality := 82
if qualityCheck(quality, imagePath) != true {
LogInfoChannel <- fmt.Sprintf("File %s already compressed\n", imagePath)
continue
}
interlace := "PNG"
if jpg.MatchString(imagePath) == true {
interlace = "JPEG"
}
cmd := fmt.Sprintf("convert %s -sampling-factor 4:2:0 -strip -quality %d -interlace %s -colorspace sRGB %s",
imagePath,
quality,
interlace,
imagePath)
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
log.Println(out)
}
fl, _ := os.Stat(imagePath)
afterSize := fl.Size()
verbose := LogVerbose{
ID: w.id,
ImagePath: imagePath,
beforeSize: beforeSize,
afterSize: afterSize,
}
go verbose.push()
}
}
//check quality
func qualityCheck(quality int, file string) bool {
cmd := fmt.Sprintf("identify -format %s %s", "'%Q'", file)
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
log.Println(string(out))
log.Printf("Incorect file name %s", file)
}
qualityNum, _ := strconv.ParseInt(string(out), 10, 0)
if int64(quality) >= qualityNum {
return false
}
return true
}