← Back to home

Coding challenge: fast width/height of an image

I am trying to find out the width and height of an image (a URL) transferring as few bytes as possible. This coding challenge has two separate parts:

  • Finding the cheapest way to retrieve width and height.
  • Verifying that the solution is indeed cheap.

The first part is not that interesting since it’s a fairly well documented problem. A bit too documented, actually, since I found several solutions:

This last solution looks pretty efficient from a network perspective but I’m worried it might leave out a few corner cases in image handling. The first two solutions are probably battle tested and very robust but I have no idea how effective they are from a network standpoint (their Javadoc doesn’t describe the specifics of how they operate).

So… Do you have a better solution? And whether you do or don’t, how would you assess how many bytes these implementations transfer?

Comments (2)

Frank De prins

http://th-schwarz.github.io/JII/

PJ

Off the cuff fast approach that (sadly) may not be supported by many web servers (though if you have control of the web server you can fix that, of course): use file extension to detect the image type then use the HTTP 'Content-Range' request to pull just the bytes that encode the image size.