diff --git a/backend/.gitignore b/backend/.gitignore index 0cffcb3..31ad7ca 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -1 +1,2 @@ -config.json \ No newline at end of file +config.json +.temp/** \ No newline at end of file diff --git a/backend/core/img.go b/backend/core/img.go new file mode 100644 index 0000000..d4ccd47 --- /dev/null +++ b/backend/core/img.go @@ -0,0 +1,46 @@ +package core + +import ( + "backend/util" + "image" + "image/jpeg" + "os" + "path/filepath" + + "github.com/disintegration/imaging" +) + +func CompressImage(path string) { + // Creating folder + output file + if err := os.MkdirAll(filepath.Dir(".temp"), 0755); err != nil { + util.Logformatrn(util.ERROR, "Failed to create temp directory (%v)", err) + return + } + outputFile, err := os.Create(".temp/test.jpeg") + if err != nil { + util.Logformatrn(util.ERROR, "Failed to create file: %v", err) + } + defer outputFile.Close() + + // Opening image + Reducing its size and quality + img, err := imaging.Open(path) + if err != nil { + util.Logformatrn(util.ERROR, "Failed to open image: %v", err) + } + width := float64(img.Bounds().Dx()) + height := float64(img.Bounds().Dy()) + ratio := float64(0) + var imgOutput *image.NRGBA + if width < height { + ratio = height / width + imgOutput = imaging.Resize(img, 283, int(283*ratio), imaging.Lanczos) + } else { + ratio = width / height + imgOutput = imaging.Resize(img, int(283*ratio), 283, imaging.Lanczos) + } + + // Encoding to jpeg afterwards + if err := jpeg.Encode(outputFile, imgOutput, &jpeg.Options{Quality: 30}); err != nil { + util.Logformatrn(util.ERROR, "Failed to save image: %v", err) + } +} diff --git a/backend/go.mod b/backend/go.mod index 4164d1a..fad25eb 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,3 +1,8 @@ module backend go 1.23.6 + +require ( + github.com/disintegration/imaging v1.6.2 // indirect + golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect +) diff --git a/backend/go.sum b/backend/go.sum new file mode 100644 index 0000000..6e27fcd --- /dev/null +++ b/backend/go.sum @@ -0,0 +1,5 @@ +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=