From 9f3098e80c6b6c87e9bfbfe36239a39e5cafb29f Mon Sep 17 00:00:00 2001 From: "Adam T. Carpenter" Date: Wed, 26 Oct 2022 21:02:31 -0400 Subject: init: add some stories and personas and begin layout out domain --- dichroism/src/image_service.rs | 76 ------------------------------------------ 1 file changed, 76 deletions(-) delete mode 100644 dichroism/src/image_service.rs (limited to 'dichroism/src/image_service.rs') diff --git a/dichroism/src/image_service.rs b/dichroism/src/image_service.rs deleted file mode 100644 index 0d2ed14..0000000 --- a/dichroism/src/image_service.rs +++ /dev/null @@ -1,76 +0,0 @@ -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: 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); - - let (width, height) = original.dimensions(); - let thumbnail = if width > height { - let offset = (width - height) / 2; - original.crop_imm(offset, 0, width - offset * 2, height) - } else { - let offset = (height - width) / 2; - original.crop_imm(0, offset, width, height - offset * 2) - } - .resize(PHOTO_THUMBNAIL_XY, PHOTO_THUMBNAIL_XY, FilterType::Lanczos3); - - Ok(PhotoSet { - id: None, - original: generate_photo(&original)?, - fullsize: generate_photo(&fullsize)?, - base: generate_photo(&base)?, - thumbnail: generate_photo(&thumbnail)?, - }) -} - -fn generate_photo(image: &DynamicImage) -> Result { - let base_name = Uuid::new_v3(&Uuid::NAMESPACE_OID, &image.to_bytes()) - .to_hyphenated() - .to_string(); - let mut path = PathBuf::from(&CONFIG_INSTANCE.img_root); - path.push(base_name); - path.set_extension("jpg"); - image.save(&path)?; - - let id = path - .file_name() - .ok_or_else(|| { - DichroismError::ImageWrite("Error extracting filename from path".to_string()) - })? - .to_str() - .ok_or_else(|| { - DichroismError::ImageWrite("Error converting filename to slice".to_string()) - })?; - Ok(Photo::new(String::from(id))) -} -- cgit v1.2.3