use crate::Result;
use image::ImageFormat;
pub fn make_thumbnail(path: &std::path::Path) -> Result<(String, Vec<u8>)> {
let format = ImageFormat::from_path(path)?;
const THUMBNAIL_LONGEST_EDGE: u32 = 1024;
const THUMBNAIL_JPEG_QUALITY: u8 = 80;
let mut img = image::open(path)?;
let longest_edge = THUMBNAIL_LONGEST_EDGE;
if img.width() > longest_edge || img.height() > longest_edge {
img = img.thumbnail(longest_edge, longest_edge);
}
let (output_format, content_type) = match format {
ImageFormat::Png => (image::ImageOutputFormat::Png, "image/png"),
_ => (
image::ImageOutputFormat::Jpeg(THUMBNAIL_JPEG_QUALITY),
"image/jpeg",
),
};
let thumbnail_bits = Vec::new();
let mut cursor = std::io::Cursor::new(thumbnail_bits);
img.write_to(&mut cursor, output_format)?;
let format = content_type.to_owned();
Ok((format, cursor.into_inner()))
}