server: use fixed-size social media images
Signed-off-by: Naman Sood <mail@nsood.in>
This commit is contained in:
parent
ab6c3af0b7
commit
bd28e62c33
1 changed files with 5 additions and 18 deletions
23
server.go
23
server.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -206,15 +205,13 @@ func (s *server) loadStylesheet(res http.ResponseWriter, req *http.Request, file
|
||||||
}
|
}
|
||||||
|
|
||||||
func createImage(title, summary, url string, out io.Writer) error {
|
func createImage(title, summary, url string, out io.Writer) error {
|
||||||
imgWidth, imgPaddingX, imgPaddingY := 1200, 45, 90
|
imgWidth, imgHeight, imgPaddingX, imgPaddingY := 1200, 630, 45, 90
|
||||||
accentHeight := 7.5
|
accentHeight, spacerHeight := 7.5, 20.0
|
||||||
titleSize, summarySize, urlSize := 63.0, 42.0, 27.0
|
titleSize, summarySize, urlSize := 63.0, 42.0, 27.0
|
||||||
lineHeight := 1.5
|
lineHeight := 1.05
|
||||||
textWidth := float64(imgWidth - 2*imgPaddingX)
|
textWidth := float64(imgWidth - 2*imgPaddingX)
|
||||||
|
|
||||||
// temporarily set height = 0 for context only used to generate
|
draw := gg.NewContext(imgWidth, imgHeight)
|
||||||
// wrapped strings
|
|
||||||
draw := gg.NewContext(imgWidth, 0)
|
|
||||||
|
|
||||||
titleFont, err := gg.LoadFontFace("fonts/Nunito-Bold.ttf", titleSize)
|
titleFont, err := gg.LoadFontFace("fonts/Nunito-Bold.ttf", titleSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -234,17 +231,6 @@ func createImage(title, summary, url string, out io.Writer) error {
|
||||||
draw.SetFontFace(summaryFont)
|
draw.SetFontFace(summaryFont)
|
||||||
wrappedSummary := draw.WordWrap(summary, textWidth)
|
wrappedSummary := draw.WordWrap(summary, textWidth)
|
||||||
|
|
||||||
imgHeight := 2 * imgPaddingY
|
|
||||||
for range wrappedTitle {
|
|
||||||
imgHeight += int(math.Ceil(lineHeight * titleSize))
|
|
||||||
}
|
|
||||||
for range wrappedSummary {
|
|
||||||
imgHeight += int(math.Ceil(lineHeight * summarySize))
|
|
||||||
}
|
|
||||||
imgHeight += int(math.Ceil(lineHeight * urlSize))
|
|
||||||
|
|
||||||
// actual context with actual height
|
|
||||||
draw = gg.NewContext(imgWidth, imgHeight)
|
|
||||||
draw.SetHexColor("#fff")
|
draw.SetHexColor("#fff")
|
||||||
draw.DrawRectangle(0, 0, float64(imgWidth), float64(imgHeight))
|
draw.DrawRectangle(0, 0, float64(imgWidth), float64(imgHeight))
|
||||||
draw.Fill()
|
draw.Fill()
|
||||||
|
@ -260,6 +246,7 @@ func createImage(title, summary, url string, out io.Writer) error {
|
||||||
draw.DrawString(line, float64(imgPaddingX), offset)
|
draw.DrawString(line, float64(imgPaddingX), offset)
|
||||||
offset += lineHeight * titleSize
|
offset += lineHeight * titleSize
|
||||||
}
|
}
|
||||||
|
offset += spacerHeight
|
||||||
|
|
||||||
draw.SetFontFace(summaryFont)
|
draw.SetFontFace(summaryFont)
|
||||||
draw.SetHexColor("#999")
|
draw.SetHexColor("#999")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue