-
Notifications
You must be signed in to change notification settings - Fork 0
/
article.go
132 lines (117 loc) · 3.15 KB
/
article.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
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
126
127
128
129
130
131
132
package draw
import (
_ "embed"
"image"
"log"
"github.com/fogleman/gg"
"github.com/sukso96100/srvogimg/res"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
)
func drawArticleOgImage(
title string,
authors string,
sitename string,
bgimgurl string,
logoimgurl string,
bgStartColor string,
bgEndColor string,
isDarkTheme bool,
filepath string) string {
dc := gg.NewContext(ogImgWidth, ogImgHeight)
userColors := res.GetThemeColors(isDarkTheme)
if bgimgurl != "" {
bgimg, err := LoadResizedBackgroundImage(bgimgurl)
if err == nil {
//Background image
dc.DrawImage(bgimg, 0, 0)
// Background image dimming
dc.DrawRectangle(0, 0, ogImgWidthFloat, ogImgHeightFloat)
if bgStartColor != "" {
bgcolor, _ := ParseHexColor(bgStartColor, 200)
dc.SetColor(bgcolor)
} else {
dc.SetColor(userColors.BackgroundColor)
}
dc.Fill()
}
} else {
// Background gradient
startColor := bgStartColor
endColor := bgEndColor
if startColor == "" {
startColor = res.DefaultGradientStartColor
}
if endColor == "" {
endColor = res.DefaultGradientEndColor
}
dc.DrawRectangle(0, 0, ogImgWidthFloat, ogImgHeightFloat)
dc.SetFillStyle(NewGradientBackground(startColor, endColor, 255))
dc.Fill()
}
// Title
f, err := opentype.Parse(res.Fontfile)
if err != nil {
log.Fatalf("Parse: %v", err)
}
face, err := opentype.NewFace(f, &opentype.FaceOptions{
Size: 80,
DPI: 72,
Hinting: font.HintingFull,
})
dc.SetFontFace(face)
dc.SetColor(userColors.TextColor)
dc.DrawStringWrapped(title, ogImgWidthFloat/2, ogImgHeightFloat*2/6, 0.5, 0.5, 1000, 1.0, gg.AlignCenter)
// Author
face, err = opentype.NewFace(f, &opentype.FaceOptions{
Size: 50,
DPI: 72,
Hinting: font.HintingFull,
})
dc.SetFontFace(face)
dc.SetColor(userColors.TextColor)
dc.DrawStringWrapped(authors, ogImgWidthFloat/2, ogImgHeightFloat*4/6, 0.5, 0.5, 1000, 1.0, gg.AlignCenter)
// Logo + Site name
face, err = opentype.NewFace(f, &opentype.FaceOptions{
Size: 40,
DPI: 72,
Hinting: font.HintingFull,
})
dc.SetFontFace(face)
dc.SetColor(userColors.TextColor)
// Calculate Logo + Site name row width
logoRowWidth := 0
var logoimg image.Image
var logoimgError error
if logoimgurl != "" {
logoimg, logoimgError = LoadResizedLogoImage(logoimgurl, 0, 80)
if logoimgError == nil {
logoRowWidth += logoimg.Bounds().Size().X
}
}
if sitename != "" {
w, _ := dc.MeasureString(sitename)
logoRowWidth += int(w)
}
if logoimgurl != "" && sitename != "" {
logoRowWidth += 10.0
}
// Draw Logo Images
logoRowItemAnchor := (ogImgWidth / 2) - (logoRowWidth / 2)
if logoimgurl != "" {
logoRowItemAnchor += (logoimg.Bounds().Size().X) / 2
dc.DrawImageAnchored(logoimg, logoRowItemAnchor, ogImgHeight*5/6, 0.5, 0.5)
logoRowItemAnchor += (logoimg.Bounds().Size().X) / 2
}
if logoimgurl != "" && sitename != "" {
logoRowItemAnchor += 10.0
}
if sitename != "" {
w, _ := dc.MeasureString(sitename)
logoRowItemAnchor += int(w / 2)
dc.DrawStringAnchored(sitename, float64(logoRowItemAnchor), float64(ogImgHeight*5/6), 0.5, 0.3)
}
imgfilePath := filepath + ".png"
dc.SavePNG(imgfilePath)
return imgfilePath
}