MegaToKilo – MB to KB Converter & Compressor
/* — CSS SEPARATION START — */
@import url(‘https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&family=Fira+Code:wght@400&display=swap’);:root {
–primary: #6366F1; /* Indigo-500 */
–accent: #8B5CF6; /* Violet-500 */
–bg-dark: #0F172A;
}body {
font-family: ‘Inter’, sans-serif;
transition: background-color 0.3s, color 0.3s;
}/* Animated Gradient Text */
.text-gradient {
background: linear-gradient(to right, #6366F1, #8B5CF6, #EC4899);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-size: 200% 200%;
animation: gradient 3s ease infinite;
}@keyframes gradient {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}/* Tab Styles */
.tab-btn {
transition: all 0.2s;
position: relative;
}
.tab-btn.active {
background-color: white;
color: #4F46E5;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
.dark .tab-btn.active {
background-color: #1E293B;
color: #818CF8;
}/* Calculator Display */
.calc-display {
font-family: ‘Fira Code’, monospace;
}/* Loading Spinner */
.loader {
border: 3px solid #e0e7ff;
border-radius: 50%;
border-top: 3px solid #6366F1;
width: 24px;
height: 24px;
animation: spin 1s linear infinite;
}@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}/* Dark Mode */
.dark body {
background-color: var(–bg-dark);
color: #F9FAFB;
}
.dark .card {
background-color: #1E293B;
border-color: #334155;
}
.dark input, .dark select {
background-color: #334155;
color: white;
border-color: #475569;
}
/* — CSS SEPARATION END — */
tailwind.config = {
darkMode: ‘class’,
theme: {
extend: {
colors: {
primary: ‘#6366F1’,
}
}
}
}
Switch between converting image file sizes (Compression) or calculating data units (Math) instantly.
Image Compressor
Unit Calculator
Upload Large MB Images We’ll shrink them to your target KB size.
Select Files
Converted Images
Clear All
Data Unit Converter
From
Kilobyte (KB)
Megabyte (MB)
Gigabyte (GB)
Terabyte (TB)
Kilobit (Kb)
Megabit (Mb)
To
1024
Kilobyte (KB)
Megabyte (MB)
Gigabyte (GB)
Terabyte (TB)
Kilobit (Kb)
Megabit (Mb)
Formula Used:
1 Megabyte (MB) = 1024 Kilobytes (KB) (Binary/IEC Standard)
// — JAVASCRIPT SEPARATION START —// — Tab Logic —
function switchTab(tab) {
// UI Buttons
document.getElementById(‘btn-compressor’).className = `tab-btn px-6 py-2.5 rounded-lg font-bold text-sm flex items-center ${tab === ‘compressor’ ? ‘active’ : ‘text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white’}`;
document.getElementById(‘btn-calculator’).className = `tab-btn px-6 py-2.5 rounded-lg font-bold text-sm flex items-center ${tab === ‘calculator’ ? ‘active’ : ‘text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white’}`;
// Content
if(tab === ‘compressor’) {
document.getElementById(‘tab-content-compressor’).classList.remove(‘hidden’);
document.getElementById(‘tab-content-calculator’).classList.add(‘hidden’);
} else {
document.getElementById(‘tab-content-compressor’).classList.add(‘hidden’);
document.getElementById(‘tab-content-calculator’).classList.remove(‘hidden’);
calculateUnit(); // Calc immediately on switch
}
}// — COMPRESSOR LOGIC —
const dropzone = document.getElementById(‘dropzone’);
const fileInput = document.getElementById(‘fileInput’);
const resultsArea = document.getElementById(‘results-area’);
const resultsList = document.getElementById(‘results-list’);
const targetInput = document.getElementById(‘target-kb’);
const canvas = document.getElementById(‘process-canvas’);
const ctx = canvas.getContext(‘2d’);let isDarkMode = false;// Drag & Drop
dropzone.addEventListener(‘click’, () => fileInput.click());
dropzone.addEventListener(‘dragover’, (e) => { e.preventDefault(); dropzone.classList.add(‘border-indigo-500’); });
dropzone.addEventListener(‘dragleave’, () => dropzone.classList.remove(‘border-indigo-500’));
dropzone.addEventListener(‘drop’, (e) => {
e.preventDefault();
dropzone.classList.remove(‘border-indigo-500’);
if(e.dataTransfer.files.length) handleFiles(e.dataTransfer.files);
});
fileInput.addEventListener(‘change’, (e) => {
if(e.target.files.length) handleFiles(e.target.files);
});function setPreset(val) {
targetInput.value = val;
}function handleFiles(files) {
resultsArea.classList.remove(‘hidden’);
const targetKB = parseInt(targetInput.value) || 200;
const targetBytes = targetKB * 1024;Array.from(files).forEach(file => {
if (!file.type.match(‘image.*’)) return;
const id = Math.random().toString(36).substr(2, 9);
createResultUI(id, file);
processImage(file, targetBytes, id);
});
}function createResultUI(id, file) {
const div = document.createElement(‘div’);
div.id = id;
div.className = ‘bg-white dark:bg-slate-800 p-4 rounded-xl shadow-sm border border-gray-200 dark:border-slate-700 flex items-center gap-4’;
div.innerHTML = `
${file.name}
Original: ${formatBytes(file.size)}
Converting…
`;
resultsList.prepend(div);
}function processImage(file, targetBytes, id) {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
compressLoop(img, targetBytes).then(blob => {
updateResultUI(id, file, blob);
}).catch(err => {
console.error(err);
document.getElementById(id).innerHTML = ‘
Error converting file
‘;
});
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}async function compressLoop(img, targetBytes) {
const w = img.width;
const h = img.height;
canvas.width = w;
canvas.height = h;
ctx.fillStyle = ‘#FFFFFF’;
ctx.fillRect(0, 0, w, h);
ctx.drawImage(img, 0, 0, w, h);let min = 0, max = 1, quality = 0.9, blob = null, attempts = 0;
// 1. Quality Reduction
while(attempts canvas.toBlob(r, ‘image/jpeg’, quality));
if(Math.abs(blob.size – targetBytes) < targetBytes * 0.1 || (blob.size 0.95)) break;
if(blob.size > targetBytes) max = quality;
else min = quality;
quality = (min + max) / 2;
attempts++;
}// 2. Dimension Reduction if still huge (MB -> KB scenario)
if(blob.size > targetBytes) {
let scale = 0.9;
while(blob.size > targetBytes && scale > 0.1) {
const newW = Math.floor(w * scale);
const newH = Math.floor(h * scale);
canvas.width = newW;
canvas.height = newH;
ctx.fillStyle = ‘#FFFFFF’;
ctx.fillRect(0, 0, newW, newH);
ctx.drawImage(img, 0, 0, newW, newH);
blob = await new Promise(r => canvas.toBlob(r, ‘image/jpeg’, 0.8));
scale -= 0.15; // Aggressive steps for MB->KB
}
}
return blob;
}function updateResultUI(id, original, blob) {
const div = document.getElementById(id);
if(!div) return;
const url = URL.createObjectURL(blob);
const saved = ((original.size – blob.size)/original.size * 100).toFixed(0);
div.innerHTML = `
${original.name}
${formatBytes(original.size)}
${formatBytes(blob.size)}
-${saved}%
Download
`;
}function formatBytes(bytes) {
if(bytes === 0) return ‘0 Bytes’;
const k = 1024;
const sizes = [‘Bytes’, ‘KB’, ‘MB’, ‘GB’];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ‘ ‘ + sizes[i];
}function resetCompressor() {
resultsList.innerHTML = ”;
resultsArea.classList.add(‘hidden’);
fileInput.value = ”;
}// — CALCULATOR LOGIC —
const calcInput = document.getElementById(‘calc-input’);
const calcFrom = document.getElementById(‘calc-unit-from’);
const calcTo = document.getElementById(‘calc-unit-to’);
const calcOutput = document.getElementById(‘calc-output’);[calcInput, calcFrom, calcTo].forEach(el => el.addEventListener(‘input’, calculateUnit));function calculateUnit() {
const val = parseFloat(calcInput.value);
if(isNaN(val)) {
calcOutput.innerText = “—“;
return;
}const from = calcFrom.value;
const to = calcTo.value;// Base unit: Bits
const factors = {
‘b’: 1,
‘Kb’: 1000,
‘Mb’: 1000 * 1000,
‘Gb’: 1000 * 1000 * 1000,
‘Tb’: 1000 * 1000 * 1000 * 1000,
‘B’: 8,
‘KB’: 8 * 1024,
‘MB’: 8 * 1024 * 1024,
‘GB’: 8 * 1024 * 1024 * 1024,
‘TB’: 8 * 1024 * 1024 * 1024 * 1024
};const bits = val * factors[from];
const result = bits / factors[to];// Formatting
let display = result;
if(result % 1 !== 0) display = result.toFixed(6).replace(/\.?0+$/, “”); // Trim zeros
calcOutput.innerText = display;
}// — Dark Mode —
function toggleDarkMode() {
const isDark = document.documentElement.classList.toggle(‘dark’);
const icon = document.getElementById(‘theme-icon’);
icon.className = isDark ? “fa-solid fa-sun text-yellow-400” : “fa-solid fa-moon text-gray-600”;
}// — JAVASCRIPT SEPARATION END —
Using MB to KB Converter The MB to KB Converter utility is designed for digital utility tasks. Check the on-page controls for available formats, limits and options. Always review the output for accuracy before sharing or publishing it.
Practical tips Keep your original files or source information as a backup. Choose suitable options using the controls provided by this tool. Check the generated result and retry with different settings if needed. Explore related tools Meta Tags Analyzer and Word to PDF are available for related tasks.