Determining image file size + dimensions via Javascript?

后端 未结 11 1102
执念已碎
执念已碎 2020-11-22 07:00

As part of a web app, once images have been downloaded and rendered on a web page, I need to determine an image\'s file size (kb) and resolution within the browser context (

11条回答
  •  难免孤独
    2020-11-22 07:39

    Service workers have access to header informations, including the Content-Length header.

    Service workers are a bit complicated to understand, so I've built a small library called sw-get-headers.

    Than you need to:

    1. subscribe to the library's response event
    2. identify the image's url among all the network requests
    3. here you go, you can read the Content-Length header!

    Note that your website needs to be on HTTPS to use Service Workers, the browser needs to be compatible with Service Workers and the images must be on the same origin as your page.

提交回复
热议问题