bug:ReferenceError: self is not defined exif-js

bug:ReferenceError: self is not defined exif-js

查找图像(base64 图像数据)的 exif 信息时出错:

Exif.getData(path, () => {
    const tag = Exif.getTag(this, 'Orientation');
    console.log(tag);
});

ReferenceError: self is not defined

/usr/local/lib/node_modules/exif-js/exif.js:931
if ((self.Image && img instanceof self.Image)
^

谁能帮忙

您是在客户端 JS 中执行此代码段还是 Node.js?

目前exif.js是基于self对象开发的。它仅支持客户端 Javascript。

在 window 加载后注册 exif.js,附上示例片段以供参考。

window.onload=getExif;

function getExif() {
    var img1 = document.getElementById("img1");
    EXIF.getData(img1, function() {
        var make = EXIF.getTag(this, "Make");
        var model = EXIF.getTag(this, "Model");
        var makeAndModel = document.getElementById("makeAndModel");
        makeAndModel.innerHTML = `${make} ${model}`;
    });

    var img2 = document.getElementById("img2");
    EXIF.getData(img2, function() {
        var allMetaData = EXIF.getAllTags(this);
        var allMetaDataSpan = document.getElementById("allMetaDataSpan");
        allMetaDataSpan.innerHTML = JSON.stringify(allMetaData, null, "\t");
    });
}

Exif 解析器在服务器端完美运行。 https://www.npmjs.com/package/exif-parser