Merge pull request #5 from alexwlchan/create-static-images
- ID
1af5bac- date
2024-08-20 09:18:16+00:00- author
Alex Chan <alex@alexwlchan.net>- parents
a972121,49681cc- message
Merge pull request #5 from alexwlchan/create-static-images Add tests for a variety of static image formats- changed files
7 files, 99 additions, 15 deletions
Changed files
README.md (507) → README.md (466)
diff --git a/README.md b/README.md
index 4ac423f..75f4d7a 100644
--- a/README.md
+++ b/README.md
@@ -7,12 +7,8 @@ focusing on a small piece of code makes it better
-> height only
* happy path:
- -> static GIF
- -> PNG
- -> JPEG
- -> TIF
- -> WebP
-> small file
+ -> test dimensions
* errors:
-> creates thumbnail directory
src/create_thumbnail.rs (1565) → src/create_thumbnail.rs (2224)
diff --git a/src/create_thumbnail.rs b/src/create_thumbnail.rs
index 28845e7..4eb9e3c 100644
--- a/src/create_thumbnail.rs
+++ b/src/create_thumbnail.rs
@@ -3,13 +3,15 @@ use std::path::PathBuf;
use std::process::Command;
use std::str;
+use image::imageops::FilterType;
+
/// Create a thumbnail for an animated GIF.
///
/// This will use `ffmpeg` to create an MP4 file of the desired dimensions
/// which plays the GIF on a loop. This is typically much smaller and more
/// space-efficient than creating a resized GIF.
///
-/// This function assumes that the associated GIF file already exists.
+/// This function assumes that the original GIF file definitely exists.
///
/// TODO: It would be nice to have a test for the case where `ffmpeg` isn't
/// installed, but I'm not sure how to simulate that.
@@ -50,3 +52,27 @@ pub fn create_animated_gif_thumbnail(
Err(io::Error::new(io::ErrorKind::Other, error_message))
}
}
+
+/// Create a thumbnail for a static (non-animated) image.
+///
+/// This function assumes that the original image file definitely exists.
+///
+/// TODO: Get rid of the use of `unwrap()` in this code.
+///
+pub fn create_static_thumbnail(
+ image_path: &PathBuf,
+ out_dir: &PathBuf,
+ width: u32,
+ height: u32,
+) -> io::Result<PathBuf> {
+ let file_name = image_path.file_name().unwrap();
+ let thumbnail_path = out_dir.join(file_name);
+
+ let img = image::open(image_path).unwrap();
+
+ img.resize(width, height, FilterType::Lanczos3)
+ .save(&thumbnail_path)
+ .unwrap();
+
+ Ok(thumbnail_path)
+}
src/main.rs (5891) → src/main.rs (7928)
diff --git a/src/main.rs b/src/main.rs
index bb6beaf..392c8a1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,7 +4,6 @@ use std::io;
use std::path::PathBuf;
use clap::{ArgGroup, Parser};
-use image::imageops::FilterType;
mod create_parent_directory;
mod create_thumbnail;
@@ -38,14 +37,7 @@ pub fn create_thumbnail(
if is_animated_gif(path)? {
create_thumbnail::create_animated_gif_thumbnail(path, out_dir, new_width, new_height)
} else {
- println!("thumbnail_path = {:?}", thumbnail_path);
- let img = image::open(path).unwrap();
-
- img.resize(new_width, new_height, FilterType::Lanczos3)
- .save(&thumbnail_path)
- .unwrap();
-
- Ok(thumbnail_path)
+ create_thumbnail::create_static_thumbnail(path, out_dir, new_width, new_height)
}
}
@@ -69,6 +61,76 @@ mod test_create_thumbnail {
assert_eq!(thumbnail_path, out_dir.join("animated_squares.mp4"));
assert!(thumbnail_path.exists());
}
+
+ #[test]
+ fn creates_a_static_gif_thumbnail() {
+ let gif_path = PathBuf::from("src/tests/yellow.gif");
+ let out_dir = test_dir();
+ let target_width = Some(16);
+ let target_height = None;
+
+ let thumbnail_path =
+ create_thumbnail(&gif_path, &out_dir, target_width, target_height).unwrap();
+
+ assert_eq!(thumbnail_path, out_dir.join("yellow.gif"));
+ assert!(thumbnail_path.exists());
+ }
+
+ #[test]
+ fn creates_a_png_thumbnail() {
+ let gif_path = PathBuf::from("src/tests/red.png");
+ let out_dir = test_dir();
+ let target_width = Some(16);
+ let target_height = None;
+
+ let thumbnail_path =
+ create_thumbnail(&gif_path, &out_dir, target_width, target_height).unwrap();
+
+ assert_eq!(thumbnail_path, out_dir.join("red.png"));
+ assert!(thumbnail_path.exists());
+ }
+
+ #[test]
+ fn creates_a_jpeg_thumbnail() {
+ let gif_path = PathBuf::from("src/tests/noise.jpg");
+ let out_dir = test_dir();
+ let target_width = Some(16);
+ let target_height = None;
+
+ let thumbnail_path =
+ create_thumbnail(&gif_path, &out_dir, target_width, target_height).unwrap();
+
+ assert_eq!(thumbnail_path, out_dir.join("noise.jpg"));
+ assert!(thumbnail_path.exists());
+ }
+
+ #[test]
+ fn creates_a_tif_thumbnail() {
+ let gif_path = PathBuf::from("src/tests/green.tiff");
+ let out_dir = test_dir();
+ let target_width = Some(16);
+ let target_height = None;
+
+ let thumbnail_path =
+ create_thumbnail(&gif_path, &out_dir, target_width, target_height).unwrap();
+
+ assert_eq!(thumbnail_path, out_dir.join("green.tiff"));
+ assert!(thumbnail_path.exists());
+ }
+
+ #[test]
+ fn creates_a_webp_thumbnail() {
+ let gif_path = PathBuf::from("src/tests/purple.webp");
+ let out_dir = test_dir();
+ let target_width = Some(16);
+ let target_height = None;
+
+ let thumbnail_path =
+ create_thumbnail(&gif_path, &out_dir, target_width, target_height).unwrap();
+
+ assert_eq!(thumbnail_path, out_dir.join("purple.webp"));
+ assert!(thumbnail_path.exists());
+ }
}
#[derive(Debug, Parser)]
src/tests/green.tiff (0) → src/tests/green.tiff (3574)
diff --git a/src/tests/green.tiff b/src/tests/green.tiff
new file mode 100644
index 0000000..6cd0dcf
Binary files /dev/null and b/src/tests/green.tiff differ
src/tests/noise.jpg (0) → src/tests/noise.jpg (25175)
diff --git a/src/tests/noise.jpg b/src/tests/noise.jpg
new file mode 100644
index 0000000..0998e82
Binary files /dev/null and b/src/tests/noise.jpg differ
src/tests/purple.webp (0) → src/tests/purple.webp (252)
diff --git a/src/tests/purple.webp b/src/tests/purple.webp
new file mode 100644
index 0000000..6f152ec
Binary files /dev/null and b/src/tests/purple.webp differ
src/tests/yellow.gif (0) → src/tests/yellow.gif (500)
diff --git a/src/tests/yellow.gif b/src/tests/yellow.gif
new file mode 100644
index 0000000..4f72bf4
Binary files /dev/null and b/src/tests/yellow.gif differ