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
|
package main
import (
"bytes"
"errors"
"image"
_ "image/gif"
_ "image/jpeg"
"image/png"
"golang.org/x/image/draw"
_ "golang.org/x/image/webp"
)
const avatarSize = 256
func cropAvatar(data []byte, x, y, size int) ([]byte, error) {
cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
if err != nil {
return nil, errors.New("Avatar must be a PNG, JPEG, GIF or WebP image")
}
if cfg.Width*cfg.Height > 25_000_000 {
return nil, errors.New("Avatar image is too large, keep it under 25 megapixels")
}
img, _, err := image.Decode(bytes.NewReader(data))
if err != nil {
return nil, errors.New("Avatar must be a PNG, JPEG, GIF or WebP image")
}
b := img.Bounds()
if size <= 0 || size > b.Dx() || size > b.Dy() {
size = min(b.Dx(), b.Dy())
x = (b.Dx() - size) / 2
y = (b.Dy() - size) / 2
}
x = max(0, min(x, b.Dx()-size))
y = max(0, min(y, b.Dy()-size))
src := image.Rect(b.Min.X+x, b.Min.Y+y, b.Min.X+x+size, b.Min.Y+y+size)
dst := image.NewRGBA(image.Rect(0, 0, avatarSize, avatarSize))
draw.CatmullRom.Scale(dst, dst.Bounds(), img, src, draw.Src, nil)
var out bytes.Buffer
if err := png.Encode(&out, dst); err != nil {
return nil, err
}
return out.Bytes(), nil
}
|