ReduceSize Pro – Reduce Image Size in KB
/* — CSS SEPARATION START — */
@import url(‘https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700&display=swap’);:root {
–primary: #F43F5E; /* Rose-500 */
–bg-dark: #0F172A;
}body {
font-family: ‘Inter’, sans-serif;
transition: background-color 0.3s, color 0.3s;
}/* Dashed border animation */
.border-dash-animate {
background-image: url(“data:image/svg+xml,%3csvg width=’100%25′ height=’100%25′ xmlns=’http://www.w3.org/2000/svg’%3e%3crect width=’100%25′ height=’100%25′ fill=’none’ rx=’16’ ry=’16’ stroke=’%23F43F5EFF’ stroke-width=’2′ stroke-dasharray=’12%2c 12′ stroke-dashoffset=’0′ stroke-linecap=’square’/%3e%3c/svg%3e”);
transition: all 0.3s;
}
.dropzone-active .border-dash-animate {
background-image: url(“data:image/svg+xml,%3csvg width=’100%25′ height=’100%25′ xmlns=’http://www.w3.org/2000/svg’%3e%3crect width=’100%25′ height=’100%25′ fill=’none’ rx=’16’ ry=’16’ stroke=’%23F43F5EFF’ stroke-width=’4′ stroke-dasharray=’12%2c 12′ stroke-dashoffset=’0′ stroke-linecap=’square’/%3e%3c/svg%3e”);
background-color: rgba(244, 63, 94, 0.05);
transform: scale(1.01);
}/* Progress Bar Striping */
.progress-striped {
background-image: linear-gradient(45deg,rgba(255,255,255,.15) 25%,transparent 25%,transparent 50%,rgba(255,255,255,.15) 50%,rgba(255,255,255,.15) 75%,transparent 75%,transparent);
background-size: 1rem 1rem;
animation: progress-stripes 1s linear infinite;
}
@keyframes progress-stripes {
0% { background-position: 1rem 0; }
100% { background-position: 0 0; }
}/* Dark Mode */
.dark body {
background-color: var(–bg-dark);
color: #F9FAFB;
}
.dark .card {
background-color: #1E293B;
border-color: #334155;
}
.dark input {
background-color: #334155;
color: white;
border-color: #475569;
}
/* — CSS SEPARATION END — */
tailwind.config = {
darkMode: ‘class’,
theme: {
extend: {
colors: {
primary: ‘#F43F5E’,
}
}
}
}
Specify any target file size. We optimize compression and dimensions to meet your requirement.
Drop Images Here JPG, PNG, WEBP supported
Browse Files
Processed Files Target: 150KB
Reset
// — JAVASCRIPT SEPARATION START —// DOM
const dropzone = document.getElementById(‘dropzone’);
const fileInput = document.getElementById(‘fileInput’);
const targetInput = document.getElementById(‘target-input’);
const targetDisplay = document.getElementById(‘target-display’);
const resultsArea = document.getElementById(‘results-area’);
const resultsList = document.getElementById(‘results-list’);
const canvas = document.getElementById(‘process-canvas’);
const ctx = canvas.getContext(‘2d’);let isDarkMode = false;// — Event Listeners —
dropzone.addEventListener(‘click’, () => fileInput.click());
dropzone.addEventListener(‘dragover’, (e) => {
e.preventDefault();
dropzone.firstElementChild.nextElementSibling.classList.add(‘dropzone-active’);
});
dropzone.addEventListener(‘dragleave’, () => {
dropzone.firstElementChild.nextElementSibling.classList.remove(‘dropzone-active’);
});
dropzone.addEventListener(‘drop’, (e) => {
e.preventDefault();
dropzone.firstElementChild.nextElementSibling.classList.remove(‘dropzone-active’);
if(e.dataTransfer.files.length) handleFiles(e.dataTransfer.files);
});
fileInput.addEventListener(‘change’, (e) => {
if(e.target.files.length) handleFiles(e.target.files);
});targetInput.addEventListener(‘input’, (e) => {
targetDisplay.innerText = `${e.target.value}KB`;
});// — Functions —function setTarget(val) {
targetInput.value = val;
targetDisplay.innerText = `${val}KB`;
}function handleFiles(files) {
resultsArea.classList.remove(‘hidden’);
const targetKB = parseInt(targetInput.value) || 150;
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 = ‘card bg-white dark:bg-slate-800 p-4 rounded-xl shadow-sm border border-gray-200 dark:border-slate-700 flex flex-col sm:flex-row items-center gap-4 animate-pulse’;
div.innerHTML = `
Optimizing…
`;
resultsList.prepend(div);
}function processImage(file, targetBytes, id) {
const reader = new FileReader();
reader.onload = (e) => {
const img = new Image();
img.onload = () => {
reduceToSize(img, targetBytes).then(result => {
updateResultUI(id, file, result.blob);
}).catch(err => {
console.error(err);
document.getElementById(id).innerHTML = ‘
Error processing image.
‘;
});
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}/**
* Advanced Reduction Logic:
* 1. Tries purely quality reduction first (to keep resolution).
* 2. If max compression still exceeds target, falls back to scaling dimensions.
*/
async function reduceToSize(img, targetBytes) {
const originalW = img.width;
const originalH = img.height;
let scale = 1.0;
let quality = 0.9;
let blob = null;
let iterations = 0;
let w, h;// Try up to 20 iterations to get it right
while (iterations canvas.toBlob(r, ‘image/jpeg’, quality));// Check if success
if (blob.size 0.1) {
// Reduce quality first
// If file is HUGE compared to target, drop quality faster
if (blob.size > targetBytes * 2) quality -= 0.2;
else quality -= 0.05;
if (quality < 0.1) quality = 0.1; // Floor
} else {
// Quality is maxed out (0.1), must resize dimensions now
scale *= 0.85;
// Reset quality slightly to avoid potato looking tiny images
quality = 0.7;
}
iterations++;
}
// Return best effort
return { blob };
}function updateResultUI(id, originalFile, blob) {
const div = document.getElementById(id);
if(!div) return;
div.classList.remove('animate-pulse');const url = URL.createObjectURL(blob);
const savedBytes = originalFile.size – blob.size;
const percent = ((savedBytes / originalFile.size) * 100).toFixed(0);
const nameParts = originalFile.name.split('.');
const ext = nameParts.pop();
const name = `${nameParts.join('.')}_reduced.jpg`;div.innerHTML = `
Done
${name}
${formatBytes(originalFile.size)}
${formatBytes(blob.size)}
-${percent}%
Download
`;
}function formatBytes(bytes) {
if(bytes === 0) return ‘0 Bytes’;
const k = 1024;
const sizes = [‘Bytes’, ‘KB’, ‘MB’];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ‘ ‘ + sizes[i];
}function resetAll() {
resultsList.innerHTML = ”;
resultsArea.classList.add(‘hidden’);
fileInput.value = ”;
}function toggleDarkMode() {
isDarkMode = !isDarkMode;
document.documentElement.classList.toggle(‘dark’);
const icon = document.getElementById(‘theme-icon’);
icon.classList.toggle(‘fa-moon’, !isDarkMode);
icon.classList.toggle(‘fa-sun’, isDarkMode);
}// — JAVASCRIPT SEPARATION END —
Reduce Image Size in Kb: Overview and Usage Tips Use the Reduce Image Size in Kb utility for tasks involving digital images. Review the tool interface for supported inputs and settings. For file-based tasks, keep a copy of your original file and check the output before using it.
Steps to get started Read the supported formats or input requirements shown in the tool. Choose the available options appropriate to your task. Run the tool and review its result for accuracy and quality. Related resources Try Image Compressor or Image Resizer for another document or digital task.