-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrontend_example.html
More file actions
80 lines (71 loc) · 3 KB
/
frontend_example.html
File metadata and controls
80 lines (71 loc) · 3 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload Image</title>
</head>
<body>
<h1>Upload Image</h1>
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" id="fileInput" name="file">
<input type="submit" value="Upload">
</form>
<script>
document.getElementById('uploadForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the default form submission
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
// Set canvas dimensions to 480p
const maxWidth = 854;
const maxHeight = 480;
let width = img.width;
let height = img.height;
// Calculate the new dimensions
if (width > height) {
if (width > maxWidth) {
height = Math.round((height *= maxWidth / width));
width = maxWidth;
}
} else {
if (height > maxHeight) {
width = Math.round((width *= maxHeight / height));
height = maxHeight;
}
}
canvas.width = width;
canvas.height = height;
// Draw the image on the canvas
ctx.drawImage(img, 0, 0, width, height);
// Convert the canvas to a Blob
canvas.toBlob(function(blob) {
const formData = new FormData();
formData.append('file', blob, file.name);
// Upload the resized image
fetch('http://localhost:5000/upload', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(result => {
console.log('Success:', result);
})
.catch(error => {
console.error('Error:', error);
});
}, file.type);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
});
</script>
</body>
</html>