-1

I am uploading an image onto the firebase storage in a web application, but sometimes there is a possibility that image size is 6 to 10 MBs. I am unable to reduce the size of image. The below code takes the original size of image.

var fileReference = document.getElementById("fileName").files[0];
var filename = Math.random().toString(36).slice(2).concat(fileReference.name);

1 Answers1

-1

You can compress pictures with this function

function compress(source_img_obj, quality, output_format, callback) {
    var mime_type = "image/jpeg";
    if (output_format == "png") {
        mime_type = "image/png";
    }
    var image = new Image();
    image.onload = function() {
        var cvs = document.createElement('canvas');
        cvs.width = image.naturalWidth;
        cvs.height = image.naturalHeight;
        var ctx = cvs.getContext("2d").drawImage(image, 0, 0);
        var newImageData = cvs.toDataURL(mime_type, quality / 100);
        var result_image_obj = new Image();
        result_image_obj.src = newImageData;
        callback(result_image_obj);
    };
    image.src = source_img_obj
}


var fileReference = document.getElementById("fileName").files[0];
compress(window.URL.createObjectURL(fileReference), 70, "jpeg", 
function(img){
  console.log(img.src); // (Base64)
})