Files
buzz/crates/buzz-media/src/thumbnail.rs
Will Pfleger d99ad131f1 refactor: rename sprout backend to buzz (#958)
Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
Signed-off-by: Will Pfleger <wpfleger@block.xyz>
Signed-off-by: Will Pfleger <wpfleger@squareup.com>
Signed-off-by: Will Pfleger <wpfleger96@gmail.com>
Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1fgdl5qqnh3k3f2xkqrvt7cujalhm623x4s7fdjdj5yrtp5fzjl9qrjpucw <4a1bfa0013bc6d14a8d600d8bf6392efefbd2a26ac3c96c9b2a106b0d12297ca@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub16v54tttfqacx9ycvc3k0ut0npj564ahcuajzy6qjvh57ntmsf4uq4806j2 <d32955ad69077062930cc46cfe2df30ca9aaf6f8e76422681265e9e9af704d78@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Will Pfleger <wpfleger96@gmail.com>
2026-06-10 19:29:51 -04:00

52 lines
1.6 KiB
Rust

//! Synchronous thumbnail generation and blurhash encoding.
use std::io::Cursor;
use image::ImageFormat;
use crate::config::MediaConfig;
use crate::error::MediaError;
use crate::storage::BlobMeta;
/// Generate thumbnail and blurhash from image bytes (CPU-bound, sync).
///
/// Returns `(metadata, optional thumbnail JPEG bytes)`.
/// Caller handles S3 writes after `spawn_blocking` returns.
pub fn generate_image_metadata_sync(
config: &MediaConfig,
sha256: &str,
bytes: &[u8],
mime: &str,
ext: &str,
) -> Result<(BlobMeta, Option<Vec<u8>>), MediaError> {
if !mime.starts_with("image/") {
return Ok((BlobMeta::default(), None));
}
let img = image::load_from_memory(bytes)?;
let (w, h) = (img.width(), img.height());
// Thumbnail: 320px max dimension, preserve aspect ratio
let thumb = img.thumbnail(320, 320);
let mut thumb_bytes = Vec::new();
thumb.write_to(&mut Cursor::new(&mut thumb_bytes), ImageFormat::Jpeg)?;
// Blurhash from thumbnail (faster than full image)
let rgba = thumb.to_rgba8();
let bh =
blurhash::encode(4, 3, thumb.width(), thumb.height(), rgba.as_raw()).unwrap_or_default();
Ok((
BlobMeta {
dim: format!("{w}x{h}"),
blurhash: bh,
thumb_url: format!("{}/{sha256}.thumb.jpg", config.public_base_url),
ext: ext.to_string(),
mime_type: mime.to_string(),
size: bytes.len() as u64,
..BlobMeta::default() // uploaded_at set by caller (process_upload)
},
Some(thumb_bytes),
))
}