Skip to content

Commit

Permalink
fix(resources): random image week resource, adjust orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
RouHim committed Feb 12, 2024
1 parent e73fa2b commit 87ec3c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
22 changes: 13 additions & 9 deletions src/image_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ pub fn adjust_image(
}

// Resize the image to the needed display size
let base_image = read_result.unwrap();
let mut image = read_result.unwrap();

// Rotate or flip the image if needed
let fixed_orientation = if let Some(orientation) = image_orientation {
image = if let Some(orientation) = image_orientation {
let rotated = match orientation.rotation {
90 => base_image.rotate90(),
180 => base_image.rotate180(),
270 => base_image.rotate270(),
_ => base_image,
90 => image.rotate90(),
180 => image.rotate180(),
270 => image.rotate270(),
_ => image,
};

if orientation.mirror_vertically {
Expand All @@ -52,14 +52,18 @@ pub fn adjust_image(
rotated
}
} else {
base_image
image
};

let resized = fixed_orientation.resize(display_width, display_height, FilterType::Triangle);
image = if display_height > 0 && display_width > 0 {
image.resize(display_width, display_height, FilterType::Triangle)
} else {
image
};

// Write the image to a buffer
let mut bytes: Vec<u8> = Vec::new();
resized
image
.write_to(&mut Cursor::new(&mut bytes), image::ImageOutputFormat::Png)
.unwrap();
Some(bytes)
Expand Down
15 changes: 13 additions & 2 deletions src/resource_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,19 @@ pub async fn get_this_week_resource_image(
return HttpResponse::NotFound().finish();
}

// Read the image data from the file system
let resource_data = fs::read(resource_image.unwrap().path).ok();
// Read the image data from the file system and adjust the image to the display
let image_resource = resource_image.unwrap();
let resource_data = fs::read(&image_resource.path)
.ok()
.and_then(|resource_data| {
image_processor::adjust_image(
image_resource.path,
resource_data,
0,
0,
image_resource.orientation,
)
});

if let Some(resource_data) = resource_data {
HttpResponse::Ok()
Expand Down

0 comments on commit 87ec3c5

Please sign in to comment.