首页 > 解决方案 > 在全局变量中存储图像尺寸

问题描述

所以使用 papa parse,我将我的文件解析为本地 CSV 文件。因此,当我将更新的对象打印到控制台时,我让对象返回我根据某些条件应用的更改值。我的问题是获取图像尺寸:宽度和高度。

  var w;
    var h;
    let testString = "s2345232";
    if (/^[s]\d+$/.test(testString) == true) {
                    url = baseUrl + testString + suffix;
                    getMeta(url, function (width, height) {
                        w = width;
                        h = height; 
                        console.log(w, h); //works
                    });  
                }
    console.log(w, h); //doesn't work
    //Here is the function to retrieve the image data
    function getMeta(url, callback) {
        var img = new Image();
        img.src = url;
        img.addEventListener("load", function () {
            callback(this.naturalWidth, this.naturalHeight);
    });
 };

标签: javascriptjquerypapaparse

解决方案


推荐阅读