-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.go
70 lines (58 loc) · 1.48 KB
/
grid.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
66
67
68
69
70
// Copyright(C) 2024 github.com/fsgo All Rights Reserved.
// Author: hidu <duv123@gmail.com>
// Date: 2024/4/6
package layout
import (
"bytes"
"image"
"image/draw"
"image/png"
)
type Grid struct {
Width int
Height int
Padding int // 照片周边留白
Gap int // 图片间隙
}
func (g *Grid) getPadding() int {
if g.Padding > 0 {
return g.Padding
}
return 10
}
func (g *Grid) getGap() int {
if g.Gap > 0 {
return g.Gap
}
return 5
}
func (g *Grid) getWH(sw int, sh int) (int, int) {
w := g.Width - 2*g.getPadding()
h := g.Height - 2*g.getPadding()
num1 := w / sw * h / sh
num2 := h / sw * w / sh
if num1 >= num2 {
return g.Width, g.Height
}
return g.Height, g.Width
}
func (g *Grid) Draw(sc image.Image) ([]byte, error) {
avatarWidth := sc.Bounds().Dx() + g.getGap()
avatarHeight := sc.Bounds().Dy() + g.getGap()
width, height := g.getWH(avatarWidth, avatarHeight)
im := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(im, im.Bounds(), image.White, image.Point{}, draw.Src)
startX := (width-(width/avatarWidth)*avatarWidth)/2 + g.getPadding()
startY := (height-(height/avatarHeight)*avatarHeight)/2 + g.getPadding()
for y := startY; y < height; y += avatarHeight {
for x := startX; x < width; x += avatarWidth {
if x+avatarWidth > width || y+avatarHeight > height {
continue
}
draw.Draw(im, image.Rect(x, y, width, height), sc, image.Point{}, draw.Over)
}
}
bf := &bytes.Buffer{}
err := png.Encode(bf, im)
return bf.Bytes(), err
}