Muhammad Ali Hassan
Muhammad Ali Hassan

Reputation: 962

Not getting width and height of uploading image file in blueimp

I am using blueimp to upload files. Its working fine. But I want to upload with certain width/height of images. I tried to follow this link blueimp fileupload - check the image width and height before upload file is having only this data

lastmodified:1502124635085
name:name.jgp
size:111111
type:"image/jpeg"

But width and height is missing Below is some part of code

add: function (e, data) {
                if (e.isDefaultPrevented()) {
                    return false;
                }

                console.log(data.files[0]);

                var $this = $(this),
                    that = $this.data('blueimp-fileupload') ||
                        $this.data('fileupload'),
                    options = that.options;
                data.context = that._renderUpload(data.files)
                    .data('data', data)
                    .addClass('processing');
                options.filesContainer[
                    options.prependFiles ? 'prepend' : 'append'
                ](data.context);
                that._forceReflow(data.context);
                that._transition(data.context);
                data.process(function () {
                    return $this.fileupload('process', data);
                }).always(function () {
                    data.context.each(function (index) {
                        $(this).find('.size').text(
                            that._formatFileSize(data.files[index].size)
                        );
                    }).removeClass('processing');
                    that._renderPreviews(data);
                }).done(function () {
                    data.context.find('.start').prop('disabled', false);
                    if ((that._trigger('added', e, data) !== false) &&
                            (options.autoUpload || data.autoUpload) &&
                            data.autoUpload !== false) {
                        data.submit();
                    }
                }).fail(function () {
                    if (data.files.error) {
                        data.context.each(function (index) {
                            var error = data.files[index].error;
                            if (error) {
                                $(this).find('.error').text(error);
                            }
                        });
                    }
                });
            },

is it correct location to get file data or somewhere else. Thanks

Upvotes: 0

Views: 284

Answers (1)

alba hoo
alba hoo

Reputation: 164

You can added a costomised process action into the default process queue the example below will only allow images with 1920x1080 to pass. Non-image files will be skipped. FileReader only support IE10+

$.blueimp.fileupload.prototype.options.processQueue.push({action: 'validateImage'});

// add a customised validator for image width for height, 1920x1080
$.widget('blueimp.fileupload', $.blueimp.fileupload, {
    processActions: {
      validateImage: function(data) {
        var dfd = $.Deferred();
        var file = data.files[data.index];
        if( !/(\.|\/)(gif|jpe?g|png)$/i.test(file.name)){
          dfd.resolveWith(this, [data]);
        }
        else {
          var reader = new FileReader();
          reader.readAsDataURL(file);
          reader.onload = function() {
            var image = new Image();
            image.src = reader.result;
            image.onload = function() {
              if(this.width !== 1920 || this.height !== 1080) {
                data.files.error = true
                file.error = 'Image have to be 1920x1080';
                dfd.rejectWith(this, [data]);
              }
              else {
                dfd.resolveWith(this, [data]);
              }
            }
          };
        }
        return dfd.promise();
      }
    }
});

Upvotes: 1

Related Questions