pub fn generate_image_thumbnail<R: BufRead + Seek>(
    content_type: &Mime,
    reader: R,
    size: Option<(u32, u32)>
) -> Result<(Vec<u8>, BaseThumbnailInfo), ImageError>
Available on crate feature image-proc only.
Expand description

Generate a thumbnail for an image.

This is a convenience method that uses the image crate.

§Arguments

  • content_type - The type of the media, this will be used as the content-type header.

  • reader - A Reader that will be used to fetch the raw bytes of the media.

  • size - The size of the thumbnail in pixels as a (width, height) tuple. If set to None, defaults to (800, 600).

§Examples

use std::{io::Cursor, path::PathBuf};

use matrix_sdk::attachment::{
    generate_image_thumbnail, AttachmentConfig, Thumbnail,
};
use mime;
let path = PathBuf::from("/home/example/my-cat.jpg");
let image = tokio::fs::read(path).await?;

let cursor = Cursor::new(&image);
let (thumbnail_data, thumbnail_info) =
    generate_image_thumbnail(&mime::IMAGE_JPEG, cursor, None)?;
let config = AttachmentConfig::with_thumbnail(Thumbnail {
    data: thumbnail_data,
    content_type: mime::IMAGE_JPEG,
    info: Some(thumbnail_info),
});

if let Some(room) = client.get_room(&room_id) {
    room.send_attachment(
        "my_favorite_cat.jpg",
        &mime::IMAGE_JPEG,
        image,
        config,
    )
    .await?;
}