Function imagesize::get_dimensions_from_blob [] [src]

pub fn get_dimensions_from_blob(data: &Vec<u8>) -> ImageResult<Dimensions>

Get the image dimensions from a block of data.

Arguments

  • data - A Vec containing the data to parse for image dimensions.

Remarks

This method is useful when you need only the size of an image and have a way to only read part of the data. For example, using the Range header in a http request to receive the first part of an image file.

Error

This method will return an ImageError under the following conditions:

  • The first byte of the header isn't recognized as a supported image
  • The data isn't long enough to find the dimensions for the given format

Examples

use imagesize::get_dimensions_from_blob;

//  First 32 bytes of a PNG Header with size 123x321
let data = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 
                0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 
                0x00, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x01, 0x41,
                0x08, 0x06, 0x00, 0x00, 0x00, 0x9A, 0x38, 0xC4];

match get_dimensions_from_blob(&data) {
    Ok(dim) => {
        assert_eq!(dim.width, 123);
        assert_eq!(dim.height, 321);
    }
    Err(why) => println!("Error getting dimensions: {:?}", why)
}