2

直指点点。当用户使用 plupload 上传图像时,我想设置图像的宽度和高度限制。

Letsay:如果宽度:1000px 高度:1000px 否则您必须上传至少宽度:1000px 和高度:1000px 的图像

// $(".form").validator();
$(function() {
    if($("#uploader").length > 0) {
        var uploader = new plupload.Uploader({
            runtimes : 'html5,flash,silverlight',
            browse_button : 'pickfile',
            container : 'uploader',
            max_file_size : '10mb',
            url : 'design.php?do=upload&ajax=1',
            multiple_queues: false,
            file_data_name: 'design',
            flash_swf_url : www + '/js/plupload.flash.swf',
            silverlight_xap_url : www + '/js/plupload.silverlight.xap',
            filters : [
                {title : "Image files", extensions : "jpg,gif,png,jpeg,bmp"}
            ]

        });

        $('#uploadfiles').click(function(e) {
            if($("#uploader select[name=category]").val() == "") {
                $("#uploader select[name=category]").next('.error-required').show();
                return false;
            }

            uploader.start();
            e.preventDefault();
        });

        uploader.init();

那么,这可能吗?

4

4 回答 4

3

您可以轻松地为 plupload 编写过滤器。

这是最小所需宽度的过滤器。将以下代码添加到您的脚本中。(只是复制)

plupload.addFileFilter('min_width', function(maxwidth, file, cb) {
    var self = this, img = new o.Image();

    function finalize(result) {
        // cleanup
        img.destroy();
        img = null;

       // if rule has been violated in one way or another, trigger an error
        if (!result) {
            self.trigger('Error', {
                code : plupload.IMAGE_DIMENSIONS_ERROR,
                message : "Image width should be more than " + maxwidth  + " pixels.",
                file : file
            });
     }
        cb(result);
    }
    img.onload = function() {
        // check if resolution cap is not exceeded
        finalize(img.width >= maxwidth);
    };
    img.onerror = function() {
        finalize(false);
    };
    img.load(file.getSource());
});

并将此过滤器添加到您的上传脚本中。

filters : {
            min_width: 700,
        },
于 2015-09-23T08:22:11.800 回答
1

Plupload 本身不支持此功能(尽管已被请求)。这可能有几个原因,首先是因为您无法在 IE 中上传之前获取图像尺寸(您可以在其他一些浏览器中),其次,这适用于使用 HTML4/5 的某些浏览器方法,我不确定 Flash/Silverlight 等方法是否也能够可靠地确定尺寸。

如果您对有限的浏览器感到满意,只有 HTML4/5 方法,您应该挂钩“FilesAdded”事件,例如

uploader.bind('FilesAdded', function(up, files) {
  //Get src of each file, create image, remove from file list if too big
});
于 2012-12-30T15:43:59.090 回答
0

我最近想做同样的事情,并且能够按照 Thom 建议的方式实现它。不过,他的局限性是正确的;如果你想添加它,它只适用于现代浏览器,而不适用于 flash 或 silverlight 运行时。这不是一个大问题,因为我的非 html5 用户在上传后只会收到错误,而不是之前。

我初始化了一个总图像计数变量来跟踪放置在页面上的图像。也可以存储我们在阅读完所有照片后要删除的照片。

var total_image_count = 0;
var files_to_remove = [];

然后我用 FileReader() 读取待处理的文件,将它们放在页面上,并获取它们的宽度

init:{
        FilesAdded: function(up, files) {
           if (uploader.runtime == "html5"){
              files = jQuery("#"+uploader.id+"_html5")[0].files
              console.log(files);
              for (i in files){

                 //create image tag for the file we are uploading
                 jQuery("<img />").attr("id","image-"+total_image_count).appendTo("#upload-container");

                 reader_arr[total_image_count] = new FileReader();

                 //create listener to place the data in the newly created 
                 //image tag when FileReader fully loads image.
                 reader_arr[total_image_count].onload = function(total_image_count) {
                    return function(e){
                       var img = $("#image-"+total_image_count);
                       img.attr('src', e.target.result);
                       if ($(img)[0].naturalWidth < 1000){

                          files_to_remove.push(files[i]); //remove them after we finish reading in all the files
                          //This is where you would append an error to the DOM if you wanted.
                          console.log("Error. File must be at least 1000px");
                       }
                    }
                 }(total_image_count);

                 reader_arr[total_image_count].readAsDataURL(files[i]);

                 total_image_count++;
              }
              for (i in files_to_remove){
                 uploader.removeFile(files_to_remove[i]);
              }
           }
        }
     }

附带说明一下,无论如何我都想显示图像缩略图,所以这种方法对我很有用。我还没有弄清楚如何在不先将其附加到 DOM 的情况下获取图像的宽度。

资料来源:

上传前缩略图: https ://stackoverflow.com/a/4459419/686440

访问图像的自然宽度: https ://stackoverflow.com/a/1093414/686440

于 2013-02-28T20:45:50.817 回答
0

要在不使用缩略图的情况下访问宽度和高度,您可以执行以下操作:

uploader.bind('FilesAdded', function(up, files) {
    files = jQuery("#"+uploader.id+"_html5").get(0).files;
    jQuery.each(files, function(i, file) {
        var reader = new FileReader();
        reader.onload = (function(e) { 
            var image = new Image();
            image.src = e.target.result;

                image.onload = function() {
                    // access image size here using this.width and this.height
                }
            };

        });

        reader.readAsDataURL(file);
    }
}
于 2013-06-06T09:29:41.117 回答