From 83492f6c8dc3f9642e803b32b86c055983df4669 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 22 Jun 2020 09:11:56 +0200 Subject: [PATCH] Resize image only if required --- src/data/http_request_handler.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 8dafbad..a181a8b 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -5,7 +5,7 @@ use std::str::FromStr; use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::http::{HeaderName, HeaderValue}; use bytes::Bytes; -use image::ImageFormat; +use image::{GenericImageView, ImageFormat}; use serde::Serialize; use crate::api_data::http_error::HttpError; @@ -281,8 +281,11 @@ impl HttpRequestHandler { // Load image let file = self.post_file(name)?; - let image = image::load_from_memory(file.buff.as_ref())?; - let image = image.resize(max_w, max_h, image::imageops::FilterType::Nearest); + let mut image = image::load_from_memory(file.buff.as_ref())?; + + if image.width() > max_w || image.height() > max_h { + image = image.resize(max_w, max_h, image::imageops::FilterType::Nearest); + } // Determine image destination