1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Fix image rotation

This commit is contained in:
Pierre HUBERT 2020-06-22 10:35:39 +02:00
parent 83492f6c8d
commit e915e4b7d0
3 changed files with 52 additions and 1 deletions

16
Cargo.lock generated
View File

@ -546,6 +546,7 @@ dependencies = [
"encoding_rs", "encoding_rs",
"futures", "futures",
"image", "image",
"kamadak-exif",
"mailchecker", "mailchecker",
"mysql", "mysql",
"percent-encoding", "percent-encoding",
@ -1111,6 +1112,15 @@ dependencies = [
"rayon", "rayon",
] ]
[[package]]
name = "kamadak-exif"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a5e66d5b5469321038611f7f0e845a48989e4fd54987b6e5bb4c8ae3adbace7"
dependencies = [
"mutate_once",
]
[[package]] [[package]]
name = "kernel32-sys" name = "kernel32-sys"
version = "0.2.2" version = "0.2.2"
@ -1323,6 +1333,12 @@ dependencies = [
"ws2_32-sys", "ws2_32-sys",
] ]
[[package]]
name = "mutate_once"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16cf681a23b4d0a43fc35024c176437f9dcd818db34e0f42ab456a0ee5ad497b"
[[package]] [[package]]
name = "mysql" name = "mysql"
version = "18.2.0" version = "18.2.0"

View File

@ -22,4 +22,5 @@ sha1 = "0.6.0"
rand = "0.7.3" rand = "0.7.3"
chrono = "0.4.11" chrono = "0.4.11"
bytes = "0.5.4" bytes = "0.5.4"
image = "0.23.5" image = "0.23.5"
kamadak-exif = "0.5.1"

View File

@ -5,6 +5,7 @@ use std::str::FromStr;
use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::{HttpRequest, HttpResponse, web};
use actix_web::http::{HeaderName, HeaderValue}; use actix_web::http::{HeaderName, HeaderValue};
use bytes::Bytes; use bytes::Bytes;
use exif::In;
use image::{GenericImageView, ImageFormat}; use image::{GenericImageView, ImageFormat};
use serde::Serialize; use serde::Serialize;
@ -287,6 +288,39 @@ impl HttpRequestHandler {
image = image.resize(max_w, max_h, image::imageops::FilterType::Nearest); image = image.resize(max_w, max_h, image::imageops::FilterType::Nearest);
} }
// Read EXIF information in case of JPEG image, if possible
if let Ok(ImageFormat::Jpeg) = image::guess_format(file.buff.as_ref()) {
let mut reader = std::io::BufReader::new(file.buff.as_ref());
if let Ok(exif_attr) = exif::get_exif_attr_from_jpeg(&mut reader) {
let exif_reader = exif::Reader::new();
let exif = exif_reader.read_raw(exif_attr)?;
if let Some(v) = exif.get_field(exif::Tag::Orientation, In::PRIMARY) {
match v.value.get_uint(0) {
Some(1) => { /* row 0 is top and column 0 is left */ }
//Some(2) => println!("row 0 at top and column 0 at right"),
Some(3) => {
/* row 0 at bottom and column 0 at right */
image = image.rotate180()
}
//Some(4) => println!("row 0 at bottom and column 0 at left"),
//Some(5) => println!("row 0 at left and column 0 at top"),
Some(6) => {
/* row 0 is right and column 0 is top */
image = image.rotate90();
}
//Some(7) => println!("row 0 at right and column 0 at bottom"),
Some(8) => {
/* row 0 is left and column 0 is bottom */
image = image.rotate270();
}
v => println!("Unhandled EXIF Orientation: {:?}", v),
};
}
}
}
// Determine image destination // Determine image destination
let target_user_data_folder = prepare_file_creation(self.user_id()?, folder)?; let target_user_data_folder = prepare_file_creation(self.user_id()?, folder)?;