fix wateramrk

This commit is contained in:
Julian Freeman
2026-01-18 23:55:14 -04:00
parent 0c307c319a
commit a5f1b165fd
3 changed files with 32 additions and 17 deletions

View File

@@ -97,7 +97,7 @@ use image::Pixel;
use rayon::prelude::*;
use std::path::Path;
use imageproc::drawing::draw_text_mut;
use ab_glyph::{FontRef, PxScale, Font};
use ab_glyph::{FontRef, PxScale};
// Embed the font to ensure it's always available without path issues
const FONT_DATA: &[u8] = include_bytes!("../assets/fonts/Roboto-Regular.ttf");
@@ -178,13 +178,13 @@ async fn export_batch(images: Vec<ExportImageTask>, watermark: WatermarkSettings
let final_scale = PxScale::from(scale_px);
let (final_t_width, final_t_height) = imageproc::drawing::text_size(final_scale, &font, &watermark.text);
// 4. Determine Position
let (pos_x_pct, pos_y_pct) = if watermark.manual_override {
// 4. Determine Position (Center based)
let (mut pos_x_pct, mut pos_y_pct) = if watermark.manual_override {
(watermark.manual_position.x, watermark.manual_position.y)
} else {
match calculate_zca_internal(&dynamic_img) {
Ok(res) => (res.x, res.y),
Err(_) => (0.5, 0.95),
Err(_) => (0.5, 0.96), // Default fallback lowered
}
};
@@ -192,12 +192,18 @@ async fn export_batch(images: Vec<ExportImageTask>, watermark: WatermarkSettings
let center_x = width as f64 * pos_x_pct;
let center_y = height as f64 * pos_y_pct;
let x = (center_x - (final_t_width as f64 / 2.0)) as i32;
let y = (center_y - (final_t_height as f64 / 2.0)) as i32;
let mut x = (center_x - (final_t_width as f64 / 2.0)) as i32;
let mut y = (center_y - (final_t_height as f64 / 2.0)) as i32;
// Clamp to be visible? imageproc handles out of bounds by clipping.
// 5. Boundary Clamping (Ensure text stays inside image with padding)
let padding = (height as f64 * 0.01).max(5.0) as i32; // 1% padding
let max_x = (width as i32 - final_t_width as i32 - padding).max(padding);
let max_y = (height as i32 - final_t_height as i32 - padding).max(padding);
// 5. Draw
x = x.clamp(padding, max_x);
y = y.clamp(padding, max_y);
// 6. Draw
draw_text_mut(
&mut base_img,
text_color,