-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgutil.go
241 lines (233 loc) · 8.2 KB
/
imgutil.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"fmt"
"github.com/disintegration/imaging"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
)
func processImgThumbnails(mediaDirPath string, config appConfig) {
if dirExists(mediaDirPath) && config.useThumbs {
// ==================================================
// delete any old / no longer needed thumbnails
// ==================================================
imgFiles, err := listFilesByExt(mediaDirPath, thumbImageFileExtensions...)
check(err)
if len(imgFiles) > 0 {
for _, imgFile := range imgFiles {
thm := thumbImgFileNameRegexp.FindStringSubmatch(imgFile)
if thm != nil {
thSize, err := strconv.Atoi(thm[1])
check(err)
if !slices.Contains(config.thumbSizes, thSize) {
thumbFilePath := fmt.Sprintf("%s%c%s", mediaDirPath, os.PathSeparator, imgFile)
deleteFile(thumbFilePath)
sprintln(" - deleted an old / no longer needed thumbnail: " + thumbFilePath)
}
}
}
}
// ==================================================
// generate thumbnails
// ==================================================
imgFiles, err = listFilesByExt(mediaDirPath, thumbImageFileExtensions...)
check(err)
for _, imgFile := range imgFiles {
if strings.Contains(imgFile, thumbImgFileSuffix) {
continue
}
imgFilePath := fmt.Sprintf("%s%c%s", mediaDirPath, os.PathSeparator, imgFile)
imgFileExt := filepath.Ext(imgFilePath)
imgFileSizeInMb := -1.0
for _, thSize := range config.thumbSizes {
thumbFilePath := imgFilePath + "_" + strconv.Itoa(thSize) + thumbImgFileSuffix + imgFileExt
if !fileExists(thumbFilePath) {
if imgFileSizeInMb < 0 {
var err error
imgFileSizeInMb, err = getFileSizeInMb(imgFilePath)
if err != nil {
sprintln(" - error reading image file info before thumbnail generation: "+imgFilePath, err)
continue
}
}
if imgFileSizeInMb >= config.thumbThreshold {
srcImg, err := imaging.Open(imgFilePath)
if err != nil {
sprintln(" - error opening image for thumbnail generation: "+imgFilePath, err)
continue
}
iw := srcImg.Bounds().Dx()
ih := srcImg.Bounds().Dy()
if iw > thSize || ih > thSize {
var tw, th int
if iw == ih {
tw = thSize
th = thSize
} else if iw > ih {
tw = thSize
th = thSize * ih / iw
} else {
th = thSize
tw = thSize * iw / ih
}
thImg := imaging.Resize(srcImg, tw, th, imaging.Lanczos)
if imgFileExt == ".jpg" || imgFileExt == ".jpeg" {
err = imaging.Save(thImg, thumbFilePath, imaging.JPEGQuality(config.jpegQuality))
} else if imgFileExt == ".png" {
err = imaging.Save(thImg, thumbFilePath, imaging.PNGCompressionLevel(config.pngCompressionLevel.Value()))
}
if err != nil {
sprintln(" - error generating a thumbnail for image: "+imgFilePath, err)
continue
}
sprintln(
" - generated a thumbnail: "+thumbFilePath,
" - original image: "+imgFilePath,
fmt.Sprintf(" - original image dimensions: %dx%d, thumbnail dimensions: %dx%d", iw, ih, tw, th),
)
thumbFileSizeInMb, err := getFileSizeInMb(thumbFilePath)
if err != nil {
sprintln(" - error reading thumbnail file info: "+thumbFilePath, err)
continue
}
println(fmt.Sprintf(" - original image file size: %.2f MB, thumbnail file size: %.2f MB\n", imgFileSizeInMb, thumbFileSizeInMb))
}
}
}
}
}
}
}
func deleteImgThumbnails(imgDirPath string, config appConfig) {
if dirExists(imgDirPath) && !config.useThumbs {
imgFiles, err := listFilesByExt(imgDirPath, thumbImageFileExtensions...)
check(err)
if len(imgFiles) > 0 {
for _, imgFile := range imgFiles {
thm := thumbImgFileNameRegexp.FindStringSubmatch(imgFile)
if thm != nil {
thumbFilePath := fmt.Sprintf("%s%c%s", imgDirPath, os.PathSeparator, imgFile)
deleteFile(thumbFilePath)
sprintln(" - deleted thumbnail: " + thumbFilePath)
}
}
}
}
}
func processOriginalMediaFiles(config appConfig, dryRun bool) bool {
resizeCnt := 0
if config.resizeOrigImages && config.maxImgSize > 0 {
deployMediaDir := fmt.Sprintf("%s%c%s", deployDirName, os.PathSeparator, mediaDirName)
if dirExists(deployMediaDir) {
ceTypeMediaDirs := []string{
fmt.Sprintf("%s%c%s", deployMediaDir, os.PathSeparator, deployPageDirName),
fmt.Sprintf("%s%c%s", deployMediaDir, os.PathSeparator, deployPostDirName),
}
if len(ceTypeMediaDirs) > 0 {
sprintln(" - inspecting original media files ...")
for _, ceTypeMediaDir := range ceTypeMediaDirs {
if dirExists(ceTypeMediaDir) {
mediaDirEntries, err := os.ReadDir(ceTypeMediaDir)
check(err)
if len(mediaDirEntries) > 0 {
for _, mediaDirEntry := range mediaDirEntries {
mediaDirEntryInfo, err := mediaDirEntry.Info()
check(err)
if mediaDirEntryInfo.IsDir() {
mediaDirEntryName := mediaDirEntryInfo.Name()
mediaDirPath := fmt.Sprintf("%s%c%s", ceTypeMediaDir, os.PathSeparator, mediaDirEntryName)
imgFiles, err := listFilesByExt(mediaDirPath, thumbImageFileExtensions...)
check(err)
if len(imgFiles) > 0 {
for _, imgFile := range imgFiles {
if !strings.Contains(imgFile, thumbImgFileSuffix) { // skip thumbnail files
imgFilePath := fmt.Sprintf("%s%c%s", mediaDirPath, os.PathSeparator, imgFile)
if processOriginalMediaFile(imgFilePath, config, dryRun) {
resizeCnt++
}
}
}
}
}
}
if resizeCnt > 0 {
if dryRun {
sprintln(" - " + strconv.Itoa(resizeCnt) + " original image(s) exceed the max size")
} else {
sprintln(" - resized " + strconv.Itoa(resizeCnt) + " original image(s)")
}
}
}
}
}
}
}
}
return resizeCnt > 0
}
func processOriginalMediaFile(mediaFilePath string, config appConfig, dryRun bool) bool {
if config.resizeOrigImages {
fileExt := strings.ToLower(filepath.Ext(mediaFilePath))
if slices.Contains(thumbImageFileExtensions, fileExt) {
maxImgSize := config.maxImgSize
if maxImgSize > 0 {
origImg, err := imaging.Open(mediaFilePath)
if err != nil {
sprintln(" - error opening image file for resizing: "+mediaFilePath, err)
} else {
ow := origImg.Bounds().Dx()
oh := origImg.Bounds().Dy()
if ow > maxImgSize || oh > maxImgSize {
// ==================================================
// calculate the new image dimensions
// ==================================================
var tw, th int
if ow == oh {
tw = maxImgSize
th = maxImgSize
} else if ow > oh {
tw = maxImgSize
th = maxImgSize * oh / ow
} else {
th = maxImgSize
tw = maxImgSize * ow / oh
}
if dryRun {
// ==================================================
// report the image file that exceeds the max size
// ==================================================
sprintln(
" - original image exceeds the max size: "+mediaFilePath,
fmt.Sprintf(" - original image dimensions: %dx%d, expected dimensions: %dx%d", ow, oh, tw, th),
)
return true
} else {
// ==================================================
// resize and save the image to the original file
// ==================================================
newImg := imaging.Resize(origImg, tw, th, imaging.Lanczos)
if fileExt == ".jpg" || fileExt == ".jpeg" {
err = imaging.Save(newImg, mediaFilePath, imaging.JPEGQuality(config.jpegQuality))
} else if fileExt == ".png" {
err = imaging.Save(newImg, mediaFilePath, imaging.PNGCompressionLevel(config.pngCompressionLevel.Value()))
}
// ==================================================
if err != nil {
sprintln(" - error saving resized image: "+mediaFilePath, err)
} else {
sprintln(
" - resized the original image: "+mediaFilePath,
fmt.Sprintf(" - original image dimensions: %dx%d, resized dimensions: %dx%d", ow, oh, tw, th),
)
return true
}
}
}
}
}
}
}
return false
}