imageinfo
Fast Rust library to read image size and format from header bytes without decoding.
Repository Health
Technical Analysis
imageinfo is a Rust library that reads an image’s dimensions and format from its header bytes, without loading or decoding the full image. Instead of trusting the file extension, it infers the format by inspecting header magic and structure, and supports a wide range of formats including AVIF, BMP, GIF, HEIC, ICO, JPEG, JPEG 2000, KTX, PNG, PSD, QOI, TGA, TIFF, and WebP. It is a rewrite of the C++ imageinfo project.
What You Get
- Image width, height, format, and MIME type read from header bytes alone
- Format detection by content inspection rather than file extension
- Support for many formats: AVIF, BMP, GIF, HEIC, ICO, JPEG, JPEG 2000, KTX, PNG, PSD, QOI, TGA, TIFF, WebP, and more
- Constructors from a file path, an in-memory buffer, or a custom reader
Common Use Cases
- Validating uploaded image dimensions and type before processing or storage
- Quickly indexing image metadata across large media libraries
- Rejecting mislabeled or oversized images without decoding them
Under The Hood
Architecture — The crate reads a small prefix of the file and dispatches to per-format detectors. lib.rs exposes the ImageInfo type and its constructors; read_interface.rs and raw_buffer.rs abstract over the input source (file, buffer, or reader) so detection logic never assumes a full in-memory image; defs.rs holds shared definitions; and the formats/ module contains one detector per supported format that parses just enough header bytes to resolve size and type.
Tech Stack — Rust 2018 edition. The runtime dependency footprint is intentionally tiny — serde (with derive) for the result types — reflecting the goal of a light, fast metadata reader. A main.rs provides a small demonstration binary alongside the library.
Code Quality — The repo includes an images_test.rs suite and a bundled images/ fixture set (with some fixtures borrowed from the image-size project) to validate detection across formats. A lefthook config wires up git hooks, and both English and Chinese READMEs are maintained.
API Design — The API is a handful of constructors (from_file_path, buffer, reader) returning an ImageInfo whose fields — ext, full_ext, size, mimetype, entry_sizes — are self-explanatory. Reading only headers means callers get results without the cost or ceremony of a full decode.