简体   繁体   English

如何从 JavaScript 读取 png 元数据?

[英]How to read png meta data from JavaScript?

My custom server embeds meta data about an image in the PNG meta data fields.我的自定义服务器在 PNG 元数据字段中嵌入了有关图像的元数据。 The image is loaded via a regular img tag.图像通过常规 img 标签加载。 I'd like to access the meta data from JavaScript - any way to achieve this?我想从 JavaScript 访问元数据 - 有什么方法可以实现吗?

If not, what are the alternatives for serving additional information for an image?如果不是,为图像提供附加信息的替代方法是什么? The images are generated on the fly and are relatively expensive to produce, so I'd like to serve the meta data and the image data in a single round trip to the server.图像是动态生成的,制作成本相对较高,所以我想在到服务器的单次往返中提供元数据和图像数据。

i had a similar task.我有一个类似的任务。 I had to write physical dimensions and additional metadata to PNG files.我必须将物理尺寸和附加元数据写入 PNG 文件。 I have found some solutions and combined it into one small library.我找到了一些解决方案并将其合并到一个小型库中。 png-metadata png-元数据

it could read PNG metadata from NodeJS Buffers, and create a new Buffers with new metadata.它可以从 NodeJS 缓冲区读取 PNG 元数据,并使用新的元数据创建一个新的缓冲区。

Here how you can read PNG metadata in the Browser:以下是如何在浏览器中读取 PNG 元数据:

        function loadFileAsBlob(url){
            return new Promise((resolve, reject) => {
                var xhr = new XMLHttpRequest();
                xhr.open('GET', url, true);
                xhr.responseType = 'blob';
                xhr.onload = function(e) {
                    if (this.status === 200) {
                        resolve(this.response);
                        // myBlob is now the blob that the object URL pointed to.
                    }else{
                        reject(this.response);
                    }
                };
                xhr.send();
            })
        };

        const blob = await loadFileAsBlob('1000ppcm.png');
        metadata = readMetadataB(blob);

A couple of solutions I can think of:我能想到的几个解决方案:

Pass the metadata as headers, use XMLHttpRequest to load the image and display it by converting the raw bytes to a data uri, as talked about here .通过元数据作为报头,使用XMLHttpRequest来加载图像,并通过将原始的字节到数据的URI,将其显示为谈到这里 Looks roughly like this:看起来大致是这样的:

var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
    var metadata = xhr.getResponseHeader("my-custom-header");
    image.src = window.URL.createObjectURL(xhr.response);
}
xhr.open('GET', 'http://whatever.com/wherever');
xhr.send();

Alternatively, write a little png parser in js (or compile libpng to javascript using something like emscripten ), and do basically the same thing as above.或者,在 js 中编写一个小的 png 解析器(或使用类似emscripten 的东西将 libpng 编译为 javascript),并执行与上述基本相同的操作。

It probably wouldn't be too hard to write actually;实际上写起来可能不会太难; since you don't care about the image data, you'd just have to write the chunk-traversing code.因为你不关心图像数据,你只需要编写块遍历代码。 Read up on how chunks are laid out here , and figure out what chunk type you're storing the metadata in. Still, don't really recommend this if you can just use headers...阅读这里的块是如何布局的,并弄清楚您将元数据存储在哪种块类型中。不过,如果您只能使用标题,请不要真正推荐这个......

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

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