Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,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);
Expand Down Expand Up @@ -464,8 +466,11 @@ impl<'a> BlobObject<'a> {
"Failed to scale image to below {max_bytes}B.",
));
}

target_wh = target_wh * 7 / 8;
(m, d) = match m > 6 {
true => (m - 1, d),
false => (11, d * 2),
};
target_wh = wh * m / d;
} else {
info!(
context,
Expand Down
20 changes: 19 additions & 1 deletion src/blob/blob_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>, width: u32, height: u32) -> image::DynamicImage {
Expand Down Expand Up @@ -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;
Expand Down
Binary file added test-data/image/noise400x400.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.