无论如何使用javascript(或类似jquery)获取图像文件大小[重复]

时间:2020-12-30 21:20:37

This question already has an answer here:

这个问题在这里已有答案:

like:

<img src="http://www.google.com/intl/en_ALL/images/srpr/logo1w.png">

i want get logo1w.png file size 7.3kb

我想得到logo1w.png文件大小7.3kb

how to implement it?

怎么实现呢?

5 个解决方案

#1


4  

Check out this : Determining image file size + dimensions via Javascript?

看看这个:通过Javascript确定图像文件大小+尺寸?

#2


3  

You could do an HTTP HEAD request for the image URL. This will return the HTTP headers, which include the content-length (a.k.a. file size). This does require an additional request to the server, which is not very efficient when the image is generated instead of being served statically.

您可以对图像URL执行HTTP HEAD请求。这将返回HTTP标头,其中包括content-length(a.k.a。文件大小)。这确实需要对服务器的附加请求,这在生成图像而不是静态服务时效率不高。

#3


3  

I don't know of any single way to consistently get the exact file size, but if you're willing to jump through some hoops, you could:

我不知道有任何一种方法可以始终如一地获得确切的文件大小,但如果你愿意跳过一些箍,你可以:

1) Estimate based on the dimensions and the file type. Because of compression techniques there's some wide variation, but you could probably run some statistics and come up with a half-decent heuristic.

1)根据尺寸和文件类型估算。由于压缩技术有一些广泛的变化,但你可能会运行一些统计数据,并提出一个半体面的启发式。

2) In IE, use the fileSize property of an image. In other browsers that support Canvas, use the technique described in this question on getting image data in JavaScript to grab the dataURL, then do the math (multiply by 3/4, since base64 is a 4/3 size increase).

2)在IE中,使用图像的fileSize属性。在其他支持Canvas的浏览器中,使用此问题中描述的技术获取JavaScript中的图像数据以获取dataURL,然后进行数学运算(乘以3/4,因为base64的大小增加了4/3)。

Edit

3) The suggestion others have given (particularly as elaborated in the question linked in Fabien Bernede's answer) about getting HTTP Headers is brilliant AND precise. Probably runs afoul of cross-domain restrictions in some cases, but still probably the most consistent and effective method.

3)其他人给出的建议(特别是在Fabien Bernede回答的问题中详细说明)关于获取HTTP标头的建议非常精确。在某些情况下可能会违反跨域限制,但仍可能是最一致和最有效的方法。

#4


1  

You cannot in javascript (sorry even magical jQuery cannot help here!). You would need a server side script to fetch the image for you and calculate the size which you could return to your page.

你不能在javascript(对不起甚至神奇的jQuery在这里无法帮助!)。您需要一个服务器端脚本来为您获取图像并计算可以返回到页面的大小。

Note - downvoters beware, the OP does not own the google domain!

注意 - downvoters要小心,OP不拥有谷歌域名!

#5


-3  

$fileSize = strlen(file_get_contents($imageSrcTag)) // <-- php

$ fileSize = strlen(file_get_contents($ imageSrcTag))// < - php

#1


4  

Check out this : Determining image file size + dimensions via Javascript?

看看这个:通过Javascript确定图像文件大小+尺寸?

#2


3  

You could do an HTTP HEAD request for the image URL. This will return the HTTP headers, which include the content-length (a.k.a. file size). This does require an additional request to the server, which is not very efficient when the image is generated instead of being served statically.

您可以对图像URL执行HTTP HEAD请求。这将返回HTTP标头,其中包括content-length(a.k.a。文件大小)。这确实需要对服务器的附加请求,这在生成图像而不是静态服务时效率不高。

#3


3  

I don't know of any single way to consistently get the exact file size, but if you're willing to jump through some hoops, you could:

我不知道有任何一种方法可以始终如一地获得确切的文件大小,但如果你愿意跳过一些箍,你可以:

1) Estimate based on the dimensions and the file type. Because of compression techniques there's some wide variation, but you could probably run some statistics and come up with a half-decent heuristic.

1)根据尺寸和文件类型估算。由于压缩技术有一些广泛的变化,但你可能会运行一些统计数据,并提出一个半体面的启发式。

2) In IE, use the fileSize property of an image. In other browsers that support Canvas, use the technique described in this question on getting image data in JavaScript to grab the dataURL, then do the math (multiply by 3/4, since base64 is a 4/3 size increase).

2)在IE中,使用图像的fileSize属性。在其他支持Canvas的浏览器中,使用此问题中描述的技术获取JavaScript中的图像数据以获取dataURL,然后进行数学运算(乘以3/4,因为base64的大小增加了4/3)。

Edit

3) The suggestion others have given (particularly as elaborated in the question linked in Fabien Bernede's answer) about getting HTTP Headers is brilliant AND precise. Probably runs afoul of cross-domain restrictions in some cases, but still probably the most consistent and effective method.

3)其他人给出的建议(特别是在Fabien Bernede回答的问题中详细说明)关于获取HTTP标头的建议非常精确。在某些情况下可能会违反跨域限制,但仍可能是最一致和最有效的方法。

#4


1  

You cannot in javascript (sorry even magical jQuery cannot help here!). You would need a server side script to fetch the image for you and calculate the size which you could return to your page.

你不能在javascript(对不起甚至神奇的jQuery在这里无法帮助!)。您需要一个服务器端脚本来为您获取图像并计算可以返回到页面的大小。

Note - downvoters beware, the OP does not own the google domain!

注意 - downvoters要小心,OP不拥有谷歌域名!

#5


-3  

$fileSize = strlen(file_get_contents($imageSrcTag)) // <-- php

$ fileSize = strlen(file_get_contents($ imageSrcTag))// < - php