Write on the margins of the internet. Powered by the AT Protocol. margin.at
extension web atproto comments

better looks for annotation/bookmark OG images

+25 -37
+25 -37
backend/internal/api/og.go
··· 793 793 func generateOGImagePNG(author, text, quote, source, avatarURL string) image.Image { 794 794 width := 1200 795 795 height := 630 796 - padding := 120 796 + padding := 100 797 797 798 798 bgPrimary := color.RGBA{12, 10, 20, 255} 799 799 accent := color.RGBA{168, 85, 247, 255} 800 800 textPrimary := color.RGBA{244, 240, 255, 255} 801 801 textSecondary := color.RGBA{168, 158, 200, 255} 802 - textTertiary := color.RGBA{107, 95, 138, 255} 803 802 border := color.RGBA{45, 38, 64, 255} 804 803 805 804 img := image.NewRGBA(image.Rect(0, 0, width, height)) 806 805 807 806 draw.Draw(img, img.Bounds(), &image.Uniform{bgPrimary}, image.Point{}, draw.Src) 808 - draw.Draw(img, image.Rect(0, 0, width, 6), &image.Uniform{accent}, image.Point{}, draw.Src) 807 + draw.Draw(img, image.Rect(0, 0, width, 12), &image.Uniform{accent}, image.Point{}, draw.Src) 809 808 810 - if logoImage != nil { 811 - logoHeight := 50 812 - logoWidth := int(float64(logoImage.Bounds().Dx()) * (float64(logoHeight) / float64(logoImage.Bounds().Dy()))) 813 - drawScaledImage(img, logoImage, padding, 80, logoWidth, logoHeight) 814 - } else { 815 - drawText(img, "Margin", padding, 120, accent, 36, true) 816 - } 809 + avatarSize := 64 810 + avatarX := padding 811 + avatarY := padding 817 812 818 - avatarSize := 80 819 - avatarX := padding 820 - avatarY := 180 821 813 avatarImg := fetchAvatarImage(avatarURL) 822 814 if avatarImg != nil { 823 815 drawCircularAvatar(img, avatarImg, avatarX, avatarY, avatarSize) 824 816 } else { 825 817 drawDefaultAvatar(img, author, avatarX, avatarY, avatarSize, accent) 826 818 } 827 - 828 - handleX := avatarX + avatarSize + 24 829 - drawText(img, author, handleX, avatarY+50, textSecondary, 24, false) 830 - 831 - yPos := 280 832 - draw.Draw(img, image.Rect(padding, yPos, width-padding, yPos+1), &image.Uniform{border}, image.Point{}, draw.Src) 833 - yPos += 40 819 + drawText(img, author, avatarX+avatarSize+24, avatarY+42, textSecondary, 28, false) 834 820 835 821 contentWidth := width - (padding * 2) 822 + yPos := 220 836 823 837 824 if text != "" { 838 825 textLen := len(text) 839 - textSize := 32 826 + textSize := 32.0 840 827 textLineHeight := 42 841 - maxTextLines := 6 828 + maxTextLines := 5 842 829 843 830 if textLen > 200 { 844 - textSize = 28 831 + textSize = 28.0 845 832 textLineHeight = 36 846 - maxTextLines = 7 833 + maxTextLines = 6 847 834 } 848 835 849 - lines := wrapTextToWidth(text, contentWidth, textSize) 836 + lines := wrapTextToWidth(text, contentWidth, int(textSize)) 850 837 numLines := min(len(lines), maxTextLines) 851 838 852 839 for i := 0; i < numLines; i++ { ··· 854 841 if i == numLines-1 && len(lines) > numLines { 855 842 line += "..." 856 843 } 857 - drawText(img, line, padding, yPos+(i*textLineHeight), textPrimary, float64(textSize), false) 844 + drawText(img, line, padding, yPos+(i*textLineHeight), textPrimary, textSize, false) 858 845 } 859 846 yPos += (numLines * textLineHeight) + 40 860 847 } 861 848 862 849 if quote != "" { 863 850 quoteLen := len(quote) 864 - quoteSize := 24 851 + quoteSize := 24.0 865 852 quoteLineHeight := 32 866 - maxQuoteLines := 2 853 + maxQuoteLines := 3 867 854 868 855 if quoteLen > 150 { 869 - quoteSize = 20 856 + quoteSize = 20.0 870 857 quoteLineHeight = 28 871 - maxQuoteLines = 3 858 + maxQuoteLines = 4 872 859 } 873 860 874 - lines := wrapTextToWidth(quote, contentWidth-30, quoteSize) 861 + lines := wrapTextToWidth(quote, contentWidth-30, int(quoteSize)) 875 862 numLines := min(len(lines), maxQuoteLines) 876 - barHeight := numLines*quoteLineHeight + 10 863 + barHeight := numLines * quoteLineHeight 877 864 878 865 draw.Draw(img, image.Rect(padding, yPos, padding+6, yPos+barHeight), &image.Uniform{accent}, image.Point{}, draw.Src) 879 866 880 867 for i := 0; i < numLines; i++ { 881 868 line := lines[i] 882 - isLast := i == numLines-1 883 - if isLast && len(lines) > numLines { 869 + if i == numLines-1 && len(lines) > numLines { 884 870 line += "..." 885 871 } 886 - drawText(img, "\""+line+"\"", padding+24, yPos+28+(i*quoteLineHeight), textTertiary, float64(quoteSize), true) 872 + drawText(img, line, padding+24, yPos+24+(i*quoteLineHeight), textSecondary, quoteSize, true) 887 873 } 888 - yPos += 30 + (numLines * quoteLineHeight) + 30 874 + yPos += barHeight + 40 889 875 } 890 876 891 - drawText(img, source, padding, 580, textTertiary, 20, false) 877 + draw.Draw(img, image.Rect(padding, yPos, width-padding, yPos+1), &image.Uniform{border}, image.Point{}, draw.Src) 878 + yPos += 40 879 + drawText(img, source, padding, yPos+32, textSecondary, 24, false) 892 880 893 881 return img 894 882 }