2021-11-19 18:14:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-06-13 06:37:16 +00:00
|
|
|
"flag"
|
|
|
|
"git.bbr-dev.info/brajkovic/resource_manager/image"
|
|
|
|
"io/fs"
|
|
|
|
"io/ioutil"
|
2021-11-19 18:14:43 +00:00
|
|
|
"log"
|
2022-06-13 06:37:16 +00:00
|
|
|
"path/filepath"
|
2021-11-19 18:14:43 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-06-13 06:37:16 +00:00
|
|
|
folder := flag.Bool("folder", false, "copying folders instead of files")
|
|
|
|
in := flag.String("in", "", "input file/folder")
|
|
|
|
out := flag.String("out", "", "output file/folder")
|
|
|
|
x := flag.Int("x", 400, "min width")
|
|
|
|
y := flag.Int("y", 300, "min height")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *folder {
|
|
|
|
resizeFolder(*in, *out, *x, *y)
|
|
|
|
} else {
|
|
|
|
resize(*in, *out, *x, *y)
|
2021-11-19 18:14:43 +00:00
|
|
|
}
|
2022-06-13 06:37:16 +00:00
|
|
|
|
2021-11-19 18:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 06:37:16 +00:00
|
|
|
func resizeFolder(src, dest string, x, y int) {
|
|
|
|
filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
|
|
|
|
if !info.IsDir() {
|
|
|
|
destination := strings.ReplaceAll(path, src, dest)
|
|
|
|
log.Printf("moving %s to %s", path, destination)
|
|
|
|
resize(path, destination, x, y)
|
2021-11-19 18:14:43 +00:00
|
|
|
}
|
2022-06-13 06:37:16 +00:00
|
|
|
return err
|
|
|
|
})
|
2021-11-19 18:14:43 +00:00
|
|
|
}
|
|
|
|
|
2022-06-13 06:37:16 +00:00
|
|
|
func resize(src, dest string, x, y int) {
|
|
|
|
if src == "" || dest == "" {
|
|
|
|
log.Fatalln("Missing src or dest")
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := ioutil.ReadFile(src)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2021-11-19 18:14:43 +00:00
|
|
|
|
2022-06-13 06:37:16 +00:00
|
|
|
destination, err := image.ResizeImage(source, x, y)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(dest, destination, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2021-11-19 18:14:43 +00:00
|
|
|
}
|