summaryrefslogtreecommitdiff
path: root/dichroism/src/image_service.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dichroism/src/image_service.rs')
-rw-r--r--dichroism/src/image_service.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/dichroism/src/image_service.rs b/dichroism/src/image_service.rs
index a69cca2..0d2ed14 100644
--- a/dichroism/src/image_service.rs
+++ b/dichroism/src/image_service.rs
@@ -2,14 +2,36 @@ use crate::config::CONFIG_INSTANCE;
use crate::constants::{PHOTO_BASE_XY, PHOTO_FULLSIZE_XY, PHOTO_THUMBNAIL_XY};
use crate::error::DichroismError;
use crate::models::{Photo, PhotoSet};
+use exif::Reader;
use image::imageops::FilterType;
use image::DynamicImage;
use image::GenericImageView;
+use std::io::{BufReader, Cursor};
use std::path::PathBuf;
use uuid::Uuid;
-pub fn generate_photo_set(data: &[u8]) -> Result<PhotoSet, DichroismError> {
- let original = image::load_from_memory(&data)?;
+pub fn generate_photo_set(data: Vec<u8>) -> Result<PhotoSet, DichroismError> {
+ // load image data from bytes
+ let mut original = image::load_from_memory(&data)?;
+
+ // read EXIF tags to check for required rotation
+ let exifreader = Reader::new();
+ let mut reader = BufReader::new(Cursor::new(data));
+ let exif = exifreader.read_from_container(&mut reader).unwrap();
+ if let Some(orient) = exif
+ .get_field(exif::Tag::Orientation, exif::In::PRIMARY)
+ .map(|o| o.value.get_uint(0))
+ .flatten()
+ {
+ // if a tag was present, rotate original image data as necessary
+ match orient {
+ 8 => original = original.rotate270(),
+ 3 => original = original.rotate180(),
+ 6 => original = original.rotate90(),
+ _ => {}
+ }
+ }
+
let fullsize = original.resize(PHOTO_FULLSIZE_XY, PHOTO_FULLSIZE_XY, FilterType::Lanczos3);
let base = original.resize(PHOTO_BASE_XY, PHOTO_BASE_XY, FilterType::Lanczos3);