basically recode images

This commit is contained in:
B. Petersen
2020-06-05 21:19:12 +02:00
parent 5d1623b98f
commit 191009372b
3 changed files with 48 additions and 1 deletions

View File

@@ -8,9 +8,11 @@ use async_std::prelude::*;
use async_std::{fs, io};
use image::GenericImageView;
use num_traits::FromPrimitive;
use thiserror::Error;
use crate::constants::AVATAR_SIZE;
use crate::config::Config;
use crate::constants::*;
use crate::context::Context;
use crate::events::Event;
@@ -377,6 +379,39 @@ impl<'a> BlobObject<'a> {
Ok(())
}
pub async fn recode_to_image_size(&self, context: &Context) -> Result<(), BlobError> {
let blob_abs = self.to_abs_path();
let img = image::open(&blob_abs).map_err(|err| BlobError::RecodeFailure {
blobdir: context.get_blobdir().to_path_buf(),
blobname: blob_abs.to_str().unwrap_or_default().to_string(),
cause: err,
})?;
let (img_wh, _quality) =
if MediaQuality::from_i32(context.get_config_int(Config::MediaQuality).await)
.unwrap_or_default()
== MediaQuality::Balanced
{
(BALANCED_IMAGE_SIZE, BALANCED_IMAGE_QUALITY)
} else {
(WORSE_IMAGE_SIZE, WORSE_IMAGE_QUALITY)
};
if img.width() <= img_wh && img.height() <= img_wh {
return Ok(());
}
let img = img.thumbnail(img_wh, img_wh);
img.save(&blob_abs).map_err(|err| BlobError::WriteFailure {
blobdir: context.get_blobdir().to_path_buf(),
blobname: blob_abs.to_str().unwrap_or_default().to_string(),
cause: err,
})?;
Ok(())
}
}
impl<'a> fmt::Display for BlobObject<'a> {