-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathcommon.go
More file actions
42 lines (35 loc) · 880 Bytes
/
common.go
File metadata and controls
42 lines (35 loc) · 880 Bytes
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
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
package common
import (
"image"
"image/draw"
"image/png"
"os"
)
func CompositeIcons(images []image.Image, width, height, iconSize, padding int) image.Image {
iconNum := len(images)
destImg := image.NewRGBA(image.Rect(0, 0, width, height))
if iconNum == 0 {
return destImg
}
y := (height - iconSize) / 2
spaceW := width - iconSize*iconNum
x := (spaceW - (iconNum-1)*padding) / 2
for _, srcImg := range images {
draw.Draw(destImg, image.Rect(x, y, x+iconSize, y+iconSize), srcImg, image.Pt(0, 0), draw.Src)
x += iconSize + padding
}
return destImg
}
func SavePngFile(m image.Image, filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
return png.Encode(f, m)
}