Skip to content

Commit

Permalink
Merge pull request #33 from isd-sgcu/dev
Browse files Browse the repository at this point in the history
resize png + crop
  • Loading branch information
bookpanda authored Jul 18, 2024
2 parents 0f52ebe + e95be4c commit 1569ed5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
2 changes: 0 additions & 2 deletions constant/user.constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ var AllowedContentType = map[string]struct{}{
"image/jpeg": {},
"image/jpg": {},
"image/png": {},
"image/gif": {},
"image/webp": {},
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ require (
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/image v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
)
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/isd-sgcu/rpkm67-go-proto v0.4.6 h1:yUBzUY3ftBfnI2x/MT+MsynBOlV5pOR143c9OTrVGuU=
github.com/isd-sgcu/rpkm67-go-proto v0.4.6/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.4.8 h1:tU6nCv4A34guBoDwkZvUzzs6z43NBzgLsSbGmX5QRYI=
github.com/isd-sgcu/rpkm67-go-proto v0.4.8/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-model v0.0.6 h1:pYlqOmeXGQIfHdOhyAta4kXkqnoLc4X3KWcAjPrAuds=
github.com/isd-sgcu/rpkm67-model v0.0.6/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc=
github.com/isd-sgcu/rpkm67-model v0.0.7 h1:3b8gf1Ocg+Ky4xocKtCqVCB3rFDg90IgEXRwNmHt0OE=
github.com/isd-sgcu/rpkm67-model v0.0.7/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
Expand Down Expand Up @@ -106,6 +102,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
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=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down Expand Up @@ -178,6 +176,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.24.0 h1:mnl8DM0o513X8fdIkmyFE/5hTYxbwYOjDS/+rK6qpRI=
golang.org/x/crypto v0.24.0/go.mod h1:Z1PMYSOR5nyMcyAVAIQSKCDwalqy85Aqn1x3Ws4L5DM=
golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
Expand Down
79 changes: 61 additions & 18 deletions internal/context/context.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@ import (
"fmt"
"image"
"image/jpeg"
"math"

"image/png"
"mime/multipart"
"strings"
)

func ExtractFile(file *multipart.FileHeader, allowedContent map[string]struct{}, maxSize int64) (data []byte, err error) {
if !isExisted(allowedContent, file.Header["Content-Type"][0]) {
format := file.Header["Content-Type"][0]
if !isExisted(allowedContent, format) {
return nil, errors.New("Allowed content type is " + fmt.Sprint(strings.Join(mapToArr(allowedContent), ", ")))
}

maxSizeMB := maxSize * 1024 * 1024
fileBytes, err := compressImage(file, int(maxSizeMB))
resizedImage, err := resizeImage(file, 500, 500)
if err != nil {
return nil, err
}

println("File size: ", len(fileBytes))
maxSizeMB := maxSize * 1024 * 1024
fileBytes, err := compressImage(resizedImage, format, int(maxSizeMB))
if err != nil {
return nil, err
}

return fileBytes, nil
}
Expand All @@ -39,28 +46,27 @@ func mapToArr(m map[string]struct{}) []string {
return arr
}

func compressImage(fileHeader *multipart.FileHeader, maxSizeMB int) ([]byte, error) {
file, err := fileHeader.Open()
if err != nil {
return nil, err
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
return nil, err
}

func compressImage(img image.Image, format string, maxSizeMB int) ([]byte, error) {
var quality int = 80
var compressed []byte
var err error

for {
buf := new(bytes.Buffer)
if err := jpeg.Encode(buf, img, &jpeg.Options{Quality: quality}); err != nil {

switch format {
case "image/jpeg", "image/jpg":
err = jpeg.Encode(buf, img, &jpeg.Options{Quality: quality})
case "image/png":
err = png.Encode(buf, img)
default:
return nil, fmt.Errorf("unsupported file type: %v", format)
}
if err != nil {
return nil, err
}
compressed = buf.Bytes()

compressed = buf.Bytes()
if len(compressed) <= maxSizeMB {
break
}
Expand All @@ -74,3 +80,40 @@ func compressImage(fileHeader *multipart.FileHeader, maxSizeMB int) ([]byte, err

return compressed, nil
}

func resizeImage(fileHeader *multipart.FileHeader, newWidth, newHeight int) (image.Image, error) {
format := fileHeader.Header["Content-Type"][0]
file, err := fileHeader.Open()
if err != nil {
return nil, err
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
return nil, err
}

origWidth := img.Bounds().Dx()
origHeight := img.Bounds().Dy()

xOffset := int(math.Max(0, float64(origWidth-newWidth)/2))
yOffset := int(math.Max(0, float64(origHeight-newHeight)/2))

cropRect := image.Rect(xOffset, yOffset, xOffset+newWidth, yOffset+newHeight)
var croppedImg image.Image
if format == "image/jpeg" || format == "image/jpg" {
croppedImg = img.(*image.YCbCr).SubImage(cropRect)
} else if format == "image/png" {
switch img := img.(type) {
case *image.RGBA:
croppedImg = img.SubImage(cropRect)
case *image.NRGBA:
croppedImg = img.SubImage(cropRect)
default:
return nil, fmt.Errorf("unsupported image type: %T", img)
}
}

return croppedImg, nil
}

0 comments on commit 1569ed5

Please sign in to comment.