Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Finish compression
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically committed Sep 8, 2024
1 parent 60c40de commit 9589c72
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/routes/v3/project_creation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ async fn project_create_inner(
let (_, file_extension) =
super::version_creation::get_name_ext(&content_disposition)?;

let url = format!("data/{project_id}/images/");
let url = format!("data/{project_id}/images");
let upload_result = upload_image_optimized(
&url,
data.freeze(),
Expand Down
4 changes: 2 additions & 2 deletions src/routes/v3/projects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1576,8 +1576,8 @@ pub async fn add_gallery_item(
.await?;

let id: ProjectId = project_item.inner.id.into();
let upload_result = crate::util::img::upload_image_optimized(
&format!("data/{}/images/", id),
let upload_result = upload_image_optimized(
&format!("data/{}/images", id),
bytes.freeze(),
&ext.ext,
Some(350),
Expand Down
35 changes: 17 additions & 18 deletions src/util/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ pub async fn upload_image_optimized(
) -> Result<UploadImageResult, ApiError> {
let content_type =
crate::util::ext::get_image_content_type(file_extension).ok_or_else(|| {
ApiError::InvalidInput(format!(
"Invalid format for image: {}",
file_extension
))
ApiError::InvalidInput(format!("Invalid format for image: {}", file_extension))
})?;

let cdn_url = dotenvy::var("CDN_URL")?;
Expand Down Expand Up @@ -86,16 +83,16 @@ pub async fn upload_image_optimized(

let url = format!("{}/{}", cdn_url, upload_data.file_name);
Ok(UploadImageResult {
raw_url: processed_upload_data
url: processed_upload_data
.clone()
.map(|x| format!("{}/{}", cdn_url, x.file_name))
.unwrap_or_else(|| url.clone()),
raw_url_path: processed_upload_data
url_path: processed_upload_data
.map(|x| x.file_name)
.unwrap_or_else(|| upload_data.file_name.clone()),

url,
url_path: upload_data.file_name,
raw_url: url,
raw_url_path: upload_data.file_name,
color,
})
}
Expand All @@ -120,16 +117,18 @@ fn process_image(
let aspect_ratio = orig_width as f32 / orig_height as f32;

if let Some(target_width) = target_width {
let new_height = (target_width as f32 / aspect_ratio).round() as u32;
img = img.resize(target_width, new_height, FilterType::Lanczos3);

if let Some(min_aspect_ratio) = min_aspect_ratio {
// Crop if necessary
if aspect_ratio < min_aspect_ratio {
let crop_height = (target_width as f32 / min_aspect_ratio).round() as u32;
let y_offset = (new_height - crop_height) / 2;
img = img.crop_imm(0, y_offset, target_width, crop_height);
}
if img.width() > target_width {
let new_height = (target_width as f32 / aspect_ratio).round() as u32;
img = img.resize(target_width, new_height, FilterType::Lanczos3);
}
}

if let Some(min_aspect_ratio) = min_aspect_ratio {
// Crop if necessary
if aspect_ratio < min_aspect_ratio {
let crop_height = (img.width() as f32 / min_aspect_ratio).round() as u32;
let y_offset = (img.height() - crop_height) / 2;
img = img.crop_imm(0, y_offset, img.width(), crop_height);
}
}

Expand Down

0 comments on commit 9589c72

Please sign in to comment.