Fix text-image profile position for Twitter

S

S

s

s

s
This commit is contained in:
Daniele Tonon
2023-12-21 14:50:27 +01:00
parent 89a1bb3104
commit 58f265c4e2

View File

@@ -25,8 +25,8 @@ import (
const ( const (
MAX_LINES = 20 MAX_LINES = 20
MAX_CHARS_PER_LINE = 51 MAX_CHARS_PER_LINE = 50
MAX_CHARS_PER_QUOTE_LINE = 48 MAX_CHARS_PER_QUOTE_LINE = 46
FONT_SIZE = 7 FONT_SIZE = 7
FONT_DPI = 260 FONT_DPI = 260
@@ -67,7 +67,7 @@ func renderImage(w http.ResponseWriter, r *http.Request) {
lines := normalizeText( lines := normalizeText(
replaceUserReferencesWithNames(r.Context(), replaceUserReferencesWithNames(r.Context(),
renderQuotesAsBlockPrefixedText(r.Context(), renderQuotesAsBlockPrefixedText(r.Context(),
data.event.Content, content,
), ),
), ),
breakWords, breakWords,
@@ -213,13 +213,15 @@ func cropToSquare(img image.Image) image.Image {
func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.ProfileMetadata, date string) (image.Image, error) { func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.ProfileMetadata, date string) (image.Image, error) {
width := 700 width := 700
height := 525 height := 525
paddingLeft := 5 paddingLeft := 25
barExtraPadding := 5
switch style { switch style {
case StyleTelegram: case StyleTelegram:
paddingLeft += 10 paddingLeft += 10
width -= 10 width -= 10
case StyleTwitter: case StyleTwitter:
height = width * 268 / 512 height = width * 268 / 512
barExtraPadding = 105
} }
img := gg.NewContext(width, height) img := gg.NewContext(width, height)
@@ -236,8 +238,8 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
lineSpacing := 0.3 lineSpacing := 0.3
lineHeight := float64(FONT_SIZE)*FONT_DPI/72.0 + float64(FONT_SIZE)*lineSpacing*FONT_DPI/72.0 lineHeight := float64(FONT_SIZE)*FONT_DPI/72.0 + float64(FONT_SIZE)*lineSpacing*FONT_DPI/72.0
for i, line := range lines { for i, line := range lines {
y := float64(i)*lineHeight + 50 // Calculate the Y position for each line y := float64(i)*lineHeight + 50 // Calculate the Y position for each line
img.DrawString(line, float64(20+paddingLeft), y) // Draw the line at the calculated Y position img.DrawString(line, float64(paddingLeft), y) // Draw the line at the calculated Y position
} }
// Draw black bar at the bottom // Draw black bar at the bottom
@@ -258,14 +260,14 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
} }
// Draw author's image from URL // Draw author's image from URL
authorTextX := paddingLeft + 20 authorTextX := paddingLeft + barExtraPadding
if metadata.Picture != "" { if metadata.Picture != "" {
authorImage, err := fetchImageFromURL(metadata.Picture) authorImage, err := fetchImageFromURL(metadata.Picture)
if err != nil { if err != nil {
return nil, err return nil, err
} }
resizedAuthorImage := resize.Resize(uint(barHeight-20), uint(barHeight-20), roundImage(cropToSquare(authorImage)), resize.Lanczos3) resizedAuthorImage := resize.Resize(uint(barHeight-20), uint(barHeight-20), roundImage(cropToSquare(authorImage)), resize.Lanczos3)
img.DrawImage(resizedAuthorImage, paddingLeft+20, height-barHeight+10) img.DrawImage(resizedAuthorImage, paddingLeft+barExtraPadding, height-barHeight+10)
authorTextX += 65 authorTextX += 65
} }
@@ -274,6 +276,15 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
img.SetColor(color.White) img.SetColor(color.White)
img.DrawStringWrapped(metadata.ShortName(), float64(authorTextX), float64(authorTextY), 0, 0, float64(width-authorTextX*2), 1.5, gg.AlignLeft) img.DrawStringWrapped(metadata.ShortName(), float64(authorTextX), float64(authorTextY), 0, 0, float64(width-authorTextX*2), 1.5, gg.AlignLeft)
// Draw the logo
logo, _ := static.ReadFile("static/logo.png")
stampImg, _ := png.Decode(bytes.NewBuffer(logo))
stampWidth := stampImg.Bounds().Dx()
stampHeight := stampImg.Bounds().Dy()
stampX := width - stampWidth - paddingLeft - barExtraPadding
stampY := height - stampHeight - 20
img.DrawImage(stampImg, stampX, stampY)
// Draw event date // Draw event date
layout := "2006-01-02 15:04:05" layout := "2006-01-02 15:04:05"
parsedTime, _ := time.Parse(layout, date) parsedTime, _ := time.Parse(layout, date)
@@ -284,16 +295,7 @@ func drawImage(lines []string, ttf *truetype.Font, style Style, metadata sdk.Pro
Hinting: font.HintingFull, Hinting: font.HintingFull,
})) }))
img.SetColor(color.RGBA{160, 160, 160, 255}) img.SetColor(color.RGBA{160, 160, 160, 255})
img.DrawStringWrapped(formattedDate, float64(width-360), float64(authorTextY+3), 0, 0, float64(200), 1.5, gg.AlignRight) img.DrawStringWrapped(formattedDate, float64(width-paddingLeft-barExtraPadding-stampWidth-260), float64(authorTextY+3), 0, 0, float64(240), 1.5, gg.AlignRight)
// Draw the logo
logo, _ := static.ReadFile("static/logo.png")
stampImg, _ := png.Decode(bytes.NewBuffer(logo))
stampWidth := stampImg.Bounds().Dx()
stampHeight := stampImg.Bounds().Dy()
stampX := width - stampWidth - 20
stampY := height - stampHeight - 20
img.DrawImage(stampImg, stampX, stampY)
return img.Image(), nil return img.Image(), nil
} }