Image Resize & Watermark — Rust source
Batch-resize images by width, height or percentage and stamp a text watermark with position, opacity and size control — entirely in your browser, nothing uploaded.
This is the Rust implementation — the same logic the interactive tool runs, in a shareable, citable form.
//! image-resize-and-watermark — resize geometry + text-watermark layout.
//!
//! Language: Rust (std only — no external crates).
//! CosmoDev polyglot showcase port of the `image-resize-and-watermark` tool,
//! ported from src/tools/ImageResizeWatermark.tsx.
//!
//! Rust's std has no image codec or font rasterizer, so — like the Zig twin —
//! this port covers the deterministic layout half: the four resize modes,
//! nine-anchor watermark placement, stroke width, and output naming.
//!
//! display source — part of CosmoDev's polyglot tool pages.
/// px gutter every anchor pins text in by.
pub const MARGIN: f64 = 8.0;
/// Default fill alpha (opacity slider 70 / 100); stroke is 55%.
pub const FILL_ALPHA: f64 = 0.7;
/// Approximate measureText: ~0.56 em average advance per character.
pub const EM_ADVANCE: f64 = 0.56;
/// JS Math.round parity: half up, floor(x + 0.5) — f64::round rounds half
/// away from zero, equal here but the contract stays explicit.
fn js_round(x: f64) -> f64 {
(x + 0.5).floor()
}
/// Math.max(1, Math.round(x)) — the clamp the tool applies to derived sides.
fn fit(x: f64) -> i32 {
(js_round(x) as i32).max(1)
}
/// Output (w, h) for a resize mode ("none" | "width" | "height" | "percent"),
/// mirroring the tool's process() arithmetic.
pub fn target_size(mode: &str, src_w: i32, src_h: i32, target: i32) -> (i32, i32) {
let (sw, sh, t) = (src_w as f64, src_h as f64, target as f64);
match mode {
"none" => (src_w, src_h),
"width" => (target, fit(sh / sw * t)),
"height" => (fit(sw / sh * t), target),
"percent" => (fit(sw * t / 100.0), fit(sh * t / 100.0)),
other => panic!("unknown resize mode: {other}"),
}
}
/// Watermark font size: wm_scale% of the shorter side, floored at 10 px.
pub fn font_size(w: i32, h: i32, wm_scale: i32) -> i32 {
(js_round(w.min(h) as f64 * wm_scale as f64 / 100.0) as i32).max(10)
}
/// Dark outline width for the strokeText pass: max(1, fontSize / 14).
pub fn stroke_width(font_px: i32) -> f64 {
(font_px as f64 / 14.0).max(1.0)
}
/// Approximate canvas measureText for a system sans-serif (~0.56 em per char).
pub fn text_width(text: &str, font_px: i32) -> f64 {
text.chars().count() as f64 * font_px as f64 * EM_ADVANCE
}
/// Horizontal anchor: left/right pin 8 px in, center balances the text box.
pub fn anchor_x(anchor: &str, box_w: i32, item: f64) -> f64 {
match anchor.as_bytes()[1] { // l | c | r
b'l' => MARGIN,
b'r' => box_w as f64 - item - MARGIN,
_ => (box_w as f64 - item) / 2.0,
}
}
/// Vertical anchor: top baseline fs+8, middle 1/3 fs below center, bottom h-8.
pub fn anchor_y(anchor: &str, box_h: i32, font_px: i32) -> f64 {
match anchor.as_bytes()[0] { // t | m | b
b't' => font_px as f64 + MARGIN,
b'b' => box_h as f64 - MARGIN,
_ => box_h as f64 / 2.0 + font_px as f64 / 3.0,
}
}
/// Strip the old extension and tag -edited.webp, like the tool's outName.
pub fn output_name(name: &str) -> String {
let stem = match name.rfind('.') {
Some(i) if i + 1 < name.len() => &name[..i],
_ => name,
};
format!("{stem}-edited.webp")
}
fn main() {
let (w, h) = target_size("width", 4032, 3024, 1280);
let fs = font_size(w, h, 6);
println!("target {w}x{h}, watermark font {fs}px");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resize_of_a_4032x3024_photo() {
assert_eq!(target_size("width", 4032, 3024, 1280), (1280, 960));
assert_eq!(target_size("percent", 1000, 500, 50), (500, 250));
assert_eq!(target_size("none", 640, 480, 9999), (640, 480));
assert_eq!(target_size("percent", 2, 3, 1), (1, 2)); // sub-1px clamps to 1
}
#[test]
fn watermark_layout_bottom_right() {
let fs = font_size(1280, 960, 6);
assert_eq!(fs, 58); // 6% of the 960 px short side
assert!((stroke_width(fs) - 58.0 / 14.0).abs() < 1e-9);
assert_eq!(anchor_y("br", 960, fs), 952.0); // h - 8
let x = anchor_x("br", 1280, text_width("CosmoDev", fs));
assert!(x > 900.0 && x < 950.0); // 1280 - ~8*58*0.56 - 8
}
#[test]
fn output_naming() {
assert_eq!(output_name("photo.JPG"), "photo-edited.webp");
assert_eq!(output_name("noext"), "noext-edited.webp");
}
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →