imghdr
Library that determines the type of image contained in a file or byte stream, basically clone of the Python imghdr module.
Documentation is here.
Examples
Check a file directly:
extern crate imghdr;
fn main() {
match imghdr::open("/path/to/image.png") {
Ok(imghdr::Type::Png) => println!("Yep, it is a PNG"),
_ => println!("Nope, it is definitely not a PNG"),
}
}
Or check a byte stream:
extern crate imghdr;
fn main() {
let mut file = File::open("/path/to/image.png").unwrap();
let mut content: Vec<u8> = vec![];
file.read_to_end(&mut content).unwrap();
match imghdr::what(content.as_slice()) {
Some(imghdr::Type::Jpg) => println!("And this is a Jpeg"),
_ => println!("Can a Png, Bmp or other file format"),
}
}