Reduce Image Size in KB
function compressImage(){
let file=document.getElementById("upload").files[0]; let quality=document.getElementById("quality").value/100;
let reader=new FileReader();
reader.readAsDataURL(file);
reader.onload=function(event){
let img=new Image();
img.src=event.target.result;
img.onload=function(){
let canvas=document.createElement("canvas"); let ctx=canvas.getContext("2d");
canvas.width=img.width; canvas.height=img.height;
ctx.drawImage(img,0,0);
let compressed=canvas.toDataURL("image/jpeg",quality);
let download=document.getElementById("download");
download.href=compressed; download.download="compressed-image.jpg"; download.innerHTML="Download Compressed Image"; download.style.display="block";
}
}
}