quack / index.html
oldfart's picture
Update index.html
793e6a7
raw
history blame
5.7 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>
<style>
/* ... (unchanged styles) */
</style>
</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 onchange="detectImageFormat()">
<p id="formatInfo">Detected Format: <span id="formatDisplay"></span></p>
<label for="targetFormat">Choose Target Format:</label>
<select id="targetFormat" required>
<option value="image/bmp">BMP</option>
<option value="image/eps">EPS</option>
<option value="image/gif">GIF</option>
<option value="image/ico">ICO</option>
<option value="image/jpeg">JPG</option>
<option value="image/png">PNG</option>
<option value="image/psd">PSD</option>
<option value="image/svg+xml">SVG</option>
<option value="image/tga">TGA</option>
<option value="image/tiff">TIFF</option>
<option value="image/webp">WebP</option>
</select>
<label for="quality">Quality (for JPEG and WebP):</label>
<input type="range" id="quality" min="1" max="100" step="1" value="85">
<label for="decreaseSize">Decrease Image Size:</label>
<input type="checkbox" id="decreaseSize">
<button type="button" onclick="convertImage()">Convert</button>
<a id="downloadLink" style="display: none" download="converted_image">Download Converted Image</a>
</form>
<script>
function detectImageFormat() {
const input = document.getElementById('imageInput');
const formatDisplay = document.getElementById('formatDisplay');
if (input.files.length > 0) {
const file = input.files[0];
formatDisplay.textContent = file.type;
}
}
function convertImage() {
const input = document.getElementById('imageInput');
const targetFormat = document.getElementById('targetFormat').value;
const quality = document.getElementById('quality').value;
const decreaseSize = document.getElementById('decreaseSize').checked;
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 blob = file.slice(0, file.size, targetFormat);
const url = URL.createObjectURL(blob);
if (decreaseSize) {
// Decrease image size
const img = new Image();
img.onload = function () {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const maxWidth = 800;
const maxHeight = 800;
let newWidth = img.width;
let newHeight = img.height;
if (img.width > maxWidth) {
newWidth = maxWidth;
newHeight = (img.height * maxWidth) / img.width;
}
if (img.height > maxHeight) {
newHeight = maxHeight;
newWidth = (img.width * maxHeight) / img.height;
}
canvas.width = newWidth;
canvas.height = newHeight;
ctx.drawImage(img, 0, 0, newWidth, newHeight);
canvas.toBlob(function (smallBlob) {
const smallUrl = URL.createObjectURL(smallBlob);
downloadLink.href = smallUrl;
downloadLink.style.display = 'block';
}, targetFormat, quality);
};
img.src = url;
} else {
if (targetFormat === 'image/jpeg' || targetFormat === 'image/webp') {
// Apply quality setting for JPEG and WebP
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 (compressedBlob) {
const compressedUrl = URL.createObjectURL(compressedBlob);
downloadLink.href = compressedUrl;
downloadLink.style.display = 'block';
}, targetFormat, quality / 100);
};
img.src = url;
} else {
// For other formats, no quality setting
downloadLink.href = url;
downloadLink.style.display = 'block';
}
}
}
}
</script>
</body>
</html>