From bb1edd35721d34ce394ab9e51c4095015f26b185 Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Mon, 28 Dec 2020 08:43:44 -0500 Subject: fixed images that were rotated with exif tags not displaying rotated --- dichroism/src/handlers.rs | 2 +- dichroism/src/image_service.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) (limited to 'dichroism/src') diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs index 8dd0f3b..b9420af 100644 --- a/dichroism/src/handlers.rs +++ b/dichroism/src/handlers.rs @@ -117,7 +117,7 @@ async fn post_photo( } // create new photo_set - let photo_set = web::block(move || image_service::generate_photo_set(&data)) + let photo_set = web::block(move || image_service::generate_photo_set(data)) .await .map_err(to_internal_error)?; let conn = pool.get().map_err(to_internal_error)?; 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 { - let original = image::load_from_memory(&data)?; +pub fn generate_photo_set(data: Vec) -> Result { + // 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); -- cgit v1.2.3