A library to extract base information of the image file:
- file size;
- MIME type;
- pHash;
- dimensions.
To get MIME type uses infer crate. If the file does not have a valid image MIME type, the tree_magic_mini crate is used
by fallback to get the real MIME type as infer returns an error if the MIME is not for image (e.g. "plain/text").
To get dimensions uses imagesize crate.
Reports error codes and error messages on failure.
Can render result as JSON string (useful for using from other languages as wrapper with binary code calling
if the FFI is not an option). If you don't need this feature you can disable it within your Cargo.toml:
[dependencies]
image_info = {version = "0.3.1", default-features = false}
HEIF and AVIF format support. Both formats are decoded via the native
libvips library. If you need pHash for either of these formats, enable the
phash-vips feature:
[dependencies]
image_info = {version = "0.3.4", features = ["phash-vips"]}# Debian/Ubuntu
apt install libvips-dev pkg-config
# macOS
brew install vips pkg-configThe libvips system library above is needed at both build and run time. It is
only required by the phash-vips feature — the default build and the plain
phash feature do not need it.
fn main() {
let args: Vec<String> = std::env::args().collect();
let path: &str = match args.get(1) {
Some(arg) => arg,
None => {
println!("No path argument specified");
std::process::exit(image_info::CODE_BAD_ARGS);
}
};
//second parameter "true" stands for "generate phash"
let info = image_info::process_path(path, true);
println!("{:?}", info);
//InfoResult { mime_type: Some("image/jpeg"), width: Some(1920), height: Some(1280), phash: Some("8Ph4HY9Tefw"), error_message: None, error_code: 0 }
//InfoResult { mime_type: Some("text/html"), width: None, height: None, phash: None, error_message: Some("Error: mime type is not image/*: text/html"), error_code: 2 }
//if "render" feature is enabled
println!("{}", info.render());
//{"mime_type":"image/jpeg","width":1920,"height":1280,"phash":"8Ph4HY9Tefw","error_message":null,"error_code":0}
//{"mime_type":"text/html","width":null,"height":null,"phash":null,"error_message":"Error: mime type is not image/*: text/html","error_code":2}
std::process::exit(info.get_error_code());
}
The example output above can be regenerated by running the examples/dump.rs example:
cargo run --example dump --features phash,render