-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
373 lines (328 loc) · 11.7 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-RB92YJ3CLH"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-RB92YJ3CLH');
</script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no" />
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation/selfie_segmentation.js"
crossorigin="anonymous"></script>
<meta name="keywords" content="passport, photo, webcam, free, privacy, browser" />
<meta name="description" content="Take your own passport photo in the browser" />
<meta property="og:title" content="DIY Passport Photo" />
<meta property="og:description" content="DIY your own passport photo in the browser" />
<title>DIY Passport Photo</title>
</head>
<style>
html,
body {
padding: 0;
margin: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.canvas_container {
margin: 0 auto;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
background-repeat: no-repeat;
background-position: center;
background-size: 100%;
}
.preview_image {
width: auto;
height: 75%;
box-shadow: 0px 1px 20px rgba(0, 0, 0, 0.3);
}
.controls {
position: absolute;
bottom: 0;
width: 100%;
padding: 10px 0 10px;
background-color: rgba(0, 0, 0, 0.3);
display: flex;
justify-content: space-around;
}
.controls button {
width: 75px;
height: 75px;
border-radius: 50%;
border: none;
box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.3);
overflow: hidden;
background-color: white;
background-repeat: no-repeat;
background-position: center;
}
.start {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/check_black_24dp.png);
}
.mirror {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/swap_horiz_black_24dp.png);
}
.capture {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/photo_camera_black_24dp.png);
}
.flip {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/flip_camera_ios_black_24dp.png);
}
.retake {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/arrow_back_black_24dp.png);
}
.download {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/file_download_black_24dp.png);
}
.print5x7 {
background-image: url(https://www.gstatic.com/images/icons/material/system_gm/2x/print_black_24dp.png);
}
.step-capture,
.step-download {
display: none;
}
</style>
<body>
<div class="canvas_container">
<div class="step-intro" style="padding: 0 10px 0 10px">
<h1>DIY Passport Photo</h1>
<h3 style="margin-top: 0; color: #333;">Printing Instructions:</h3>
<ol style="color: #555; line-height: 1.6;">
<li>Download the generated photo grid image.</li>
<li>Order 5x7 photo prints at a local photo center (e.g., CVS, Walgreens) or using a high-quality home
printer.
</li>
<li>Once printed, carefully cut out individual passport photos along the grid lines.</li>
<li>Double-check that the cut photos meet the required passport photo dimensions.</li>
</ol>
<p id="privacy-notice" style="margin-top: 20px; font-size: 14px; color: #666;">
Privacy Notice: We do not save or upload your captured photos.
All processing is done locally in your browser.
</p>
</div>
<canvas class="output_canvas step-capture"></canvas>
<img class="preview_image step-download" />
</div>
<div class="controls">
<button class="start step-intro" title="Start"></button>
<button class="mirror step-capture" title="Mirror"></button>
<button class="capture step-capture" title="Capture"></button>
<button class="flip step-capture" title="Flip"></button>
<button class="meta_info step-download" style="visibility: hidden;"></button>
<button class="retake step-download" title="Retake"></button>
<button class="print5x7 step-download" title="Print 5x7"></button>
<button class="download step-download" title="Download"></button>
</div>
<script>
const canvasContainer = document.getElementsByClassName('canvas_container')[0];
const canvasElement = document.getElementsByClassName('output_canvas')[0];
const previewElement = document.getElementsByClassName('preview_image')[0];
const startEl = document.getElementsByClassName('start')[0];
const mirrorEl = document.getElementsByClassName('mirror')[0];
const flipEl = document.getElementsByClassName('flip')[0];
const captureEl = document.getElementsByClassName('capture')[0];
const retakeEl = document.getElementsByClassName('retake')[0];
const downloadEl = document.getElementsByClassName('download')[0];
const print5x7El = document.getElementsByClassName('print5x7')[0];
const meta_info = document.getElementsByClassName('meta_info')[0];
const canvasCtx = canvasElement.getContext('2d');
const facingMode = ['user', 'environment'];
let camera, video, mirrored = false;
let facingModeIndex = 0;
let downloadFn;
let videoWidth, videoHeight, videoRatio;
let smallestWindowDimension = Math.min(window.innerWidth, window.innerHeight);
function hide(el) { el.style.display = 'none'; }
function show(el) { el.style.display = 'block'; }
startEl.addEventListener('click', () => {
start();
});
mirrorEl.addEventListener('click', () => {
if (mirrored) {
mirrored = false;
canvasElement.style.transform = 'none';
} else {
mirrored = true;
canvasElement.style.transform = 'scaleX(-1)';
}
});
flipEl.addEventListener('click', () => {
stopCamera();
facingModeIndex = (facingModeIndex + 1) % facingMode.length;
startCamera();
});
retakeEl.addEventListener('click', () => {
startCamera();
document.querySelectorAll('.step-download').forEach(hide);
document.querySelectorAll('.step-capture').forEach(show);
canvasContainer.style.backgroundImage = 'url(./passport-mask.png)';
downloadEl.removeEventListener('click', downloadFn);
});
captureEl.addEventListener('click', function () {
let seconds = 3;
let interval = 0;
captureEl.disabled = true;
const timer = document.createElement('div');
timer.style = `position:absolute;font-size:160px;color:white;top:${smallestWindowDimension / 2}px;margin-top:-80px;text-align:center;width:100%;`;
timer.textContent = seconds;
document.body.appendChild(timer);
const countdown = function () {
seconds -= 1;
timer.textContent = seconds;
if (seconds === 0) {
takePhoto();
window.clearInterval(interval);
timer.remove();
captureEl.disabled = false;
}
}
interval = window.setInterval(countdown, 1000);
});
window.onresize = () => {
smallestWindowDimension = Math.min(window.innerWidth, window.innerHeight);
resizeOutput();
}
function takePhoto() {
const outputSize = 600;
const img = new Image();
img.onload = function () {
const imageHeight = img.height;
const imageWidth = img.width;
canvasElement.height = outputSize;
canvasElement.width = outputSize;
canvasCtx.save(); // save canvas transform state before doing horizontal flip
canvasCtx.clearRect(0, 0, outputSize, outputSize); // reset canvas
canvasCtx.rect(0, 0, outputSize, outputSize); // paint white background so it's not transparent
canvasCtx.fillStyle = 'white';
canvasCtx.fill();
if (mirrored) {
canvasCtx.translate(outputSize, 0);
canvasCtx.scale(-1, 1);
}
canvasCtx.drawImage(img, (imageWidth - imageHeight) / 2, 0, imageHeight, imageHeight, 0, 0, outputSize, outputSize);
canvasCtx.restore();
const dataURL = canvasElement.toDataURL();
previewElement.src = dataURL;
resizeOutput();
document.querySelectorAll('.step-download').forEach(show);
document.querySelectorAll('.step-capture').forEach(hide);
canvasContainer.style.backgroundImage = 'none';
stopCamera();
downloadFn = function () { download(dataURL) };
downloadEl.addEventListener('click', downloadFn);
}
img.src = canvasElement.toDataURL();
}
function download(url) {
const a = document.createElement('a')
a.href = url;
a.download = 'webcam-passport-photo';
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}
function resizeOutput() {
canvasElement.style.height = `${smallestWindowDimension}px`;
canvasElement.style.width = `${smallestWindowDimension * videoRatio}px`;
canvasContainer.style.width = `${smallestWindowDimension}px`;
canvasContainer.style.height = `${smallestWindowDimension}px`;
canvasElement.height = videoHeight;
canvasElement.width = videoWidth;
}
function onResults(results) {
canvasCtx.save();
canvasCtx.clearRect(0, 0, videoWidth, videoHeight);
canvasCtx.drawImage(results.segmentationMask, 0, 0,
videoWidth, videoHeight);
// Only overwrite existing pixels.
canvasCtx.globalCompositeOperation = 'source-in';
canvasCtx.drawImage(
results.image, 0, 0, videoWidth, videoHeight);
canvasCtx.restore();
}
const selfieSegmentation = new SelfieSegmentation({
locateFile: (file) => {
return `https://cdn.jsdelivr.net/npm/@mediapipe/selfie_segmentation/${file}`;
}
});
selfieSegmentation.setOptions({
selfieMode: true,
modelSelection: 0,
});
selfieSegmentation.onResults(onResults);
function startCamera() {
const mode = facingMode[facingModeIndex];
video = document.createElement('video');
video.setAttribute('playsinline', 'true');
let firstFrame = true;
camera = new Camera(video, {
onFrame: async () => {
if (firstFrame) {
firstFrame = false;
videoWidth = video.videoWidth;
videoHeight = video.videoHeight;
videoRatio = videoWidth / videoHeight;
canvasContainer.style.backgroundImage = 'url(./passport-mask.png)';
resizeOutput();
meta_info.textContent = `${mode}, ${video.videoWidth}, ${video.videoHeight}`;
}
await selfieSegmentation.send({ image: video });
},
facingMode: mode,
width: 720,
height: 720
});
camera.start();
}
function stopCamera() {
camera && camera.stop();
video && video.remove();
}
function start() {
startCamera();
document.querySelectorAll('.step-capture').forEach(show);
document.querySelectorAll('.step-intro').forEach(hide);
}
// New Function to Create 5x7 inch Photo with Passport Size Images
print5x7El.addEventListener('click', () => {
const printCanvas = document.createElement('canvas');
const printCanvasCtx = printCanvas.getContext('2d');
// Set canvas size to 5x7 inches at 300 DPI
printCanvas.width = 1500; // 5 inches * 300 DPI
printCanvas.height = 2100; // 7 inches * 300 DPI
// Load the captured photo
const img = new Image();
img.onload = () => {
// Draw the passport photos (2x2 inches, 600x600 pixels each at 300 DPI)
const photoSize = 600;
const padding = 30; // Add some padding between photos
let x = padding, y = padding;
for (let i = 0; i < 6; i++) { // Fit 6 passport photos
if (x + photoSize + padding > printCanvas.width) {
x = padding;
y += photoSize + padding;
}
printCanvasCtx.drawImage(img, x, y, photoSize, photoSize);
x += photoSize + padding;
}
// Convert to Data URL
const dataURL = printCanvas.toDataURL();
// Trigger download
const a = document.createElement('a');
a.href = dataURL;
a.download = '5x7-passport-photo.png';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
img.src = previewElement.src;
});
</script>
</body>
</html>