3

I use below way to get an image size from a URL:

const img = new Image();
            img.onload = () => {
                const imageSize = {
                    x: img.width,
                    y: img.height
                };
                resolve(imageSize);
            };

            img.src = imageUrl;

basically it loads the whole image and determine its size. I wonder there is any other way to get it without downloading the full image. Is there a header I can load to read the image's size?

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • 1
    I don't think that that is possible. The image dimensions are encoded as part of the image itself. – ibrahim mahrir Aug 26 '18 at 22:21
  • Though there are [some interesting ideas in this thread](https://stackoverflow.com/questions/7452896/how-to-determine-the-size-of-an-image-without-downloading-it-in-full) worth reading. – Marty Aug 26 '18 at 22:22
  • Do you have controll over the server at which the images are stored? – ibrahim mahrir Aug 26 '18 at 22:24
  • @ibrahimmahrir I don't think the answer you marked is necessarily true. It *is* possible to get the dimensions without downloading the *full* image. The solution may not be elegant or practical but it's definitely possible. – Marty Aug 26 '18 at 22:27
  • @Marty I saw the link you posted. I guess in javascript, downloading the image a 100 times would be better than to use any of those solutions. There aren't any javascript libraries that parses image formats that I know of, and if they exist they won't cover each image format and they will require much work and overhead. – ibrahim mahrir Aug 26 '18 at 22:33
  • Like I said; the solution won't be nice but saying it's not possible is false. – Marty Aug 26 '18 at 22:40
  • For jpeg you'll need the whole file for png, gif, bmp the 22 firsts bits should be enough. You can find such a parser from here https://stackoverflow.com/questions/42540903/sort-by-image-resolution-in-gallery/42649964#42649964 where we used a file input, but you can very well replace it with a xhr range request. – Kaiido Aug 26 '18 at 22:41

0 Answers0