/
imgutil.go
114 lines (111 loc) · 3.67 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
package main
import (
"fmt"
"github.com/disintegration/imaging"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
)
func processImgThumbnails(imgDirPath string, config appConfig) {
if dirExists(imgDirPath) && config.useThumbs {
// ==================================================
// delete any old / no longer needed thumbnails
// ==================================================
imgFiles := listFilesByExt(imgDirPath, thumbImageFileExtensions...)
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", imgDirPath, os.PathSeparator, imgFile)
deleteFile(thumbFilePath)
sprintln(" - deleted an old / no longer needed thumbnail: " + thumbFilePath)
}
}
}
}
// ==================================================
// generate thumbnails
// ==================================================
imgFiles = listFilesByExt(imgDirPath, thumbImageFileExtensions...)
for _, imgFile := range imgFiles {
if strings.Contains(imgFile, thumbImgFileSuffix) {
continue
}
imgFilePath := fmt.Sprintf("%s%c%s", imgDirPath, 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 {
println(" - error reading image file info before thumbnail generation: "+imgFilePath, err)
continue
}
}
if imgFileSizeInMb >= config.thumbThreshold {
srcImg, err := imaging.Open(imgFilePath)
if err != nil {
println(" - 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)
err = imaging.Save(thImg, thumbFilePath)
if err != nil {
println(" - error generating a thumbnail for image: "+imgFilePath, err)
continue
}
println(
" - 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 {
println(" - 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 := listFilesByExt(imgDirPath, thumbImageFileExtensions...)
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)
}
}
}
}
}