forked from simonw/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestimonial-card-generator.html
More file actions
462 lines (414 loc) · 16.6 KB
/
testimonial-card-generator.html
File metadata and controls
462 lines (414 loc) · 16.6 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Testimonial Card Image Generator</title>
<link rel="stylesheet" href="styles.css">
<style>
:root {
--card-width: 720px;
--card-padding: 32px;
--image-width: 1200px;
--image-height: 630px;
}
.page-shell {
max-width: min(100%, calc(var(--image-width) + 320px));
}
.layout {
display: flex;
flex-direction: column;
gap: clamp(1rem, 3vw, 1.5rem);
align-items: stretch;
}
.field {
display: grid;
gap: 0.35rem;
margin-bottom: 1rem;
}
.inputs {
display: grid;
gap: 1rem;
}
textarea {
min-height: 140px;
}
.helper {
font-size: 0.95rem;
color: var(--tx-2);
margin: 0;
}
.highlight-controls {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
align-items: center;
margin-top: 0.5rem;
}
.chip {
background: var(--bg);
border: 1px solid var(--ui-2);
border-radius: 999px;
padding: 0.35rem 0.7rem;
font-size: 0.95rem;
display: inline-flex;
gap: 0.35rem;
align-items: center;
}
.chip button {
background: transparent;
border: none;
color: inherit;
cursor: pointer;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
align-items: center;
margin-top: 0.35rem;
}
.ghost {
background: transparent;
border-color: var(--ui-3);
color: var(--tx);
}
.ghost:hover,
.ghost:focus-visible {
background: var(--bg-2);
}
.preview-wrapper {
display: grid;
gap: 0.75rem;
align-items: start;
min-inline-size: 0;
}
.preview-shell {
width: 100%;
overflow-x: auto;
padding: 0.25rem;
}
.image-space {
width: var(--image-width);
height: var(--image-height);
background: var(--bg-2);
border: 1px solid var(--ui-2);
border-radius: var(--radius-lg);
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
margin-inline: auto;
}
.render-surface {
width: var(--image-width);
height: var(--image-height);
display: flex;
justify-content: center;
align-items: center;
background: transparent;
padding: clamp(1.5rem, 3vw, 2rem);
}
.card {
width: clamp(280px, 75vw, var(--card-width));
max-width: min(100%, 820px);
background: linear-gradient(135deg, var(--bg), var(--bg-2));
padding: var(--card-padding);
border-radius: var(--radius-lg);
}
.card blockquote {
font-size: 1.15rem;
line-height: 1.6;
margin: 0 0 18px;
color: var(--tx);
}
.card blockquote mark {
background: rgba(160, 47, 111, 0.16);
color: inherit;
padding: 2px 4px;
border-radius: 6px;
}
.person {
display: flex;
gap: 0.75rem;
align-items: center;
}
.avatar {
width: 64px;
height: 64px;
border-radius: var(--radius-md);
object-fit: cover;
border: 2px solid var(--ui-2);
background: var(--bg-2);
}
.name {
font-weight: 700;
}
.affiliation {
color: var(--tx-2);
}
.status {
min-height: 24px;
font-size: 0.95rem;
color: var(--tx-2);
}
.paste-zone {
border: 1px dashed var(--ui-3);
border-radius: var(--radius-md);
padding: 0.75rem;
background: var(--bg);
display: grid;
gap: 0.35rem;
align-items: center;
cursor: pointer;
}
.paste-zone:focus-visible {
outline: 2px solid var(--link);
outline-offset: 2px;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html-to-image/1.11.13/html-to-image.min.js"
integrity="sha512-iZ2ORl595Wx6miw+GuadDet4WQbdSWS3JLMoNfY8cRGoEFy6oT3G9IbcrBeL6AfkgpA51ETt/faX6yLV+/gFJg=="
crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
<div class="page-shell">
<header class="page-header">
<a class="site-link" href="https://tools.mathspp.com/" aria-label="Back to tools.mathspp.com">←
tools.mathspp.com</a>
<h1>Testimonial Card Image Generator</h1>
<p class="lead">Create a polished testimonial card from text, name, affiliation, and avatar, then download
it as a social-ready PNG.</p>
</header>
<main class="content-flow">
<section class="layout">
<div class="surface inputs">
<div class="field">
<label for="testimonial">Testimonial text</label>
<textarea id="testimonial" placeholder="Paste the testimonial..."></textarea>
<p class="helper">Select text in the testimonial field and click “Highlight selection” to
emphasize key phrases.</p>
<div class="highlight-controls" id="highlightChips"></div>
<div class="actions">
<button type="button" id="highlightButton">Highlight selection</button>
<button type="button" class="ghost" id="clearHighlights">Clear highlights</button>
</div>
</div>
<div class="field">
<label for="name">Person name</label>
<input type="text" id="name" placeholder="Name">
</div>
<div class="field">
<label for="affiliation">Affiliation</label>
<input type="text" id="affiliation" placeholder="Company, role, or project">
</div>
<div class="field">
<label for="photoUpload">Photo</label>
<p class="helper">Upload an image file or paste one from your clipboard (Ctrl/Cmd + V). Images
stay on your device.</p>
<input type="file" id="photoUpload" accept="image/*">
<div class="paste-zone" id="pasteZone" tabindex="0" role="button"
aria-label="Click or focus and paste an image for the avatar">
<strong>Click to choose a file, then paste an image here.</strong>
<p class="helper">Pasting will replace the current photo preview.</p>
</div>
</div>
</div>
<div class="surface preview-wrapper" aria-live="polite">
<div class="status" id="status"></div>
<div class="preview-shell">
<div class="image-space">
<div class="render-surface" id="renderSurface">
<div class="card" id="card">
<blockquote id="quote">Add your testimonial to see a preview.</blockquote>
<div class="person">
<img class="avatar" id="avatar" src="" alt="Avatar preview" crossorigin="anonymous" referrerpolicy="no-referrer">
<div>
<div class="name" id="previewName">Name</div>
<div class="affiliation" id="previewAffiliation">Affiliation</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="actions">
<button type="button" id="download">Download image</button>
</div>
</div>
</section>
</main>
<footer class="page-footer">
<p>Built with ❤️, 🤖, and 🐍, by <a href="https://mathspp.com/">Rodrigo Girão Serrão</a></p>
</footer>
</div>
<script>
const testimonialInput = document.getElementById('testimonial');
const nameInput = document.getElementById('name');
const affiliationInput = document.getElementById('affiliation');
const photoUploadInput = document.getElementById('photoUpload');
const pasteZone = document.getElementById('pasteZone');
const quote = document.getElementById('quote');
const previewName = document.getElementById('previewName');
const previewAffiliation = document.getElementById('previewAffiliation');
const avatar = document.getElementById('avatar');
const status = document.getElementById('status');
const highlightButton = document.getElementById('highlightButton');
const clearHighlightsButton = document.getElementById('clearHighlights');
const highlightChips = document.getElementById('highlightChips');
const downloadButton = document.getElementById('download');
const renderSurface = document.getElementById('renderSurface');
const placeholderImage = 'https://placehold.co/128x128?text=Photo';
let highlightPhrases = [];
let uploadedPhotoUrl = '';
let objectUrl = '';
const escapeHtml = (str) => str.replace(/[&<>"']/g, (match) => ({
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
}[match]));
const escapeRegex = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
function applyHighlights(text) {
if (!text) return '';
let safe = escapeHtml(text);
for (const phrase of highlightPhrases) {
if (!phrase) continue;
const pattern = new RegExp(escapeRegex(phrase), 'gi');
safe = safe.replace(pattern, (match) => `<mark>${match}</mark>`);
}
return safe;
}
function renderPreview() {
const testimonialText = testimonialInput.value.trim();
quote.innerHTML = testimonialText ? applyHighlights(testimonialText) : 'Add your testimonial to see a preview.';
previewName.textContent = nameInput.value.trim() || 'Name';
previewAffiliation.textContent = affiliationInput.value.trim() || 'Affiliation';
const photo = uploadedPhotoUrl;
avatar.src = photo || placeholderImage;
avatar.alt = photo ? `${previewName.textContent} photo` : 'Placeholder avatar';
status.textContent = '';
}
function updateChips() {
highlightChips.innerHTML = '';
if (!highlightPhrases.length) return;
for (const phrase of highlightPhrases) {
const chip = document.createElement('span');
chip.className = 'chip';
chip.textContent = phrase;
const remove = document.createElement('button');
remove.setAttribute('type', 'button');
remove.textContent = '✕';
remove.addEventListener('click', () => {
highlightPhrases = highlightPhrases.filter((p) => p !== phrase);
renderPreview();
updateChips();
});
chip.appendChild(remove);
highlightChips.appendChild(chip);
}
}
function addHighlightFromSelection() {
const start = testimonialInput.selectionStart;
const end = testimonialInput.selectionEnd;
if (start === end) {
status.textContent = 'Select text in the testimonial to highlight it.';
return;
}
const selected = testimonialInput.value.substring(start, end).trim();
if (!selected) {
status.textContent = 'Please select some text to highlight.';
return;
}
if (!highlightPhrases.includes(selected)) {
highlightPhrases.push(selected);
updateChips();
renderPreview();
}
status.textContent = `Highlighted: "${selected}"`;
}
function slugify(text) {
return text.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/(^-|-$)/g, '') || 'person-name';
}
const fetchOptions = {
mode: 'cors',
referrerPolicy: 'no-referrer',
headers: {
Accept: 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
},
};
async function downloadImage() {
status.textContent = 'Rendering image...';
downloadButton.disabled = true;
try {
const dataUrl = await htmlToImage.toPng(renderSurface, {
pixelRatio: 2,
backgroundColor: 'rgba(0,0,0,0)',
fetchRequestInit: fetchOptions,
});
const link = document.createElement('a');
link.href = dataUrl;
const filename = `testimonial-${slugify(nameInput.value.trim() || 'person-name')}.png`;
link.download = filename;
link.click();
status.textContent = `Downloaded ${filename}`;
} catch (error) {
console.error(error);
status.textContent = 'Could not generate the image. Try again.';
} finally {
downloadButton.disabled = false;
}
}
function setPhotoFromFile(file, source) {
if (!file) return;
if (!file.type.startsWith('image/')) {
status.textContent = 'Please choose an image file.';
return;
}
if (objectUrl) {
URL.revokeObjectURL(objectUrl);
}
objectUrl = URL.createObjectURL(file);
uploadedPhotoUrl = objectUrl;
renderPreview();
status.textContent = source;
}
function handlePaste(event) {
const files = event.clipboardData?.files || [];
const imageFile = Array.from(files).find((file) => file.type.startsWith('image/'));
if (!imageFile) return;
event.preventDefault();
setPhotoFromFile(imageFile, 'Photo added from pasted image.');
}
testimonialInput.addEventListener('input', renderPreview);
nameInput.addEventListener('input', renderPreview);
affiliationInput.addEventListener('input', renderPreview);
highlightButton.addEventListener('click', addHighlightFromSelection);
clearHighlightsButton.addEventListener('click', () => {
highlightPhrases = [];
renderPreview();
updateChips();
status.textContent = 'Highlights cleared.';
});
downloadButton.addEventListener('click', downloadImage);
photoUploadInput.addEventListener('change', (event) => {
const [file] = event.target.files;
if (!file) return;
setPhotoFromFile(file, 'Photo added from file upload.');
});
pasteZone.addEventListener('click', () => photoUploadInput.click());
pasteZone.addEventListener('keydown', (event) => {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
photoUploadInput.click();
}
});
pasteZone.addEventListener('paste', handlePaste);
document.addEventListener('paste', handlePaste);
renderPreview();
</script>
</body>
</html>