简体   繁体   English

jQuery File Upload-获取上传图像的大小

[英]jQuery File Upload - get size of uploaded images

I am using the jQuery File Upload to upload product images. 我正在使用jQuery File Upload上传产品图像。 There are different "types" of images for each product (different size/dimensions). 每种产品的图像有不同的“类型”(不同的尺寸/尺寸)。

I have altered the upload template so that a dropdown of possible image types is shown for each file added. 我已经更改了上传模板,以便为每个添加的文件显示可能的图像类型的下拉列表。 Before clicking upload, the user must pick from the dropdown to indicate what image type the file being uploaded is. 在单击上载之前,用户必须从下拉菜单中进行选择,以指示要上载的文件的图像类型。

Some image types are an exact size, some just have a minimum width. 有些图像类型是确切的尺寸,有些只是具有最小宽度。 What I want to do is compare the actual image size to the type chosen to validate they are indicating the correct type. 我要做的是将实际图像大小与所选类型进行比较,以验证它们是否指示正确的类型。

I'm attempting to do this via the "fileuploadsubmit" callback where I have already added a check to make sure an option is selected in the dropdown. 我正在尝试通过“ fileuploadsubmit”回调进行此操作,在该回调中我已经添加了检查以确保在下拉列表中选择了一个选项。

$('#fileupload').bind('fileuploadsubmit', function (e, data) {
    $('#uploading_note').removeClass("alert-danger").html("");
    var inputs = data.context.find(':input');
    if (inputs.filter(function () {
        return !this.value && $(this).prop('required');
    }).first().focus().length) {
        data.context.addClass("alert-danger");
        data.context.find('.file_msg').html("Image Type is required!"); 
    data.context.find('button').prop('disabled', false);
    return false;
}
    var fixed_dimension = 0;
    var wt = 0;
    var ht = 0;
    var image_size = getImageSize(data,function(width, height) {
        wt = width;
        ht = height;
        console.log (wt+'x'+ht);
        return [wt,ht];
    }); 
   ........... 

This is the getImageSize function. 这是getImageSize函数。

    function getImageSize(data,callback){
    var img = new Image();
    img.src = _URL.createObjectURL(data.files[0]);
            img.onload = function () {
                var w = this.width;
                var h = this.height;
                console.log(w+'-getImageSize-'+h);
                callback (w,h);
            }; 

}

The console.log() in both output correctly but I can't get these numbers outside the functions to use them. 两者中的console.log()都能正确输出,但是我无法在使用函数的函数之外获取这些数字。 I understand it has to do with the functions be asynchronous but I can't figure out how to get around it. 我知道这与异步功能有关,但我不知道如何解决它。 Or maybe there is a better jQuery File Upload built in option I'm missing. 或者,也许我没有更好的内置jQuery File Upload选项。

I understand the asynchronous function issue, this one was attempt at finding a solution to validating the image size. 我了解异步函数问题,这是试图找到一种验证图像大小的解决方案。 I have also attempted to do this in the PHP script. 我也尝试在PHP脚本中执行此操作。 It works in that I can prevent the upload of an image but the user experience is poor. 它可以防止上载图像,但是用户体验很差,因此可以正常工作。 So I am still looking for a solution as to how to do image size validation using jQuery File Upload. 因此,我仍在寻找有关如何使用jQuery File Upload进行图像大小验证的解决方案。

Below is an update version of the code. 下面是代码的更新版本。

$('#fileupload').bind('fileuploadsubmit', function (e, data) {
    console.log(data);

    $('#uploading_note').removeClass("alert-danger").html("");
    var inputs = data.context.find(':input');
    if (inputs.filter(function () {
        return !this.value && $(this).prop('required');
    }).first().focus().length) {
        console.log(data.context);
        data.context.addClass("alert-danger");
        data.context.find('.file_msg').html("Image Type is required!"); 
        //$('#uploading_note').addClass("alert-danger").html("Image Type is required!");
    data.context.find('button').prop('disabled', false);
    return false;
}

    var fixed_dimension = 0;
    var wt = 0;
    var ht = 0;
    var image_size = getImageSize(data,function(width, height) {
        wt = width;
        ht = height;
        console.log(wt+'-getImageSize-function'+ht);
       switch (inputs[0].value){
        case "16x9_background":
        case "16x9":
            var req_width = 1920;
            var req_height = 1080;
            fixed_dimension = 1;
            break;
        case "3x4":
            var req_width = 1600;
            var req_height = 1200;
            fixed_dimension = 1;
            break;
        case "hires":
            var req_width = 1400;
            var req_height = 0;
            break;
        case "lowres":
            var req_width = 200;
            var req_height = 0;
            break;
    }
    if (fixed_dimension){
        if (wt != req_width || ht != req_height){
            data.context.addClass("alert-danger");
            data.context.find('.file_msg').html("Image dimensions must be exactly "+req_width+"x"+req_height+". This image is "+wt+"x"+ht+". Image not uploaded.");
            return false;
        } 
    } else {
        if (wt < req_width){
           data.context.addClass("alert-danger"); 
           data.context.find('.file_msg').html("Image dimensions wrong!! Width must be "+req_width+", not "+wt+". Image not uploaded.");
            console.log(wt+'x'+ht);
            return false;
        }
    }    
    d = inputs.serializeArray();
    d.push({ name: "folder", value: $('#barcode').val() });
    d.push({ name: "stock_num", value: $('#stock_num').val() });
    data.formData = d;
    console.log(wt+"xxxx"+ht);
    data.submit();  // SUMBIT THE FORM
    }); 

    console.log(wt+"xxxx"+ht);
    console.log("imagesize:"+image_size);
    return false;  //STOP THE DEFAULT SUBMIT
}); 

What I ended up doing was removing the actual form submit button and replace it with a button (not type submit) that calls the validation code. 我最终要做的是删除实际的表单提交按钮,然后将其替换为调用验证代码的按钮(而不是类型提交)。 If it passes, the form is then submitted normally (via data.submit()), if not it returns the error. 如果通过,则正常提交表单(通过data.submit()),否则返回错误。 I had to remove the file level submit buttons (or I found it easier to do that). 我不得不删除文件级提交按钮(或者我发现这样做更容易)。

    $('#fileupload').fileupload({
    url: 'process.php',
    previewThumbnail: true,
    sequentialUploads: true,
    acceptFileTypes: /(\.|\/)(jpe?g|pdf|zip)$/i,
    maxChunkSize: 10000000,
    add: function (e, data) {
        $('#start_upload_button').click(function () {
          getImageSize3(data).then(function(dimensions) {
        image validation... return false if any errors

    data.submit() // submit the form
    }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM