Scale jpeg while loading.

This commit is contained in:
Rasmus Kaj 2020-06-30 20:51:40 +02:00
parent c646b9fb0d
commit 2ce3c59e9b
2 changed files with 21 additions and 2 deletions

View File

@ -15,7 +15,7 @@ chrono = "0.4.0" # Must match version used by diesel
dotenv = "0.15"
env_logger = "0.7.1"
flate2 = "1.0.14"
image = "0.23.0"
image = "0.23.11"
medallion = "2.3.1"
kamadak-exif = "0.5.0"
libc = "0.2.68"

View File

@ -128,7 +128,17 @@ pub async fn get_scaled_jpeg(
) -> Result<Vec<u8>, ImageLoadFailed> {
spawn_blocking(move || {
info!("Should open {:?}", path);
let img = image::open(path)?;
let img = if is_jpeg(&path) {
use std::fs::File;
use std::io::BufReader;
let file = BufReader::new(File::open(path)?);
let mut decoder = image::jpeg::JpegDecoder::new(file)?;
decoder.scale(size as u16, size as u16)?;
image::DynamicImage::from_decoder(decoder)?
} else {
image::open(path)?
};
let img = if 3 * size <= img.width() || 3 * size <= img.height() {
info!("T-nail from {}x{} to {}", img.width(), img.height(), size);
@ -155,3 +165,12 @@ pub async fn get_scaled_jpeg(
})
.await?
}
fn is_jpeg(path: &Path) -> bool {
if let Some(suffix) = path.extension().and_then(|s| s.to_str()) {
suffix.eq_ignore_ascii_case("jpg")
|| suffix.eq_ignore_ascii_case("jpeg")
} else {
false
}
}