Update index.html
Browse files- index.html +48 -62
index.html
CHANGED
@@ -13,9 +13,10 @@
|
|
13 |
|
14 |
<form id="imageForm">
|
15 |
<label for="imageInput">Select an Image:</label>
|
16 |
-
<input type="file" id="imageInput" accept="image/*" required onchange="
|
17 |
|
18 |
<p id="formatInfo">Detected Format: <span id="formatDisplay"></span></p>
|
|
|
19 |
|
20 |
<label for="targetFormat">Choose Target Format:</label>
|
21 |
<select id="targetFormat" required>
|
@@ -32,11 +33,17 @@
|
|
32 |
<option value="image/webp">WebP</option>
|
33 |
</select>
|
34 |
|
35 |
-
<label for="
|
36 |
-
<input type="
|
37 |
|
38 |
-
<label for="
|
39 |
-
<input type="
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
<button type="button" onclick="convertImage()">Convert</button>
|
42 |
|
@@ -44,21 +51,25 @@
|
|
44 |
</form>
|
45 |
|
46 |
<script>
|
47 |
-
function
|
48 |
const input = document.getElementById('imageInput');
|
49 |
const formatDisplay = document.getElementById('formatDisplay');
|
|
|
50 |
|
51 |
if (input.files.length > 0) {
|
52 |
const file = input.files[0];
|
53 |
formatDisplay.textContent = file.type;
|
|
|
54 |
}
|
55 |
}
|
56 |
|
57 |
function convertImage() {
|
58 |
const input = document.getElementById('imageInput');
|
59 |
const targetFormat = document.getElementById('targetFormat').value;
|
60 |
-
const
|
61 |
-
const
|
|
|
|
|
62 |
const formatDisplay = document.getElementById('formatDisplay');
|
63 |
const downloadLink = document.getElementById('downloadLink');
|
64 |
|
@@ -69,68 +80,43 @@
|
|
69 |
formatDisplay.textContent = file.type;
|
70 |
|
71 |
// Convert and create a download link
|
72 |
-
const
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
// Decrease image size
|
77 |
-
const img = new Image();
|
78 |
-
img.onload = function () {
|
79 |
-
const canvas = document.createElement('canvas');
|
80 |
-
const ctx = canvas.getContext('2d');
|
81 |
|
82 |
-
|
83 |
-
|
84 |
|
85 |
-
|
86 |
-
|
|
|
87 |
|
88 |
-
if (
|
89 |
-
|
90 |
-
newHeight = (img.height *
|
91 |
}
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
newWidth = (img.width * maxHeight) / img.height;
|
96 |
-
}
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
downloadLink.href = smallUrl;
|
105 |
-
downloadLink.style.display = 'block';
|
106 |
-
}, targetFormat, quality);
|
107 |
-
};
|
108 |
-
|
109 |
-
img.src = url;
|
110 |
-
} else {
|
111 |
-
if (targetFormat === 'image/jpeg' || targetFormat === 'image/webp') {
|
112 |
-
// Apply quality setting for JPEG and WebP
|
113 |
-
const img = new Image();
|
114 |
-
img.onload = function () {
|
115 |
-
const canvas = document.createElement('canvas');
|
116 |
-
canvas.width = img.width;
|
117 |
-
canvas.height = img.height;
|
118 |
-
const ctx = canvas.getContext('2d');
|
119 |
-
ctx.drawImage(img, 0, 0);
|
120 |
-
canvas.toBlob(function (compressedBlob) {
|
121 |
-
const compressedUrl = URL.createObjectURL(compressedBlob);
|
122 |
-
downloadLink.href = compressedUrl;
|
123 |
-
downloadLink.style.display = 'block';
|
124 |
-
}, targetFormat, quality / 100);
|
125 |
-
};
|
126 |
-
|
127 |
-
img.src = url;
|
128 |
-
} else {
|
129 |
-
// For other formats, no quality setting
|
130 |
downloadLink.href = url;
|
131 |
downloadLink.style.display = 'block';
|
132 |
-
}
|
133 |
-
}
|
|
|
|
|
134 |
}
|
135 |
}
|
136 |
</script>
|
|
|
13 |
|
14 |
<form id="imageForm">
|
15 |
<label for="imageInput">Select an Image:</label>
|
16 |
+
<input type="file" id="imageInput" accept="image/*" required onchange="detectImageProperties()">
|
17 |
|
18 |
<p id="formatInfo">Detected Format: <span id="formatDisplay"></span></p>
|
19 |
+
<p id="sizeInfo">Image Size: <span id="sizeDisplay"></span></p>
|
20 |
|
21 |
<label for="targetFormat">Choose Target Format:</label>
|
22 |
<select id="targetFormat" required>
|
|
|
33 |
<option value="image/webp">WebP</option>
|
34 |
</select>
|
35 |
|
36 |
+
<label for="customWidth">Custom Width (leave empty for original):</label>
|
37 |
+
<input type="text" id="customWidth">
|
38 |
|
39 |
+
<label for="customHeight">Custom Height (leave empty for original):</label>
|
40 |
+
<input type="text" id="customHeight">
|
41 |
+
|
42 |
+
<input type="checkbox" id="keepAspectRatio">
|
43 |
+
<label for="keepAspectRatio">Keep Aspect Ratio</label>
|
44 |
+
|
45 |
+
<input type="checkbox" id="compressImage">
|
46 |
+
<label for="compressImage">Compress Image</label>
|
47 |
|
48 |
<button type="button" onclick="convertImage()">Convert</button>
|
49 |
|
|
|
51 |
</form>
|
52 |
|
53 |
<script>
|
54 |
+
function detectImageProperties() {
|
55 |
const input = document.getElementById('imageInput');
|
56 |
const formatDisplay = document.getElementById('formatDisplay');
|
57 |
+
const sizeDisplay = document.getElementById('sizeDisplay');
|
58 |
|
59 |
if (input.files.length > 0) {
|
60 |
const file = input.files[0];
|
61 |
formatDisplay.textContent = file.type;
|
62 |
+
sizeDisplay.textContent = `${file.size} bytes`;
|
63 |
}
|
64 |
}
|
65 |
|
66 |
function convertImage() {
|
67 |
const input = document.getElementById('imageInput');
|
68 |
const targetFormat = document.getElementById('targetFormat').value;
|
69 |
+
const customWidth = document.getElementById('customWidth').value;
|
70 |
+
const customHeight = document.getElementById('customHeight').value;
|
71 |
+
const keepAspectRatio = document.getElementById('keepAspectRatio').checked;
|
72 |
+
const compressImage = document.getElementById('compressImage').checked;
|
73 |
const formatDisplay = document.getElementById('formatDisplay');
|
74 |
const downloadLink = document.getElementById('downloadLink');
|
75 |
|
|
|
80 |
formatDisplay.textContent = file.type;
|
81 |
|
82 |
// Convert and create a download link
|
83 |
+
const img = new Image();
|
84 |
+
img.onload = function () {
|
85 |
+
const canvas = document.createElement('canvas');
|
86 |
+
const ctx = canvas.getContext('2d');
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
+
// Apply custom dimensions
|
89 |
+
let newWidth, newHeight;
|
90 |
|
91 |
+
if (customWidth && customHeight) {
|
92 |
+
newWidth = parseInt(customWidth, 10);
|
93 |
+
newHeight = parseInt(customHeight, 10);
|
94 |
|
95 |
+
if (keepAspectRatio) {
|
96 |
+
// Calculate new height to maintain aspect ratio
|
97 |
+
newHeight = (img.height * newWidth) / img.width;
|
98 |
}
|
99 |
+
} else {
|
100 |
+
// If custom dimensions are not provided, use original dimensions
|
101 |
+
newWidth = img.width;
|
102 |
+
newHeight = img.height;
|
103 |
+
}
|
104 |
|
105 |
+
canvas.width = newWidth;
|
106 |
+
canvas.height = newHeight;
|
|
|
|
|
107 |
|
108 |
+
// Draw the image on the canvas
|
109 |
+
ctx.drawImage(img, 0, 0, newWidth, newHeight);
|
110 |
+
|
111 |
+
// Convert and create a download link
|
112 |
+
canvas.toBlob(function (blob) {
|
113 |
+
const url = URL.createObjectURL(blob);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
downloadLink.href = url;
|
115 |
downloadLink.style.display = 'block';
|
116 |
+
}, targetFormat, compressImage ? 0.85 : 1.0); // Adjust compression quality if needed
|
117 |
+
};
|
118 |
+
|
119 |
+
img.src = URL.createObjectURL(file);
|
120 |
}
|
121 |
}
|
122 |
</script>
|