-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
320 lines (293 loc) · 10.9 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
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]> <html class="no-js"> <!--<![endif]-->
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Create your 4x6 Passport Photo</title>
<meta name="description" content="Convert your cropped Passport Photo into a 4x6 Photo format to cheaply print!" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="" />
<style>
body {
font-family: Open Sans, Arial;
color: #454545;
font-size: 16px;
margin: 2em auto;
min-width: 1080px;
max-width: 1080px;
padding: 1em;
line-height: 1.4;
text-align: left;
background-color: #fff;
}
.header-action {
display: flex;
flex-direction: row;
gap: 3em;
}
#header-image {
justify-content: right;
align-content: center;
}
.responsive {
height: auto;
width: auto;
max-width: 100%;
}
#input-photo {
margin-bottom: 2em;
}
</style>
</head>
<body>
<!--[if lt IE 7]>
<p class="browsehappy">
You are using an <strong>outdated</strong> browser. Please
<a href="#">upgrade your browser</a> to improve your experience.
</p>
<![endif]-->
<section style="margin-bottom: 2em;">
<div class="header-action">
<div>
<h1 style="font-size: 36px;">US Passport 4x6 Photo Template</h1>
<p>Create a 4x6 photo from your square photo!</p>
<strong>Upload your photo (JPEG)</strong>
<input type="file" accept="image/jpeg" id="input-photo" />
<button id="orientation-btn" onclick="toggleOrientation()">
Toggle Orientation
</button>
<button id="download" onclick="downloadCanvas()">
Download your 4x6 Passport Photo
</button>
</div>
<div><img id="header-image" class="responsive" src="LandingPageBenefit.png" alt="test photo"></div>
</div>
</section>
<strong>This tool does not upload your photos to a server. Everything is done on your local device.</strong>
<p>Visit the <a href="https://travel.state.gov/content/travel/en/passports/how-apply/photos.html" target="_blank">US
Department of State Passport Photos page</a> for their photo policies.</p>
<p>
Here is a link to their photo cropping tool to get a square passport photo: </br>
<a href="https://tsg.phototool.state.gov/photo" target="_blank">U.S. Passport Photos - United States Department of
State</a>
</p>
<p>Source code can be found here: </br>
<a href="https://github.com/sozodennis01/free-passport-photos-4x6" target="_blank">Github Repo</a>
</p>
<aside>If this is cool, connect with me on <a href="https://www.linkedin.com/in/sozodennis">LinkedIn</a> and visit my
<a href="https://www.sozodennis.com">website!</br>
</a>
</aside>
<p>Any bugs/issues/requests, contact me on LinkedIn!</p>
<script>
console.log("Script started: " + length);
const inputElement = document.getElementById("input-photo");
inputElement.addEventListener("change", handleFiles, false);
var fileUploaded = false;
//If true, portrait 4x6. Else, landscape 4x6
/* https://www.w3schools.com/js/js_comments.asp
FUN FACT: iOS (and i assume other mobile devices) use the orientation variable name so you can't flip the 4x6 orientation in Safari.
Its a read-only field... yeah i went crazy trying to debug why setting orientation = true... didn't do anything.
Changing this to a different name now. woohoo! 1/09/2022
*/
var template_orientation = false;
//Global Photo URL
var PhotoLiveURL = null;
var isSquare = false;
function handleFiles() {
let fileList = this.files; /* now you can work with the file list */
if (fileList.length != 0 && fileList[0].type != "image/jpeg") {
fileUploaded = false;
console.error("Not a JPEG file.")
alert("You can only upload a JPEG file at this time.");
return;
} else if (fileList.length == 0) {
fileUploaded = false;
console.error("No file uploaded, don't run the rest of the script.");
return;
}
console.log("File uploaded.")
//If a new file/photo is uploaded, reset photos and canvas, then release ObjectURL
if (fileUploaded) {
let uploadedTitle = document.getElementById("upload-title");
let photo = document.getElementById("uploaded_photo");
let currentCanvas = document.getElementById("photo_canvas");
let seperator = document.getElementById("seperatorID");
uploadedTitle.remove();
photo.remove();
currentCanvas.remove();
seperator.remove();
delete photo;
delete currentCanvas;
delete seperator;
//Must do memory management for object URLs. They'll be released when the user leaves the website.
URL.revokeObjectURL(PhotoLiveURL);
}
//Only one photo can be uploaded, thus only check 1st element.
PhotoLiveURL = URL.createObjectURL(fileList[0]);
let checkImg = new Image();
checkImg.src = PhotoLiveURL;
checkImg.onload = function () {
if (checkImg.width != checkImg.height) {
isSquare = false;
fileUploaded = false;
console.error("Not a square photo, need to resize.");
alert("Not a square photo!\nPlease use the photo cropping tool below provided by the US State Department.")
}
else {
isSquare = true;
//Resize if needed.
resize(checkImg);
if (isSquare) {
console.log("Square photo, show the file.")
showFile(fileList);
fileUploaded = true;
}
}
};
}
function resize(resizeMe) {
console.log("Resizing photo...")
if (resizeMe == null || resizeMe.src == null) {
return;
}
//Thank you https://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
let canvas = document.createElement("canvas");
// Feel free to change
let MAX_WIDTH = 1080;
let MAX_HEIGHT = 1080;
let width = resizeMe.width;
let height = resizeMe.height;
if (width > height) {
if (width > MAX_WIDTH) {
height *= MAX_WIDTH / width;
width = MAX_WIDTH;
}
} else {
if (height > MAX_HEIGHT) {
width *= MAX_HEIGHT / height;
height = MAX_HEIGHT;
}
}
canvas.width = width;
canvas.height = height;
let ctx_temp = canvas.getContext("2d");
ctx_temp.drawImage(resizeMe, 0, 0, width, height);
let resize = canvas.toDataURL("image/jpeg", 1.0);
PhotoLiveURL = resize;
}
function showFile(fileList) {
console.log("Showing uploaded photo")
const title = document.createElement("h1");
title.innerHTML = "Uploaded File: ";
title.id = "upload-title";
document.body.appendChild(title);
let photo_html = new Image();
photo_html.src = PhotoLiveURL;
photo_html.setAttribute("id", "uploaded_photo");
photo_html.setAttribute("style", "display: block; align-content: center; margin-bottom: 1em;");
photo_html.classList.add("responsive");
photo_html.onload = function () {
create4x6_portrait(photo_html);
};
document.body.appendChild(photo_html);
template_orientation = true;
//Add line break to seperate uploaded photo and 4x6 canvas
const seperator = document.createElement("hr");
seperator.setAttribute("id", "seperatorID");
seperator.setAttribute("style", "margin: 2em");
document.body.appendChild(seperator);
}
function toggleOrientation() {
if (fileUploaded) {
let photo = document.getElementById("uploaded_photo");
let currentCanvas = document.getElementById("photo_canvas");
currentCanvas.remove();
delete currentCanvas;
if (template_orientation) {
create4x6_landscape(photo);
template_orientation = false;
} else {
create4x6_portrait(photo);
template_orientation = true;
}
} else {
console.error("No photo uploaded?")
alert("Please upload a photo first 😅");
}
}
function create4x6_portrait(photo_html) {
console.log("Creating portrait")
let canvas = document.createElement("canvas");
canvas.setAttribute("id", "photo_canvas");
canvas.setAttribute("width", photo_html.width * 2);
canvas.setAttribute("height", photo_html.height * 3);
canvas.classList.add("responsive");
document.body.appendChild(canvas);
let html_canvas = document
.getElementById("photo_canvas")
.getContext("2d");
html_canvas.drawImage(photo_html, 0, 0);
html_canvas.drawImage(photo_html, photo_html.clientWidth, 0);
html_canvas.drawImage(photo_html, 0, photo_html.clientWidth);
html_canvas.drawImage(
photo_html,
photo_html.clientWidth,
photo_html.clientWidth
);
html_canvas.drawImage(photo_html, 0, photo_html.clientWidth * 2);
html_canvas.drawImage(
photo_html,
photo_html.clientWidth,
photo_html.clientWidth * 2
);
console.log("Created portrait 4x6 photo.");
}
function create4x6_landscape(photo_html) {
console.log("Creating landscape")
let canvas = document.createElement("canvas");
canvas.setAttribute("id", "photo_canvas");
canvas.setAttribute("width", photo_html.height * 3);
canvas.setAttribute("height", photo_html.width * 2);
canvas.classList.add("responsive");
document.body.appendChild(canvas);
let html_canvas = document
.getElementById("photo_canvas")
.getContext("2d");
html_canvas.drawImage(photo_html, 0, 0);
html_canvas.drawImage(photo_html, photo_html.clientWidth, 0);
html_canvas.drawImage(photo_html, photo_html.clientWidth * 2, 0);
html_canvas.drawImage(photo_html, 0, photo_html.clientWidth);
html_canvas.drawImage(
photo_html,
photo_html.clientWidth,
photo_html.clientWidth
);
html_canvas.drawImage(
photo_html,
photo_html.clientWidth * 2,
photo_html.clientWidth
);
console.log("Created landscape 4x6 photo.");
}
function downloadCanvas() {
console.log("Downloading 4x6")
if (fileUploaded) {
let html_canvas = document.getElementById("photo_canvas");
let export_photo = html_canvas.toDataURL("image/jpeg", 0.8);
let file = document.createElement("a");
file.href = export_photo;
file.download = "4x6PassportPhoto";
file.click();
} else {
console.error("No photo uploaded?")
alert("Please upload a photo first 😅");
}
}
</script>
</body>
</html>