quack1 / index.html
oldfart's picture
Update index.html
c91e80e
raw
history blame
2.49 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Format Converter</title>
</head>
<body>
<h1>Image Format Converter</h1>
<form id="imageForm">
<label for="imageInput">Select an Image:</label>
<input type="file" id="imageInput" accept="image/*" required>
<p id="formatInfo">Detected Format: <span id="formatDisplay"></span></p>
<label for="targetFormat">Choose Target Format:</label>
<select id="targetFormat" required>
<option value="image/jpeg">JPEG</option>
<option value="image/png">PNG</option>
<!-- Add more format options as needed -->
</select>
<button type="button" onclick="convertImage()">Convert</button>
<a id="downloadLink" style="display: none" download="converted_image">Download Converted Image</a>
</form>
<script>
function convertImage() {
const input = document.getElementById('imageInput');
const targetFormat = document.getElementById('targetFormat').value;
const formatDisplay = document.getElementById('formatDisplay');
const downloadLink = document.getElementById('downloadLink');
if (input.files.length > 0) {
const file = input.files[0];
// Display detected format
formatDisplay.textContent = file.type;
// Convert and create a download link
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
canvas.toBlob(function (blob) {
const url = URL.createObjectURL(blob);
downloadLink.href = url;
downloadLink.style.display = 'block';
}, targetFormat);
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
}
</script>
</body>
</html>