-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpfp.go
65 lines (56 loc) · 1.27 KB
/
pfp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"bytes"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"image"
"image/draw"
"image/jpeg"
"image/png"
"net/http"
)
func cropToSquare(imageData []byte) ([]byte, error) {
img, format, err := image.Decode(bytes.NewReader(imageData))
if err != nil {
return nil, err
}
bounds := img.Bounds()
width := bounds.Dx()
height := bounds.Dy()
size := width
if height < width {
size = height
}
startX := (width - size) / 2
startY := (height - size) / 2
cropRect := image.Rect(startX, startY, startX+size, startY+size)
cropped := image.NewRGBA(cropRect)
draw.Draw(cropped, cropped.Bounds(), img, cropRect.Min, draw.Src)
var buffer bytes.Buffer
switch format {
case "jpeg":
err = jpeg.Encode(&buffer, cropped, nil)
case "png":
err = png.Encode(&buffer, cropped)
default:
return nil, err
}
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
func pfpRoutes() {
r.GET("/pfp", authMiddleware(), func(c *gin.Context) {
userId := c.DefaultQuery("user", "")
if userId != "" {
http.ServeFile(c.Writer, c.Request, "./pfp/"+userId+".jpg")
}
session := sessions.Default(c)
pfp := session.Get("pfp")
if pfp == nil {
pfp = "./pfp/default_pfp.jpg"
}
http.ServeFile(c.Writer, c.Request, pfp.(string))
})
}