nguyenvanduocit
9/17/2015 - 9:07 AM

Plupload resolution_limit

Plupload resolution_limit

plupload.addFileFilter('resolution_limit', function(limit, file, cb) {
        var self = this;
        var reader = new FileReader();
        var image  = new Image();
        var blodFile = file.getNative();
        reader.readAsDataURL(blodFile);
        reader.onload = function(_file) {
            image.src    = _file.target.result;
            image.onload = function() {
                var width = this.width;
                var height = this.height;
                var isValid = true;
                var errorMesage = '';
                if(typeof limit.min != 'undefined'){
                    if(typeof limit.min.width != 'undefined'){
                        if(width < limit.min.width){
                            isValid = false;
                            errorMesage = plupload.translate('The width of the image must be greater than ') + limit.min.width +'px';
                        }
                    }
                    if(typeof limit.min.height != 'undefined'){
                        if(height < limit.min.height){
                            isValid = false;
                            errorMesage = plupload.translate('The height of the image must be greater than ') + limit.min.height +'px';
                        }
                    }
                }

                if(typeof  limit.max != 'undefined'){
                    if(typeof limit.max.width != 'undefined'){
                        if(width > limit.max.width){
                            isValid = false;
                            errorMesage = plupload.translate('The height of the image must be less than ') + limit.max.width +'px';
                        }
                    }
                    if(typeof limit.max.height != 'undefined'){
                        if(height > limit.max.height){
                            isValid = false;
                            errorMesage = plupload.translate('The height of the image must be less than ') + limit.max.height +'px';
                        }
                    }
                }

                if (!isValid) {
                    self.trigger('Error', {
                        code : plupload.IMAGE_DIMENSIONS_ERROR,
                        message : errorMesage,
                        file : file
                    });

                }
                cb(isValid);
            };

            image.onerror= function(e) {
                console.log(e);
                self.trigger('Error', {
                    code : plupload.IMAGE_FORMAT_ERROR,
                    message : plupload.translate('Image format error.'),
                    file : file
                });
                cb(false);
            };
        };
    });