<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
<title>Image Resizer 50KB</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.image-resizer-tool {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
text-align: center;
max-width: 600px;
width: 100%;
}
h2 {
margin-bottom: 20px;
color: #39e75f; /* Match the vivid green theme */
}
input[type=”file”] {
margin-bottom: 20px;
}
button {
padding: 10px 20px;
background-color: #0073aa;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #005177;
}
#preview img, #result img {
max-width: 100%;
height: auto;
margin-top: 20px;
border: 1px solid #ddd;
border-radius: 4px;
}
/* Vivid Green Download Button */
.download-btn {
display: inline-block;
margin-top: 20px;
padding: 12px 30px;
background: linear-gradient(145deg, #39e75f, #1abc9c); /* Bright green to teal */
color: white;
font-size: 16px;
font-weight: bold;
text-transform: uppercase;
text-decoration: none;
border: none;
border-radius: 25px;
cursor: pointer;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease, box-shadow 0.3s ease, background 0.3s ease, border 0.3s ease;
}
.download-btn:hover {
transform: translateY(-3px);
box-shadow: 0 6px 10px rgba(0, 0, 0, 0.2), 0 0 15px rgba(57, 231, 95, 0.8); /* Add glowing effect */
background: linear-gradient(145deg, #1abc9c, #39e75f); /* Reverse gradient on hover */
}
.download-btn:active {
transform: translateY(0);
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div class=”image-resizer-tool”>
<!– Updated Title –>
<h2>Image Resizer 50KB</h2>
<!– Image Upload and Preview –>
<form id=”resizeImageForm”>
<label for=”imageUpload”>Upload Image:</label>
<input type=”file” id=”imageUpload” accept=”image/*” required>
<div id=”preview”>
<!– The preview of the uploaded image will appear here –>
</div>
<button type=”button” id=”resizeButton”>Resize Image to 50KB</button>
</form>
<!– Resized Image Result –>
<div id=”result”>
<!– The resized image will appear here –>
</div>
<!– Vivid Green Download Button –>
<a id=”download-btn” style=”display: none;” download=”resized-image.jpg”>
<button class=”download-btn”>Download Resized Image</button>
</a>
</div>
<script>
document.getElementById(‘imageUpload’).addEventListener(‘change’, function(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(e) {
// Display the preview image
const previewDiv = document.getElementById(‘preview’);
previewDiv.innerHTML = `<img src=”${e.target.result}” alt=”Preview Image”>`;
};
reader.readAsDataURL(file);
} else {
// Clear the preview if no file is selected
document.getElementById(‘preview’).innerHTML = ”;
}
});
document.getElementById(‘resizeButton’).addEventListener(‘click’, function() {
const fileInput = document.getElementById(‘imageUpload’);
if (!fileInput.files.length) {
alert(‘Please upload an image first.’);
return;
}
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = function(event) {
const img = new Image();
img.src = event.target.result;
img.onload = function() {
// Start with a high-quality compression
let quality = 0.9;
let canvas = document.createElement(‘canvas’);
let ctx = canvas.getContext(‘2d’);
// Function to resize and compress the image
function resizeAndCompress(image, quality) {
const maxWidth = 1200; // Maximum width
const maxHeight = 1200; // Maximum height
let width = image.width;
let height = image.height;
// Calculate new dimensions while maintaining aspect ratio
if (width > height) {
if (width > maxWidth) {
height *= maxWidth / width;
width = maxWidth;
}
} else {
if (height > maxHeight) {
width *= maxHeight / height;
height = maxHeight;
}
}
canvas.width = width;
canvas.height = height;
// Draw the image onto the canvas
ctx.drawImage(image, 0, 0, width, height);
// Convert the canvas content to a data URL with the specified quality
return canvas.toDataURL(‘image/jpeg’, quality);
}
// Recursive function to reduce quality until the file size is under 50KB
function compressToTargetSize(dataUrl, quality) {
return new Promise((resolve, reject) => {
const imgBlob = dataURItoBlob(dataUrl);
if (imgBlob.size <= 50 * 1024) {
resolve(dataUrl); // If the image is under 50KB, resolve
} else {
// Reduce quality and try again
quality -= 0.1;
if (quality <= 0) {
reject(‘Unable to compress image to 50KB.’);
} else {
const newDataUrl = resizeAndCompress(img, quality);
compressToTargetSize(newDataUrl, quality).then(resolve).catch(reject);
}
}
});
}
// Helper function to convert Data URI to Blob
function dataURItoBlob(dataURI) {
const byteString = atob(dataURI.split(‘,’)[1]);
const mimeString = dataURI.split(‘,’)[0].split(‘:’)[1].split(‘;’)[0];
const arrayBuffer = new ArrayBuffer(byteString.length);
const uint8Array = new Uint8Array(arrayBuffer);
for (let i = 0; i < byteString.length; i++) {
uint8Array[i] = byteString.charCodeAt(i);
}
return new Blob([arrayBuffer], { type: mimeString });
}
// Start the compression process
const initialDataUrl = resizeAndCompress(img, quality);
compressToTargetSize(initialDataUrl, quality)
.then(finalDataUrl => {
// Display the resized image
const resultDiv = document.getElementById(‘result’);
resultDiv.innerHTML = `<img src=”${finalDataUrl}” alt=”Resized Image”>`;
// Show the download button and set the download link
const downloadBtn = document.getElementById(‘download-btn’);
downloadBtn.href = finalDataUrl;
downloadBtn.style.display = ‘inline-block’;
})
.catch(error => {
alert(error);
});
};
};
// Read the uploaded file as a data URL
reader.readAsDataURL(file);
});
</script>
</body>
</html>