Crates.io | imghdr |
lib.rs | imghdr |
version | 0.7.0 |
source | src |
created_at | 2016-07-17 06:55:07.931022 |
updated_at | 2019-05-08 20:42:09.077916 |
description | Library that determines the type of image contained in a file or byte stream. |
homepage | |
repository | https://github.com/svartalf/rust-imghdr |
max_upload_size | |
id | 5691 |
size | 265,989 |
Library that determines the type of image contained in a file or byte stream, basically clone of the Python imghdr module.
Check the file directly:
# extern crate imghdr;
# fn main() {
match imghdr::from_file("./tests/images/example.png") {
Ok(Some(imghdr::Type::Png)) => println!("Yep, it is a PNG"),
Ok(..) => println!("Nope, it is definitely not a PNG"),
Err(e) => println!("Some error happened: {:?}", e),
}
# }
Or check the bytes stream:
# extern crate imghdr;
# use std::fs::File;
# use std::io::{self, Read};
#
# fn main() -> io::Result<()> {
let mut file = File::open("./tests/images/example.jpeg")?;
let mut content: Vec<u8> = vec![];
file.read_to_end(&mut content)?;
match imghdr::from_bytes(&content) {
Some(imghdr::Type::Jpeg) => println!("And this is a Jpeg"),
_ => println!("Can a Png, Bmp or other file format"),
}
# Ok(())
# }