youbbs avatar

Golang 模仿微信群头像九宫格

🕙 by youbbs

在主题前面添加类似微信群用户头像九宫格,这个想法很早就有,拖到现在才实现,楼主在右下角,最新回复用户在前。

感谢 Li BuBai 写好的 核心功能,省掉很多繁琐的工作。

Go: Merge
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
func Merge(src []io.Reader, dst io.Writer) error {
	defer func() {
		if r := recover(); r != nil {
			log.Println("Merge.recover:", r)
		}
	}()
	var err error
	imagePoints := getXy(len(src))
	width := getWidth(len(src))

	//Create a background image
	background := image.NewRGBA(image.Rect(0, 0, 132, 132))

	//Set the background to gray (127, 127, 127)
	for m := 0; m < 132; m++ {
		for n := 0; n < 132; n++ {
			background.SetRGBA(m, n, color.RGBA{R: 214, G: 214, B: 214, A: 0})
		}
	}

	for i, v := range imagePoints {
		x := v.x
		y := v.y

		fOut := MemoryNewWriter(nil)

		_, _, err = Scale(src[i], fOut, width, width, 100)
		if err != nil {
			return err
		}

		//file2draw, err := jpeg.Decode(fOut)
		file2draw, _, err := image.Decode(fOut)
		if err != nil {
			return err
		}
		draw.Draw(background, background.Bounds(), file2draw, file2draw.Bounds().Min.Sub(image.Pt(x, y)), draw.Src)
	}

	return jpeg.Encode(dst, background, nil)
}

type Point struct {
	x, y int
}

func getXy(size int) []*Point {
	s := make([]*Point, size)
	var _x, _y int

	if size == 1 {
		_x, _y = 6, 6
		s[0] = &Point{_x, _y}
	}
	if size == 2 {
		_x, _y = 4, 4
		s[0] = &Point{_x, 132/2 - 60/2}
		s[1] = &Point{60 + 2*_x, 132/2 - 60/2}
	}
	if size == 3 {
		_x, _y = 4, 4
		s[0] = &Point{132/2 - 60/2, _y}
		s[1] = &Point{_x, 60 + 2*_y}
		s[2] = &Point{60 + 2*_y, 60 + 2*_y}
	}
	if size == 4 {
		_x, _y = 4, 4
		s[0] = &Point{_x, _y}
		s[1] = &Point{_x*2 + 60, _y}
		s[2] = &Point{_x, 60 + 2*_y}
		s[3] = &Point{60 + 2*_y, 60 + 2*_y}
	}
	if size == 5 {
		_x, _y = 3, 3
		s[0] = &Point{(132 - 40*2 - _x) / 2, (132 - 40*2 - _y) / 2}
		s[1] = &Point{(132-40*2-_x)/2 + 40 + _x, (132 - 40*2 - _y) / 2}
		s[2] = &Point{_x, (132-40*2-_x)/2 + 40 + _y}
		s[3] = &Point{_x*2 + 40, (132-40*2-_x)/2 + 40 + _y}
		s[4] = &Point{_x*3 + 40*2, (132-40*2-_x)/2 + 40 + _y}
	}
	if size == 6 {
		_x, _y = 3, 3
		s[0] = &Point{_x, (132 - 40*2 - _x) / 2}
		s[1] = &Point{_x*2 + 40, (132 - 40*2 - _x) / 2}
		s[2] = &Point{_x*3 + 40*2, (132 - 40*2 - _x) / 2}
		s[3] = &Point{_x, (132-40*2-_x)/2 + 40 + _y}
		s[4] = &Point{_x*2 + 40, (132-40*2-_x)/2 + 40 + _y}
		s[5] = &Point{_x*3 + 40*2, (132-40*2-_x)/2 + 40 + _y}
	}

	if size == 7 {
		_x, _y = 3, 3
		s[0] = &Point{(132 - 40) / 2, _y}
		s[1] = &Point{_x, _y*2 + 40}
		s[2] = &Point{_x*2 + 40, _y*2 + 40}
		s[3] = &Point{_x*3 + 40*2, _y*2 + 40}
		s[4] = &Point{_x, _y*3 + 40*2}
		s[5] = &Point{_x*2 + 40, _y*3 + 40*2}
		s[6] = &Point{_x*3 + 40*2, _y*3 + 40*2}
	}
	if size == 8 {
		_x, _y = 3, 3
		s[0] = &Point{(132 - 80 - _x) / 2, _y}
		s[1] = &Point{(132-80-_x)/2 + _x + 40, _y}
		s[2] = &Point{_x, _y*2 + 40}
		s[3] = &Point{_x*2 + 40, _y*2 + 40}
		s[4] = &Point{_x*3 + 40*2, _y*2 + 40}
		s[5] = &Point{_x, _y*3 + 40*2}
		s[6] = &Point{_x*2 + 40, _y*3 + 40*2}
		s[7] = &Point{_x*3 + 40*2, _y*3 + 40*2}
	}
	if size == 9 {
		_x, _y = 3, 3
		s[0] = &Point{_x, _y}
		s[1] = &Point{_x*2 + 40, _y}
		s[2] = &Point{_x*3 + 40*2, _y}
		s[3] = &Point{_x, _y*2 + 40}
		s[4] = &Point{_x*2 + 40, _y*2 + 40}
		s[5] = &Point{_x*3 + 40*2, _y*2 + 40}
		s[6] = &Point{_x, _y*3 + 40*2}
		s[7] = &Point{_x*2 + 40, _y*3 + 40*2}
		s[8] = &Point{_x*3 + 40*2, _y*3 + 40*2}
	}
	return s
}

顺便把头像及九宫格头像保存在数据库里,减少小文件 io

标题参与用户头像九宫格在参与用户大于9个才好看,4个头像也不错。

相关问题

  • 大量头像文件(小文件4~6KB)保存在数据库的利弊?

💘 相关文章

评论

共5条关于"Golang 模仿微信群头像九宫格"的评论

小萌娘 avatar
#1 小萌娘 回复

wow 你都已经用上了啊 这个什么时候推送到github啊 到时学学习下 感觉挺酷的

写一条评论

Based on Golang + fastHTTP + sdb | go1.17.3 Processed in 1ms