Search code examples
javascriptjqueryfile-uploadclient-sideimage-resizing

Jquery-fileupload get original file size before resizing


I am using blueimp/jQuery-File-Upload plugin to upload and resize the images on client side on my website.

I need a way to get origonal uploaded image dimensions before it is resized to my provided values in imageMaxWidth and imageMaxHeight options.

Any help or guidance will be great, or i can edit the library and add this hook if it is not already there to return original file data.

Thanks in advance.


Solution

  • Just discovered something without adding custom hooks:

    Just override add method and calculate image dimensions from file object as such:

    add: function(e, data) {
    var img = new Image;
    var _URL = window.URL || window.webkitURL;
    img.onload = function() {
      sizes =
        width: this.width
        height: this.height
    };
    img.src = _URL.createObjectURL(data.files[0]);
    
    //below code is to let the library further process file e.g resize etc
    var $this = $(this);
    return data.process(function() {
      return $this.fileupload('process', data);
    }).done(function() {
      return data.submit();
    });
    

    }

    And here you have it, in the sizes hash.

    Cheers!