Skip to content

Commit

Permalink
png encode
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jul 18, 2024
1 parent 384634f commit bb0bb4a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 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": {},
}
4 changes: 0 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
44 changes: 40 additions & 4 deletions internal/context/context.utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"image"
"image/jpeg"

"image/png"
"mime/multipart"
"strings"
)
Expand All @@ -21,8 +23,6 @@ func ExtractFile(file *multipart.FileHeader, allowedContent map[string]struct{},
return nil, err
}

println("File size: ", len(fileBytes))

return fileBytes, nil
}

Expand All @@ -40,6 +40,7 @@ func mapToArr(m map[string]struct{}) []string {
}

func compressImage(fileHeader *multipart.FileHeader, maxSizeMB int) ([]byte, error) {
fileExt := fileHeader.Header["Content-Type"][0]
file, err := fileHeader.Open()
if err != nil {
return nil, err
Expand All @@ -56,11 +57,20 @@ func compressImage(fileHeader *multipart.FileHeader, maxSizeMB int) ([]byte, err

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

switch fileExt {
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", fileExt)
}
if err != nil {
return nil, err
}
compressed = buf.Bytes()

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

return compressed, nil
}

// func resizeImage(img image.Image, width, height int) *image.RGBA {
// bounds := img.Bounds()

// if width == 0 && height == 0 {
// return nil
// }

// if width == 0 {
// width = bounds.Dx() * height / bounds.Dy()
// }
// if height == 0 {
// height = bounds.Dy() * width / bounds.Dx()
// }

// if width > 500 || height > 500 {
// scaleFactor := float64(500) / math.Max(float64(width), float64(height))
// width = int(float64(width) * scaleFactor)
// height = int(float64(height) * scaleFactor)
// }

// newImg := image.NewRGBA(image.Rect(0, 0, width, height))
// draw.CatmullRom.Scale(newImg, newImg.Bounds(), img, bounds, draw.Over, nil)

// return newImg
// }

0 comments on commit bb0bb4a

Please sign in to comment.