Skip to content

Commit

Permalink
Merge pull request #62 from picture-pro/25-thumbnail-watermark
Browse files Browse the repository at this point in the history
25 thumbnail watermark
  • Loading branch information
johnbchron authored Mar 1, 2024
2 parents a8f19f6 + a34f70d commit 0c0d380
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
Binary file added crates/bl/assets/watermark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added crates/bl/assets/watermark.xcf
Binary file not shown.
7 changes: 6 additions & 1 deletion crates/bl/src/upload.rs → crates/bl/src/upload/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "ssr")]
mod watermark;

use std::collections::HashMap;

#[cfg(feature = "ssr")]
Expand Down Expand Up @@ -169,11 +172,13 @@ async fn create_photo(img: image::DynamicImage) -> Result<PhotoRecordId> {
let thumbnail_size = thumbnail_size(aspect_ratio);

// create thumbnail
let thumbnail_image = img.resize_exact(
let mut thumbnail_image = img.resize_exact(
thumbnail_size.0,
thumbnail_size.1,
image::imageops::FilterType::Lanczos3,
);
// apply watermark
watermark::apply_watermark(&mut thumbnail_image);

// encode thumbnail image as jpeg
let mut thumbnail_bytes = Vec::new();
Expand Down
44 changes: 44 additions & 0 deletions crates/bl/src/upload/watermark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use image::{DynamicImage, GenericImageView};

fn create_watermark(width: u32, height: u32) -> image::DynamicImage {
// load the watermark image from bytes which is a transparent 200x200 pixels
// image
let watermark_bytes = include_bytes!("../../assets/watermark.png");
let watermark = image::load_from_memory(watermark_bytes).unwrap();

// center the watermark inside an image of the correct size, or if it's too
// large, scale it down
let (w, h) = watermark.dimensions();
let (w, h) = if w > width || h > height {
let scale = (width as f32 / w as f32).min(height as f32 / h as f32);
(w as f32 * scale, h as f32 * scale)
} else {
(w as f32, h as f32)
};
let watermark = watermark.resize_exact(
w as u32,
h as u32,
image::imageops::FilterType::Lanczos3,
);
// center the watermark
let x = (width - w as u32) / 2;
let y = (height - h as u32) / 2;
let mut final_watermark = image::RgbaImage::new(width, height);
image::imageops::overlay(
&mut final_watermark,
&watermark.to_rgba8(),
x.into(),
y.into(),
);

DynamicImage::ImageRgba8(final_watermark)
}

pub fn apply_watermark(target: &mut DynamicImage) {
// create the watermark image
let (width, height) = target.dimensions();
let watermark = create_watermark(width, height);

// blend the watermark onto the target image
image::imageops::overlay(target, &watermark, 0, 0);
}

0 comments on commit 0c0d380

Please sign in to comment.