nodeca / probe-image-size

Get image size without full download. Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, PSD, ICO.
MIT License
978 stars 77 forks source link

How does this library work? #43

Closed agcty closed 3 years ago

agcty commented 3 years ago

How does this library work as in what's the technical background behind it and not how you use it?

I'd love to understand how that library works behind the scenes but I'm having some difficulties understanding what's going on in the source code. Can anybody shed a little light on this? 🤗

puzrin commented 3 years ago

Sorry, due time constrains, can't participate in generic programming talks. May be, if you ask something more concrete, i could try to explain. Start with reading sources of sync method, those are trivial.

agcty commented 3 years ago

Alright, perfectly understand!

My main question is where do you actually get the information from? From the header of the remote source?

puzrin commented 3 years ago

If you ask about remote images - information is taken from remote source. It does not depend on extension, mime type and so on. Downloads starts and is immediately tested by multiple parsers in parallel https://github.com/nodeca/probe-image-size/tree/master/lib/parse_stream.

Each parser needs very few data to decide if it succeeds or fail. So, when each got decision - download is terminated and joined decision is returned.

That's a principal difference with other solutions - no need to load full source into memory.

If you need to understand, what exactly is tested - see sync variant, it does the same, but looks much more simple https://github.com/nodeca/probe-image-size/tree/master/lib/parse_sync

agcty commented 3 years ago

@puzrin ah i see! thank you very much for your explanation!