feat: compute thumbhash for images (#596)
* feat: compute thumbhash for images Signed-off-by: newt <hi@newty.dev> * Merge branch 'main' into feat/thumbhash Signed-off-by: newt <hi@newty.dev> * style: move comment onto newline Signed-off-by: newt <hi@newty.dev> * feat: make thumbhash optional Signed-off-by: newt <hi@newty.dev> --------- Signed-off-by: newt <hi@newty.dev> Signed-off-by: newt (: <hi@newty.dev> Signed-off-by: Tom <iamtomahawkx@gmail.com> Co-authored-by: Tom <iamtomahawkx@gmail.com>
This commit is contained in:
@@ -19,6 +19,7 @@ pub async fn strip_metadata(
|
||||
Metadata::Image {
|
||||
width,
|
||||
height,
|
||||
thumbhash,
|
||||
animated,
|
||||
} => match mime {
|
||||
// // little_exif does not appear to parse JPEGs correctly? had 2/2 files fail
|
||||
@@ -102,6 +103,7 @@ pub async fn strip_metadata(
|
||||
Metadata::Image {
|
||||
width,
|
||||
height,
|
||||
thumbhash: thumbhash.clone(),
|
||||
animated: *animated,
|
||||
},
|
||||
))
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::io::Cursor;
|
||||
|
||||
use image::{GenericImageView, ImageError, ImageReader};
|
||||
use revolt_database::Metadata;
|
||||
use revolt_files::{image_size, is_animated, video_size};
|
||||
use tempfile::NamedTempFile;
|
||||
@@ -26,6 +27,16 @@ pub fn generate_metadata(f: &NamedTempFile, mime_type: &str) -> Metadata {
|
||||
.map(|(width, height)| Metadata::Image {
|
||||
width: width as isize,
|
||||
height: height as isize,
|
||||
thumbhash: ImageReader::open(f)
|
||||
.and_then(|r| r.with_guessed_format())
|
||||
.map_err(ImageError::from)
|
||||
.and_then(|r| r.decode())
|
||||
.map(|img| img.thumbnail(100, 100))
|
||||
.map(|img| (img.dimensions(), img.to_rgba8().into_raw()))
|
||||
.map(|((width, height), rgba)| {
|
||||
thumbhash::rgba_to_thumb_hash(width as usize, height as usize, &rgba)
|
||||
})
|
||||
.ok(),
|
||||
animated: is_animated(f, mime_type).unwrap_or(false),
|
||||
})
|
||||
.unwrap_or_default()
|
||||
|
||||
Reference in New Issue
Block a user