Skip to content

Commit

Permalink
Resize 0 (#6)
Browse files Browse the repository at this point in the history
* Skip resizing if 0

Signed-off-by: lloydmeta <[email protected]>

* Update readme

Signed-off-by: lloydmeta <[email protected]>

---------

Signed-off-by: lloydmeta <[email protected]>
  • Loading branch information
lloydmeta authored Nov 1, 2024
1 parent 0d0d4c8 commit b25dd94
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Tiny HTTP image resizer.
To fulfil the above:

* Runs in a Lambda
* Running on ARM, [because it's cheaper](https://blog.awsfundamentals.com/aws-lambda-pricing-a-complete-guide-to-understanding-the-cost-of-the-serverless-service#heading-architectures-arm-vs-x86-arm-is-cheaper): "about 34% cheaper compared to the default x86 processors"
* Rust ⚡️
* Caching in layers: CDN to protect the app, with S3 for storing images
* Serverless, but built on HTTP framework ([cargo-lambda](https://www.cargo-lambda.info) on top of [axum](https://github.com/tokio-rs/axum))
Expand Down
28 changes: 27 additions & 1 deletion server/src/infra/image_manipulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ impl OperationsRunner for SingletonOperationsRunner {
operations.0.iter().fold(image, |mut next, op| {
next = match op {
Operation::Resize { width, height } => {
next.resize(*width, *height, image::imageops::FilterType::Gaussian)
let resize_to_width = if *width == 0 { next.width() } else { *width };
let resize_to_height = if *height == 0 { next.height() } else { *height };
next.resize(
resize_to_width,
resize_to_height,
image::imageops::FilterType::Gaussian,
)
}
Operation::FlipHorizontally => next.fliph(),
Operation::FlipVertically => next.flipv(),
Expand Down Expand Up @@ -121,4 +127,24 @@ mod tests {
assert_eq!(3, result.width());
assert_eq!(2, result.height());
}

#[tokio::test]
async fn test_operations_runner_no_resize() {
let image_bin = include_bytes!("not-aliens.jpg");
let image_reader = ImageReader::new(Cursor::new(image_bin))
.with_guessed_format()
.unwrap();

let image = image_reader.decode().unwrap();
let original_image = image.clone();

let operations = Operations::build(&Some(ImageResize {
target_width: 0,
target_height: 0,
}));

let result = SingletonOperationsRunner.run(image, &operations).await;
assert_eq!(original_image.width(), result.width());
assert_eq!(original_image.height(), result.height());
}
}

0 comments on commit b25dd94

Please sign in to comment.