-
Notifications
You must be signed in to change notification settings - Fork 8
/
resizecalculator.html
123 lines (117 loc) · 4.28 KB
/
resizecalculator.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Megapixel Resize Calculator</title>
<meta name="description" content="Maximum width and height calculator for Google Docs.">
<meta name="keywords" content="Google Docs, Megapixel, Resize, Calculator">
<meta property="og:title" content="Megapixel Resize Calculator">
<meta property="og:type" content="website">
<meta property="og:url" content="https://mysterypancake.github.io/Fun/html/resizecalculator">
<meta property="og:site_name" content="Megapixel Resize Calculator">
<meta property="og:description" content="Maximum width and height calculator for Google Docs.">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
font-family: system-ui, Arial, Helvetica, sans-serif;
}
div {
padding: 3px 0;
}
</style>
<script>
let widthElem;
let heightElem;
let targetElem;
let targetElem2;
let roundUpElem;
let newWidthElem;
let newHeightElem;
let megapixelDisplay;
let megapixelDisplay2;
function setup() {
widthElem = document.getElementById("widthInput");
heightElem = document.getElementById("heightInput");
targetElem = document.getElementById("targetInput");
targetElem2 = document.getElementById("targetInput2");
roundUpElem = document.getElementById("roundUpCheckbox");
newWidthElem = document.getElementById("newWidthOutput");
newHeightElem = document.getElementById("newHeightOutput");
megapixelDisplay = document.getElementById("megapixelLabel");
megapixelDisplay2 = document.getElementById("megapixelLabel2");
}
function calculate() {
const originalWidth = parseInt(widthElem.value);
const originalHeight = parseInt(heightElem.value);
const targetSize = parseInt(targetElem.value);
if (originalWidth && originalHeight && targetSize) {
const roundUp = roundUpElem.checked;
const originalArea = originalWidth * originalHeight;
megapixelDisplay.textContent = " = " + (originalArea / 1000000) + " megapixels";
const ratio = Math.sqrt(targetSize / originalArea);
const newWidth = (roundUp ? Math.ceil : Math.floor)(originalWidth * ratio);
const newHeight = (roundUp ? Math.ceil : Math.floor)(originalHeight * ratio);
megapixelDisplay2.textContent = " = " + ((newWidth * newHeight) / 1000000) + " megapixels";
newWidthElem.value = newWidth;
newHeightElem.value = newHeight;
}
}
function calculateMegapixels() {
targetElem2.value = parseInt(targetElem.value) / 1000000;
calculate();
}
function calculatePixels() {
targetElem.value = parseFloat(targetElem2.value) * 1000000;
calculate();
}
function uploadImage(elem) {
const image = elem.files[0];
if (image) {
const reader = new FileReader();
reader.readAsDataURL(image);
reader.onload = function(e) {
const image = new Image();
image.src = e.target.result;
image.onload = function() {
widthElem.value = this.width;
heightElem.value = this.height;
calculate();
};
};
}
}
</script>
</head>
<body onload="setup();">
<h1>Megapixel Resize Calculator</h1>
<p>This was made because Google Docs requires images to be smaller than 25 megapixels.</p>
<div>
<label for="widthInput">Width and height (pixels) OR </label>
<input type="file" accept="image/*" onchange="uploadImage(this);">
</div>
<div>
<input type="number" id="widthInput" oninput="calculate();">
<input type="number" id="heightInput" oninput="calculate();">
<span id="megapixelLabel"></span>
</div>
<div>
<label for="targetInput">Target area (pixels/megapixels)</label>
</div>
<div>
<input type="number" id="targetInput" value="25000000" oninput="calculateMegapixels();">
<input type="number" id="targetInput2" value="25" oninput="calculatePixels();">
</div>
<div>
<label for="newWidthOutput">New width and height (pixels)</label>
</div>
<div>
<input type="number" id="newWidthOutput" readonly>
<input type="number" id="newHeightOutput" readonly>
<span id="megapixelLabel2"></span>
</div>
<div>
<input type="checkbox" id="roundUpCheckbox">
<label for="roundUpCheckbox" oninput="calculate();">Round up</label>
</div>
</body>
</html>