-
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathfeatured-sponsors.svg.js
More file actions
199 lines (172 loc) · 5.14 KB
/
featured-sponsors.svg.js
File metadata and controls
199 lines (172 loc) · 5.14 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import featuredSponsors from "../../featured-sponsors.json"
import fs from "fs"
import path from "path"
import sizeOf from "image-size"
const baseUrl = import.meta.env.SITE
// Width of the composed SVG
const overallWidth = 814
// Maximum height a logo may have
const maxHeight = 50
// Lead sponsor height (doubled from regular sponsors)
const leadSponsorHeight = maxHeight * 2
// Maximum width a logo may have
const maxWidth = 200
// Lead sponsor maximum width (doubled from regular sponsors)
const leadSponsorMaxWidth = maxWidth * 2
// Horizontal padding between logos
const xPadding = 40
// Vertical padding between rows of logos
const yPadding = 20
// Additional padding below lead sponsors
const leadSponsorBottomPadding = 80
const buildResponse = () => {
let images = []
let currentY = 30
// First process lead sponsors
const leadSponsors = featuredSponsors.filter(
(sponsor) => sponsor.isLeading === true
)
const regularSponsors = featuredSponsors.filter(
(sponsor) => sponsor.isLeading !== true
)
console.log("Processing lead sponsors")
// Handle lead sponsors (centered, own row)
if (leadSponsors.length > 0) {
// Get dimensions for lead sponsors
const leadSponsorInfo = leadSponsors
.map((sponsor) => {
// Ensure we have a valid logo path
const logoPath = sponsor.logo ? `./public${sponsor.logo}` : null
if (!logoPath) {
console.error(`Missing logo path for sponsor: ${sponsor.name}`)
return null
}
try {
const dimensions = sizeOf(fs.readFileSync(logoPath))
const [width, height] = getScaledImageDimensions(
dimensions.width,
dimensions.height,
true
)
return { sponsor, width, height, logoPath }
} catch (error) {
console.error(`Error processing logo for ${sponsor.name}:`, error)
return null
}
})
.filter(Boolean) // Remove any null entries
// Calculate total width including padding between lead sponsors
const totalLeadWidth = leadSponsorInfo.reduce((sum, info, index) => {
return sum + info.width + (index < leadSponsors.length - 1 ? xPadding : 0)
}, 0)
// Center the lead sponsors
let startX = (overallWidth - totalLeadWidth) / 2
// Place lead sponsors
leadSponsorInfo.forEach(({ sponsor, width, height, logoPath }) => {
images.push({
href: baseUrl + sponsor.logo,
path: logoPath,
x: startX,
y: currentY + (leadSponsorHeight - height) / 2,
height,
width,
url: sponsor.url,
isLead: true,
})
startX += width + xPadding
})
currentY += leadSponsorHeight + leadSponsorBottomPadding
}
// Now process regular sponsors
let currentX = 0
regularSponsors.forEach((sponsor) => {
// Ensure we have a valid logo path
const logoPath = sponsor.logo ? `./public${sponsor.logo}` : null
if (!logoPath) {
console.error(`Missing logo path for sponsor: ${sponsor.name}`)
return
}
try {
const dimensions = sizeOf(fs.readFileSync(logoPath))
let [width, height] = getScaledImageDimensions(
dimensions.width,
dimensions.height,
false
)
// Start new row if needed
if (currentX + width > overallWidth) {
currentX = 0
currentY += maxHeight + yPadding
}
images.push({
href: baseUrl + sponsor.logo,
path: logoPath,
x: currentX,
y: currentY + (maxHeight - height) / 2,
height,
width,
url: sponsor.url,
isLead: false,
})
currentX += width + xPadding
} catch (error) {
console.error(`Error processing logo for ${sponsor.name}:`, error)
}
})
// Calculate total height needed
const totalHeight = currentY + maxHeight + yPadding
// Generate SVG
let response = `
<svg
width="${overallWidth}"
height="${totalHeight}"
version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
>
`
images.forEach((image) => {
response += `
<a xlink:href="${image.url}" target="_blank">
<image
href="${imgToBase64(image.path)}"
x="${image.x}"
y="${image.y}"
height="${image.height}"
width="${image.width}"
/>
</a>
`
})
response += `</svg>`
return response
}
function imgToBase64(filePath) {
let extname = path.extname(filePath).slice(1) || "png"
if (extname === "svg") {
extname = "svg+xml"
}
return (
"data:image/" +
extname +
";base64," +
fs.readFileSync(filePath).toString("base64")
)
}
const getScaledImageDimensions = (width, height, isLeadSponsor = false) => {
const ratio = width / height
const maxH = isLeadSponsor ? leadSponsorHeight : maxHeight
const maxW = isLeadSponsor ? leadSponsorMaxWidth : maxWidth
let h = maxH
let w = (maxH / height) * width
if (w > maxW) {
w = maxW
h = (maxW / width) * height
}
return [w, h]
}
export async function GET({ params, request }) {
return new Response(buildResponse(), {
headers: { "Content-Type": "image/svg+xml" },
})
}