Quickly probe the size of various image formats without reading the entire file.
The goal of this crate is to be able to read the dimensions of a supported image without loading unnecessary data, and without pulling in more dependencies. Most reads only require 16 bytes or less, and more complex formats take advantage of skipping junk data.
Add the following to your Cargo.toml:
[dependencies]
imagesize = "0.10"
- BMP
- GIF
- HEIC / HEIF
- ICO*
- JPEG
- JPEG XL
- PNG
- PSD / PSB
- TIFF
- WEBP
If you have a format you think should be added, feel free to create an issue.
*ICO files can contain multiple images, imagesize
will give the dimensions of the largest one.
let (width, height) = match size("example.webp") {
Ok(dim) => (dim.width, dim.height),
Err(why) => println!("Error getting dimensions: {:?}", why)
}
Where magic_partial_download
is a function that downloads a specified amount of bytes from a given url.
let data: Vec<u8> = magic_partial_download("http://example.com/example.jpg", 0x200);
let (width, height) = match blob_size(&data) {
Ok(dim) => (dim.width, dim.height),
Err(why) => println!("Error getting dimensions: {:?}", why)
}