Backend: added 'CompressImage' function.
This commit is contained in:
parent
0f1325bb6f
commit
fc4326e715
4 changed files with 58 additions and 1 deletions
3
backend/.gitignore
vendored
3
backend/.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
config.json
|
config.json
|
||||||
|
.temp/**
|
46
backend/core/img.go
Normal file
46
backend/core/img.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,8 @@
|
||||||
module backend
|
module backend
|
||||||
|
|
||||||
go 1.23.6
|
go 1.23.6
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/disintegration/imaging v1.6.2 // indirect
|
||||||
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
|
||||||
|
)
|
||||||
|
|
5
backend/go.sum
Normal file
5
backend/go.sum
Normal file
|
@ -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=
|
Loading…
Add table
Reference in a new issue