From 4b6fe76fc80e3314a5e95b7f2960c7a4a42283c8 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Thu, 22 Apr 2021 19:51:34 +0300 Subject: [PATCH] Initial commit --- go.mod | 5 ++++ go.sum | 2 ++ main.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..12e3423 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/velizarov.ru/cli/tool/image + +go 1.13 + +require github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..96adbed --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ= +github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8= diff --git a/main.go b/main.go new file mode 100644 index 0000000..9e7e9b0 --- /dev/null +++ b/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "bytes" + "fmt" + "image" + "image/jpeg" + _ "image/jpeg" + _ "image/png" + "io/fs" + "io/ioutil" + "os" + "path/filepath" + "strconv" + "strings" + + "github.com/nfnt/resize" +) + +func resizeImage(originalImage *bytes.Buffer, width uint, ext string) (*bytes.Buffer, error) { + image, _, err := image.Decode(originalImage) + + if err != nil { + return nil, err + } + + //newImage := resize.Thumbnail(width, width, image, resize.NearestNeighbor) + newImage := resize.Resize(width, 0, image, resize.NearestNeighbor) + + out := new(bytes.Buffer) + /* + switch ext { + case ".png": + png.Encode(out, newImage) + case ".jpg", ".jpeg": + }*/ + jpeg.Encode(out, newImage, nil) + + return out, nil +} + +func main() { + //out := os.Stdout + if !(len(os.Args) == 3) { + panic("usage go run main.go . [width]") + } + path := os.Args[1] + + width, err := strconv.ParseUint(os.Args[2], 10, 0) + + if err != nil { + panic("Wrong value for image width") + } + + files, err := os.ReadDir(path) + + if err != nil { + panic("Wrong value for image width") + } + + err = os.MkdirAll("output"+string(os.PathSeparator)+strconv.FormatUint(width, 10), 0777) + if err != nil { + panic(err) + } + + for _, file := range files { + if !file.IsDir() { + if err != nil { + panic(err) + } + fmt.Print(file.Name(), " => ") + switch filepath.Ext(file.Name()) { + case ".png", ".jpg", ".jpeg": + originalImage, err := ioutil.ReadFile(path + string(os.PathSeparator) + file.Name()) + if err != nil { + panic(err) + } + newImage, err := resizeImage(bytes.NewBuffer(originalImage), uint(width), filepath.Ext(file.Name())) + + filename := file.Name() + filename = strings.Replace(filename, ".png", ".jpg", 1) + filename = strings.Replace(filename, ".jpeg", ".jpg", 1) + filename = "output" + string(os.PathSeparator) + strconv.FormatUint(width, 10) + string(os.PathSeparator) + filename + fmt.Println(filename) + err = os.WriteFile(filename, newImage.Bytes(), fs.ModePerm) + + if err != nil { + panic(err) + } + } + } + } +}