From eb685f99e7790d2fe84f5c16f3577968a1075cf6 Mon Sep 17 00:00:00 2001 From: asonix Date: Tue, 16 Jun 2020 21:05:06 -0500 Subject: [PATCH] Use sample for thumbnail generation --- Cargo.lock | 3 +-- Cargo.toml | 2 +- src/processor.rs | 11 ++++++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bad07d2..dafee3d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1188,8 +1188,7 @@ checksum = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" [[package]] name = "magick_rust" version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef1258fe816e2f16c40532a167718321ec78178984a08b62fca6f6a45784bd33" +source = "git+https://git.asonix.dog/asonix/magick-rust#c5c4c68dcee9947c0975e2831f0a5c32a3ebea66" dependencies = [ "bindgen", "libc", diff --git a/Cargo.toml b/Cargo.toml index 50790b9..eb725f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ base64 = "0.12.1" bytes = "0.5" futures = "0.3.4" gif = "0.10.3" -magick_rust = "0.14.0" +magick_rust = { version = "0.14.0", git = "https://git.asonix.dog/asonix/magick-rust" } mime = "0.3.1" once_cell = "1.4.0" rand = "0.7.3" diff --git a/src/processor.rs b/src/processor.rs index d6f3328..79d1fcc 100644 --- a/src/processor.rs +++ b/src/processor.rs @@ -105,7 +105,16 @@ impl Processor for Thumbnail { let height = wand.get_image_height(); if width > self.0 || height > self.0 { - wand.fit(self.0, self.0); + let width_ratio = width as f64 / self.0 as f64; + let height_ratio = height as f64 / self.0 as f64; + + let (new_width, new_height) = if width_ratio < height_ratio { + (width as f64 / height_ratio, self.0 as f64) + } else { + (self.0 as f64, height as f64 / width_ratio) + }; + + wand.op(|w| w.sample_image(new_width as usize, new_height as usize))?; Ok(true) } else if wand.op(|w| w.get_image_format())? == "GIF" { Ok(true)