Function matrix_sdk::attachment::generate_image_thumbnail

source ·
pub fn generate_image_thumbnail<R: BufRead + Seek>(
    content_type: &Mime,
    reader: R,
    size: Option<(u32, u32)>,
    format: ThumbnailFormat,
) -> Result<Thumbnail, 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).

  • format - The image format to use to encode the thumbnail.

§Examples

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

use matrix_sdk::attachment::{
    generate_image_thumbnail, AttachmentConfig, Thumbnail, ThumbnailFormat,
};
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 = generate_image_thumbnail(
    &mime::IMAGE_JPEG,
    cursor,
    None,
    ThumbnailFormat::Original,
)?;
let config = AttachmentConfig::with_thumbnail(thumbnail);

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