简体   繁体   English

JavaScript 中来自数据 URI 的图像文件大小

[英]Image file size from data URI in JavaScript

I am not sure it's even possible but - can I get the image file size from data URI?我不确定这是否可能,但是 - 我可以从数据 URI 中获取图像文件大小吗?

For example, let's say there is an IMG element where src goes:例如,假设有一个src所在的 IMG 元素:

src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...

Based on the src , can I get the image file size by using plain JavaScript?基于src ,我可以使用纯 JavaScript 获取图像文件大小吗? (without server request) (没有服务器请求)

If you want file size, simply decode your base64 string and check the length.如果您想要文件大小,只需解码您的 base64 字符串并检查长度。

 var src ="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/// yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"; var base64str = src.substr(22); var decoded = atob(base64str); console.log("FileSize: " + decoded.length);

If you're okay with a (very good) estimate, the file size is 75% of the size of the base64 string.如果您接受(非常好的)估计,则文件大小是 base64 字符串大小的 75%。 The true size is no larger than this estimate, and, at most, two bytes smaller.真实大小不大于此估计值,最多小两个字节。

If you want to write one line and be done with it, use atob() and check the length, as in the other answers.如果您想写一行并完成它,请使用atob()并检查长度,就像在其他答案中一样。

If you want an exact answer with maximum performance (in the case of gigantic files or millions of files or both), use the estimate but account for the padding to get the exact size:如果您想要具有最高性能的准确答案(在巨大文件或数百万个文件或两者的情况下),请使用估计值但考虑填充以获得确切大小:

let base64Length = src.length - (src.indexOf(',') + 1);
let padding = (src.charAt(src.length - 2) === '=') ? 2 : ((src.charAt(src.length - 1) === '=') ? 1 : 0);
let fileSize = base64Length * 0.75 - padding;

This avoids parsing the entire string, and is entirely overkill unless you're hunting for microoptimizations or are short on memory.这避免了解析整个字符串,并且完全是矫枉过正,除非您正在寻找微优化或内存不足。

Your best option is to calculate the length of the base64 string itself.您最好的选择是计算 base64 字符串本身的长度。

What is a base64 length in bytes? 什么是 base64 长度(以字节为单位)?

You have to convert the base64 string to a normal string using atob() and then check it length, it will return a value that you can say is close to the actual size of the image.您必须使用atob()将 base64 字符串转换为普通字符串,然后检查它的长度,它将返回一个您可以说接近图像实际大小的值。 Also you don't need the data:image/jpeg;base64, part from the data URI to check the size.此外,您不需要数据 URI 中的data:image/jpeg;base64,部分来检查大小。

This is a universal solution for all types of base64 strings based on Daniel Trans's code .这是基于Daniel Trans 代码的所有类型 base64 字符串的通用解决方案。

var src ="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP/// yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";

var base64str = src.split('base64,')[1];
var decoded = atob(base64str);

console.log("FileSize: " + decoded.length);

The other solutions make use of atob, which has now been deprecated.其他解决方案使用了现在已被弃用的 atob。 Here is an up-to-date example, using Buffer instead.这是一个使用 Buffer 代替的最新示例。

const src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD...";
const base64str = src.split('base64,')[1]; //remove the image type metadata.
const imageFile = Buffer.from(base64str, 'base64'); //encode image into bytes
console.log('FileSize: ' + imageFile.length);

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

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