Skip to content

Commit c4b3dd5

Browse files
committed
Encode recap images as webp instead of png
1 parent 029a8f9 commit c4b3dd5

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

assets/recap.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ document.addEventListener('DOMContentLoaded', () => {
166166
});
167167

168168
// Handle share/download button clicks
169-
document.querySelectorAll('.share-png-btn').forEach(btn => {
169+
document.querySelectorAll('.share-webp-btn').forEach(btn => {
170170
btn.addEventListener('click', async () => {
171171
const url = btn.dataset.shareUrl;
172172
const filename = btn.dataset.shareFilename;
@@ -177,11 +177,11 @@ document.addEventListener('DOMContentLoaded', () => {
177177
// Fetch the image and convert to a File object
178178
const response = await fetch(url);
179179
const blob = await response.blob();
180-
const file = new File([blob], filename, { type: 'image/png' });
180+
const file = new File([blob], filename, { type: 'image/webp' });
181181

182182
// Check if we can share this file
183183
if (navigator.canShare({ files: [file] })) {
184-
// Extract year from filename (e.g., "koshelf_2024_story.png" -> "2024")
184+
// Extract year from filename (e.g., "koshelf_2024_story.webp" -> "2024")
185185
const yearMatch = filename.match(/koshelf_(\d{4})_/);
186186
const year = yearMatch ? yearMatch[1] : new Date().getFullYear();
187187

src/share_image.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ impl ShareFormat {
4040
}
4141
}
4242

43-
/// Get the PNG filename for this format
43+
/// Get the WebP filename for this format
4444
pub fn filename(&self) -> &'static str {
4545
match self {
46-
ShareFormat::Story => "share_story.png",
47-
ShareFormat::Square => "share_square.png",
48-
ShareFormat::Banner => "share_banner.png",
46+
ShareFormat::Story => "share_story.webp",
47+
ShareFormat::Square => "share_square.webp",
48+
ShareFormat::Banner => "share_banner.webp",
4949
}
5050
}
5151

@@ -90,12 +90,12 @@ pub fn generate_share_image(
9090
// Render SVG to pixmap
9191
resvg::render(&tree, resvg::tiny_skia::Transform::default(), &mut pixmap.as_mut());
9292

93-
// Encode as PNG and write to file
94-
let png_data = pixmap.encode_png()
95-
.context("Failed to encode PNG")?;
93+
// Encode as WebP and write to file
94+
let encoder = webp::Encoder::from_rgba(pixmap.data(), width, height);
95+
let webp_data = encoder.encode(90.0); // 90% quality for good balance of size/quality
9696

97-
std::fs::write(output_path, png_data)
98-
.context("Failed to write PNG file")?;
97+
std::fs::write(output_path, &*webp_data)
98+
.context("Failed to write WebP file")?;
9999

100100
// Also generate SVG file
101101
let svg_path = output_path.with_extension("svg");

templates/recap/recap_year.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ <h2 class="hidden lg:block text-2xl font-bold text-gray-900 dark:text-white">Rec
103103
</header>
104104

105105
<!-- Share Modal -->
106-
<div id="shareModal" class="fixed inset-0 z-50 hidden items-center justify-center bg-black/50 backdrop-blur-sm p-4">
106+
<div id="shareModal" class="fixed inset-0 z-[60] hidden items-center justify-center bg-black/50 backdrop-blur-sm p-4">
107107
<div class="bg-gray-100 dark:bg-dark-900 rounded-2xl shadow-2xl max-w-md w-full max-h-[90vh] overflow-y-auto border border-gray-200/70 dark:border-dark-700/50">
108108
<!-- Modal Header -->
109109
<div class="flex items-center justify-between p-4 border-b border-gray-200/70 dark:border-dark-700/50">
@@ -131,8 +131,8 @@ <h3 id="shareModalTitle" class="text-lg font-bold text-gray-900 dark:text-white"
131131
</div>
132132
</div>
133133
<div class="flex gap-2">
134-
<button data-share-url="/recap/{{ year }}/share_story.png" data-share-filename="koshelf_{{ year }}_story.png" class="share-png-btn flex-1 px-4 py-2 text-sm font-medium text-center bg-gray-100 dark:bg-dark-700 hover:bg-purple-100 dark:hover:bg-purple-900/30 text-gray-700 dark:text-gray-200 hover:text-purple-700 dark:hover:text-purple-300 rounded-lg transition-colors border border-gray-200/70 dark:border-dark-600/50 hover:border-purple-300 dark:hover:border-purple-700/50">
135-
<span class="share-btn-text">Download PNG</span>
134+
<button data-share-url="/recap/{{ year }}/share_story.webp" data-share-filename="koshelf_{{ year }}_story.webp" class="share-webp-btn flex-1 px-4 py-2 text-sm font-medium text-center bg-gray-100 dark:bg-dark-700 hover:bg-purple-100 dark:hover:bg-purple-900/30 text-gray-700 dark:text-gray-200 hover:text-purple-700 dark:hover:text-purple-300 rounded-lg transition-colors border border-gray-200/70 dark:border-dark-600/50 hover:border-purple-300 dark:hover:border-purple-700/50">
135+
<span class="share-btn-text">Download</span>
136136
</button>
137137
<a href="/recap/{{ year }}/share_story.svg" download="koshelf_{{ year }}_story.svg" class="px-4 py-2 text-sm font-medium text-center bg-gray-100 dark:bg-dark-700 hover:bg-purple-100 dark:hover:bg-purple-900/30 text-gray-600 dark:text-gray-400 hover:text-purple-700 dark:hover:text-purple-300 rounded-lg transition-colors border border-gray-200/70 dark:border-dark-600/50 hover:border-purple-300 dark:hover:border-purple-700/50">
138138
SVG
@@ -154,8 +154,8 @@ <h3 id="shareModalTitle" class="text-lg font-bold text-gray-900 dark:text-white"
154154
</div>
155155
</div>
156156
<div class="flex gap-2">
157-
<button data-share-url="/recap/{{ year }}/share_square.png" data-share-filename="koshelf_{{ year }}_square.png" class="share-png-btn flex-1 px-4 py-2 text-sm font-medium text-center bg-gray-100 dark:bg-dark-700 hover:bg-blue-100 dark:hover:bg-blue-900/30 text-gray-700 dark:text-gray-200 hover:text-blue-700 dark:hover:text-blue-300 rounded-lg transition-colors border border-gray-200/70 dark:border-dark-600/50 hover:border-blue-300 dark:hover:border-blue-700/50">
158-
<span class="share-btn-text">Download PNG</span>
157+
<button data-share-url="/recap/{{ year }}/share_square.webp" data-share-filename="koshelf_{{ year }}_square.webp" class="share-webp-btn flex-1 px-4 py-2 text-sm font-medium text-center bg-gray-100 dark:bg-dark-700 hover:bg-blue-100 dark:hover:bg-blue-900/30 text-gray-700 dark:text-gray-200 hover:text-blue-700 dark:hover:text-blue-300 rounded-lg transition-colors border border-gray-200/70 dark:border-dark-600/50 hover:border-blue-300 dark:hover:border-blue-700/50">
158+
<span class="share-btn-text">Download</span>
159159
</button>
160160
<a href="/recap/{{ year }}/share_square.svg" download="koshelf_{{ year }}_square.svg" class="px-4 py-2 text-sm font-medium text-center bg-gray-100 dark:bg-dark-700 hover:bg-blue-100 dark:hover:bg-blue-900/30 text-gray-600 dark:text-gray-400 hover:text-blue-700 dark:hover:text-blue-300 rounded-lg transition-colors border border-gray-200/70 dark:border-dark-600/50 hover:border-blue-300 dark:hover:border-blue-700/50">
161161
SVG
@@ -177,8 +177,8 @@ <h3 id="shareModalTitle" class="text-lg font-bold text-gray-900 dark:text-white"
177177
</div>
178178
</div>
179179
<div class="flex gap-2">
180-
<button data-share-url="/recap/{{ year }}/share_banner.png" data-share-filename="koshelf_{{ year }}_banner.png" class="share-png-btn flex-1 px-4 py-2 text-sm font-medium text-center bg-gray-100 dark:bg-dark-700 hover:bg-green-100 dark:hover:bg-green-900/30 text-gray-700 dark:text-gray-200 hover:text-green-700 dark:hover:text-green-300 rounded-lg transition-colors border border-gray-200/70 dark:border-dark-600/50 hover:border-green-300 dark:hover:border-green-700/50">
181-
<span class="share-btn-text">Download PNG</span>
180+
<button data-share-url="/recap/{{ year }}/share_banner.webp" data-share-filename="koshelf_{{ year }}_banner.webp" class="share-webp-btn flex-1 px-4 py-2 text-sm font-medium text-center bg-gray-100 dark:bg-dark-700 hover:bg-green-100 dark:hover:bg-green-900/30 text-gray-700 dark:text-gray-200 hover:text-green-700 dark:hover:text-green-300 rounded-lg transition-colors border border-gray-200/70 dark:border-dark-600/50 hover:border-green-300 dark:hover:border-green-700/50">
181+
<span class="share-btn-text">Download</span>
182182
</button>
183183
<a href="/recap/{{ year }}/share_banner.svg" download="koshelf_{{ year }}_banner.svg" class="px-4 py-2 text-sm font-medium text-center bg-gray-100 dark:bg-dark-700 hover:bg-green-100 dark:hover:bg-green-900/30 text-gray-600 dark:text-gray-400 hover:text-green-700 dark:hover:text-green-300 rounded-lg transition-colors border border-gray-200/70 dark:border-dark-600/50 hover:border-green-300 dark:hover:border-green-700/50">
184184
SVG

0 commit comments

Comments
 (0)