diff --git a/src/blob.rs b/src/blob.rs index 27c44f3e3..26432e23b 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -435,6 +435,8 @@ impl<'a> BlobObject<'a> { }); if do_scale { + let (mut m, mut d) = (8, 8); + let wh = target_wh; loop { if mem::take(&mut add_white_bg) { self::add_white_bg(&mut img); @@ -467,8 +469,11 @@ impl<'a> BlobObject<'a> { "Failed to scale image to below {max_bytes}B.", )); } - - target_wh = target_wh * 2 / 3; + (m, d) = match m > 6 { + true => (m - 1, d), + false => (11, d * 2), + }; + target_wh = wh * m / d; } else { info!( context, diff --git a/src/blob/blob_tests.rs b/src/blob/blob_tests.rs index ed5a2b001..7395c7e06 100644 --- a/src/blob/blob_tests.rs +++ b/src/blob/blob_tests.rs @@ -4,7 +4,9 @@ use super::*; use crate::message::{Message, Viewtype}; use crate::param::Param; use crate::sql; -use crate::test_utils::{self, AVATAR_64x64_BYTES, AVATAR_64x64_DEDUPLICATED, TestContext}; +use crate::test_utils::{ + self, AVATAR_64x64_BYTES, AVATAR_64x64_DEDUPLICATED, TestContext, TestContextManager, +}; use crate::tools::SystemTime; fn check_image_size(path: impl AsRef, width: u32, height: u32) -> image::DynamicImage { @@ -239,6 +241,22 @@ async fn test_selfavatar_in_blobdir() { ); } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_huge_selfavatar() -> Result<()> { + let mut tcm = TestContextManager::new(); + let t = &tcm.unconfigured().await; + let avatar_src = t.get_blobdir().join("avatar.png"); + let bytes = include_bytes!("../../test-data/image/noise400x400.png"); + + fs::write(&avatar_src, bytes).await?; + t.set_config(Config::Selfavatar, Some(avatar_src.to_str().unwrap())) + .await?; + let avatar_cfg = t.get_config(Config::Selfavatar).await?.unwrap(); + // At 6/8 the avatar is still huge, so it's downscaled to 11/16. + check_image_size(avatar_cfg, 275, 275); + Ok(()) +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_selfavatar_copy_without_recode() { let t = TestContext::new().await; diff --git a/test-data/image/noise400x400.png b/test-data/image/noise400x400.png new file mode 100644 index 000000000..730bf8d60 Binary files /dev/null and b/test-data/image/noise400x400.png differ