/
textonimg.go
45 lines (37 loc) · 1.12 KB
/
textonimg.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
package azureog
import (
"image"
"image/color"
"github.com/fogleman/gg"
)
//TextOnImg given a path to an image, a font & size and a text it will return an Image with the
//given text printed in the ~middle of the image
func TextOnImg(request Request) (image.Image, error) {
bgImage, err := gg.LoadImage(request.BgImgPath)
if err != nil {
return nil, err
}
imgWidth := bgImage.Bounds().Dx()
imgHeight := bgImage.Bounds().Dy()
dc := gg.NewContext(imgWidth, imgHeight)
dc.DrawImage(bgImage, 0, 0)
if err := dc.LoadFontFace(request.FontPath, request.FontSize); err != nil {
return nil, err
}
x := float64(imgWidth / 2)
signatureOff := 80
y := float64((imgHeight / 2) - signatureOff)
textMargin := 60.0
maxWidth := float64(imgWidth) - textMargin
dc.SetColor(color.Black)
dc.DrawStringWrapped(request.Text, x+3, y+3, 0.5, 0.5, maxWidth, 1.5, gg.AlignCenter)
dc.SetColor(color.White)
dc.DrawStringWrapped(request.Text, x, y, 0.5, 0.5, maxWidth, 1.5, gg.AlignCenter)
return dc.Image(), nil
}
func Save(img image.Image, path string) error {
if err := gg.SavePNG(path, img); err != nil {
return err
}
return nil
}