From a3c2f864818ba89c308ffce184b9a112d8b83cb5 Mon Sep 17 00:00:00 2001 From: malinowy5 Date: Wed, 29 May 2024 11:38:12 +0200 Subject: [PATCH] auto resize pfps --- src/api_calls.rs | 2 ++ src/image_processing.rs | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/api_calls.rs b/src/api_calls.rs index 159f769..ab9a395 100644 --- a/src/api_calls.rs +++ b/src/api_calls.rs @@ -1,6 +1,7 @@ use crate::database_functions::*; use crate::types::*; use crate::auth::*; +use crate::image_processing::*; use bytes::BufMut; use futures::{StreamExt, TryStreamExt}; use urlencoding::decode; @@ -1707,6 +1708,7 @@ pub async fn upload_image( error!("error writing file: {}", e); warp::reject::reject() })?; + crop_and_resize(pfp_file_name); add_upload_db(&connection, token.claims.uid, 10).await; info!("created file: {}", file_name); return Ok(warp::reply::with_status( diff --git a/src/image_processing.rs b/src/image_processing.rs index 704ea1f..35c58e3 100644 --- a/src/image_processing.rs +++ b/src/image_processing.rs @@ -2,8 +2,8 @@ use image::{GenericImageView, ImageFormat}; use image::imageops::{crop_imm, resize, FilterType}; use std::cmp::min; -pub fn crop_and_resize(filename: String) { - let img = image::open(format!("media/profile_pictures/{}", filename)).unwrap(); +pub fn crop_and_resize(path: String) { + let img = image::open(path.clone()).unwrap(); let width = img.dimensions().0; let height = img.dimensions().1; @@ -15,12 +15,12 @@ pub fn crop_and_resize(filename: String) { let cropped_img = crop_imm(&img, mid_x-temp_size/2, mid_y-temp_size/2, mid_x+temp_size/2, mid_y+temp_size/2); cropped_img .to_image() - .save_with_format(&format!("media/profile_pictures/{}", filename), ImageFormat::Png) + .save_with_format(&path, ImageFormat::Png) .unwrap(); - let img = image::open(format!("media/profile_pictures/{}", filename)).unwrap(); + let img = image::open(path.clone()).unwrap(); let resized_img = resize(&img, 128, 128, FilterType::Lanczos3); - resized_img.save_with_format(&format!("media/profile_pictures/{}", filename), ImageFormat::Png).unwrap(); + resized_img.save_with_format(&path, ImageFormat::Png).unwrap(); } \ No newline at end of file