-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_build.go
110 lines (99 loc) · 2.39 KB
/
image_build.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
package ffimage
import (
"encoding/json"
"fmt"
"os"
"os/exec"
ffmpeg "github.com/u2takey/ffmpeg-go"
)
// buildQuality
func (i *Image) buildQuality() *Image {
if i.Output.Quality == 0 {
return i
}
switch i.Output.Format {
case ImageFormatAVIF:
q := qualityFactor(0, 63, i.Output.Quality, true)
i.addArg(ffmpeg.KwArgs{"crf": q})
case ImageFormatJPEG:
q := qualityFactor(2, 31, i.Output.Quality, true)
i.addArg(ffmpeg.KwArgs{"qscale:v": q})
case ImageFormatJPEGXL:
q := qualityFactor(0, 100, i.Output.Quality, false)
i.addArg(ffmpeg.KwArgs{"qscale:v": q})
case ImageFormatWEBP:
q := qualityFactor(0, 100, i.Output.Quality, false)
i.addArg(ffmpeg.KwArgs{"quality": q})
}
return i
}
// buildLoop
func (i *Image) buildLoop() *Image {
if i.Output.Format == ImageFormatAPNG {
i.addArg(ffmpeg.KwArgs{"plays": i.Output.Loop})
} else {
i.addArg(ffmpeg.KwArgs{"loop": i.Output.Loop})
}
return i
}
// buildAfterQuality
func (i *Image) buildAfterQuality() *Image {
if i.Output.Quality == 0 {
return i
}
switch i.Output.Format {
case ImageFormatPNG:
q := qualityFactor(0, 100, i.Output.Quality, false)
exec.Command("pngquant", "--quality", fmt.Sprintf("0-%d", q), "-f", i.Output.Path, "-o", i.Output.Path).Run()
case ImageFormatGIF:
q := qualityFactor(0, 100, i.Output.Quality, false)
exec.Command("gifsicle", "-O3", fmt.Sprintf("--lossy=%d", q), i.Output.Path, "-o", i.Output.Path).Run()
}
return i
}
// buildBeforeEXIF
func (i *Image) buildBeforeEXIF() *Image {
if !i.Output.IsPreserved {
return i
}
tmpFile, err := os.CreateTemp("", "")
if err != nil {
return i
}
b, err := exec.Command("exiftool", "-json", i.Path).Output()
if err != nil {
return i
}
j := make([]map[string]interface{}, 0)
if err := json.Unmarshal(b, &j); err != nil {
return i
}
if len(j) == 0 {
return i
}
// Set SourceFile as * so the data extracted from exiftool can import to any file.
j[0]["SourceFile"] = "*"
//
b, err = json.Marshal(j)
if err != nil {
return i
}
if _, err := tmpFile.Write(b); err != nil {
return i
}
if err := tmpFile.Close(); err != nil {
return i
}
i.Output.EXIF = tmpFile.Name()
return i
}
// buildAfterEXIF
func (i *Image) buildAfterEXIF() *Image {
if !i.Output.IsPreserved {
return i
}
if err := exec.Command("exiftool", "-overwrite_original", fmt.Sprintf("-json=%s", i.Output.EXIF), i.Output.Path).Run(); err != nil {
return i
}
return i
}