<h2>Optimizador de Imágenes para Blogger</h2>
<input type="file" id="upload" accept="image/*" />
<br><br>
<canvas id="canvas" style="display:none;"></canvas>
<button onclick="downloadImage()">Descargar Imagen Optimizada</button>
<script>
const upload = document.getElementById('upload');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
upload.onchange = function (event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function (e) {
const img = new Image();
img.onload = function () {
const maxWidth = 800;
const maxHeight = 600;
let newWidth = img.width;
let newHeight = img.height;
if (newWidth > maxWidth) {
newHeight = newHeight * (maxWidth / newWidth);
newWidth = maxWidth;
}
if (newHeight > maxHeight) {
newWidth = newWidth * (maxHeight / newHeight);
newHeight = maxHeight;
}
canvas.width = newWidth;
canvas.height = newHeight;
ctx.drawImage(img, 0, 0, newWidth, newHeight);
canvas.style.display = 'block';
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
};
function downloadImage() {
const quality = 0.6;
const image = canvas.toDataURL('image/jpeg', quality).replace('image/jpeg', 'image/octet-stream');
const link = document.createElement('a');
link.download = 'imagen-optimizada.jpg';
link.href = image;
link.click();
}
</script>