feat: add watermark, text overlay, and image composition tools

Add 4 watermark/overlay tools with API routes and frontend settings:
- watermark-text: SVG text overlay with tiling, position, opacity, rotation
- watermark-image: logo/image watermark with position, opacity, scale
- text-overlay: styled text on images with shadow and background box
- compose: layer images with position, opacity, and blend modes
This commit is contained in:
Siddharth Kumar Sah
2026-03-22 04:20:26 +08:00
parent c41d046316
commit aeaf783ee4
8 changed files with 988 additions and 0 deletions
@@ -0,0 +1,148 @@
import { useState, useRef } from "react";
import { useFileStore } from "@/stores/file-store";
import { Download, Loader2, Upload } from "lucide-react";
function getToken(): string {
return localStorage.getItem("stirling-token") || "";
}
export function ComposeSettings() {
const { files, processing, error, setProcessing, setError, setProcessedUrl, setSizes, setJobId } = useFileStore();
const [overlayFile, setOverlayFile] = useState<File | null>(null);
const [x, setX] = useState(0);
const [y, setY] = useState(0);
const [opacity, setOpacity] = useState(100);
const [blendMode, setBlendMode] = useState("over");
const [downloadUrl, setDownloadUrl] = useState<string | null>(null);
const [originalSize, setOriginalSize] = useState<number | null>(null);
const [processedSize, setProcessedSize] = useState<number | null>(null);
const overlayInputRef = useRef<HTMLInputElement>(null);
const handleProcess = async () => {
if (files.length === 0 || !overlayFile) return;
setProcessing(true);
setError(null);
setDownloadUrl(null);
try {
const formData = new FormData();
formData.append("file", files[0]);
formData.append("overlay", overlayFile);
formData.append("settings", JSON.stringify({ x, y, opacity, blendMode }));
const res = await fetch("/api/v1/tools/compose", {
method: "POST",
headers: { Authorization: `Bearer ${getToken()}` },
body: formData,
});
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.error || `Processing failed: ${res.status}`);
}
const result = await res.json();
setJobId(result.jobId);
setProcessedUrl(result.downloadUrl);
setDownloadUrl(result.downloadUrl);
setOriginalSize(result.originalSize);
setProcessedSize(result.processedSize);
setSizes(result.originalSize, result.processedSize);
} catch (err) {
setError(err instanceof Error ? err.message : "Processing failed");
} finally {
setProcessing(false);
}
};
const hasFile = files.length > 0;
return (
<div className="space-y-4">
<div>
<label className="text-xs text-muted-foreground">Overlay Image</label>
<input
ref={overlayInputRef}
type="file"
accept="image/*"
onChange={(e) => setOverlayFile(e.target.files?.[0] ?? null)}
className="hidden"
/>
<button
onClick={() => overlayInputRef.current?.click()}
className="w-full mt-0.5 px-2 py-2 rounded border border-dashed border-border bg-background text-sm text-muted-foreground hover:text-foreground flex items-center justify-center gap-2"
>
<Upload className="h-4 w-4" />
{overlayFile ? overlayFile.name : "Choose overlay image"}
</button>
</div>
<div className="flex gap-2">
<div className="flex-1">
<label className="text-xs text-muted-foreground">X Position</label>
<input type="number" value={x} onChange={(e) => setX(Number(e.target.value))} min={0}
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground" />
</div>
<div className="flex-1">
<label className="text-xs text-muted-foreground">Y Position</label>
<input type="number" value={y} onChange={(e) => setY(Number(e.target.value))} min={0}
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground" />
</div>
</div>
<div>
<div className="flex justify-between items-center">
<label className="text-xs text-muted-foreground">Opacity</label>
<span className="text-xs font-mono text-foreground">{opacity}%</span>
</div>
<input type="range" min={0} max={100} value={opacity} onChange={(e) => setOpacity(Number(e.target.value))} className="w-full mt-1" />
</div>
<div>
<label className="text-xs text-muted-foreground">Blend Mode</label>
<select
value={blendMode}
onChange={(e) => setBlendMode(e.target.value)}
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
>
<option value="over">Normal</option>
<option value="multiply">Multiply</option>
<option value="screen">Screen</option>
<option value="overlay">Overlay</option>
<option value="darken">Darken</option>
<option value="lighten">Lighten</option>
<option value="hard-light">Hard Light</option>
<option value="soft-light">Soft Light</option>
<option value="difference">Difference</option>
<option value="exclusion">Exclusion</option>
</select>
</div>
{error && <p className="text-xs text-red-500">{error}</p>}
{originalSize != null && processedSize != null && (
<div className="text-xs text-muted-foreground space-y-0.5">
<p>Original: {(originalSize / 1024).toFixed(1)} KB</p>
<p>Processed: {(processedSize / 1024).toFixed(1)} KB</p>
</div>
)}
<button
onClick={handleProcess}
disabled={!hasFile || !overlayFile || processing}
className="w-full py-2.5 rounded-lg bg-primary text-primary-foreground font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
>
{processing && <Loader2 className="h-4 w-4 animate-spin" />}
{processing ? "Processing..." : "Compose"}
</button>
{downloadUrl && (
<a href={downloadUrl} download className="w-full py-2.5 rounded-lg border border-primary text-primary font-medium flex items-center justify-center gap-2 hover:bg-primary/5">
<Download className="h-4 w-4" />
Download
</a>
)}
</div>
);
}
@@ -0,0 +1,106 @@
import { useState } from "react";
import { useFileStore } from "@/stores/file-store";
import { useToolProcessor } from "@/hooks/use-tool-processor";
import { Download, Loader2 } from "lucide-react";
export function TextOverlaySettings() {
const { files } = useFileStore();
const { processFiles, processing, error, downloadUrl, originalSize, processedSize } =
useToolProcessor("text-overlay");
const [text, setText] = useState("Your Text Here");
const [fontSize, setFontSize] = useState(48);
const [color, setColor] = useState("#FFFFFF");
const [position, setPosition] = useState<"top" | "center" | "bottom">("bottom");
const [backgroundBox, setBackgroundBox] = useState(false);
const [backgroundColor, setBackgroundColor] = useState("#000000");
const [shadow, setShadow] = useState(true);
const handleProcess = () => {
processFiles(files, { text, fontSize, color, position, backgroundBox, backgroundColor, shadow });
};
const hasFile = files.length > 0;
return (
<div className="space-y-4">
<div>
<label className="text-xs text-muted-foreground">Text</label>
<input
type="text"
value={text}
onChange={(e) => setText(e.target.value)}
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
/>
</div>
<div>
<div className="flex justify-between items-center">
<label className="text-xs text-muted-foreground">Font Size</label>
<span className="text-xs font-mono text-foreground">{fontSize}px</span>
</div>
<input type="range" min={8} max={200} value={fontSize} onChange={(e) => setFontSize(Number(e.target.value))} className="w-full mt-1" />
</div>
<div>
<label className="text-xs text-muted-foreground">Text Color</label>
<input type="color" value={color} onChange={(e) => setColor(e.target.value)} className="w-full mt-0.5 h-8 rounded border border-border" />
</div>
<div>
<label className="text-xs text-muted-foreground">Position</label>
<select
value={position}
onChange={(e) => setPosition(e.target.value as "top" | "center" | "bottom")}
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
>
<option value="top">Top</option>
<option value="center">Center</option>
<option value="bottom">Bottom</option>
</select>
</div>
<label className="flex items-center gap-2 text-sm text-foreground">
<input type="checkbox" checked={shadow} onChange={(e) => setShadow(e.target.checked)} className="rounded" />
Drop Shadow
</label>
<label className="flex items-center gap-2 text-sm text-foreground">
<input type="checkbox" checked={backgroundBox} onChange={(e) => setBackgroundBox(e.target.checked)} className="rounded" />
Background Box
</label>
{backgroundBox && (
<div>
<label className="text-xs text-muted-foreground">Box Color</label>
<input type="color" value={backgroundColor} onChange={(e) => setBackgroundColor(e.target.value)} className="w-full mt-0.5 h-8 rounded border border-border" />
</div>
)}
{error && <p className="text-xs text-red-500">{error}</p>}
{originalSize != null && processedSize != null && (
<div className="text-xs text-muted-foreground space-y-0.5">
<p>Original: {(originalSize / 1024).toFixed(1)} KB</p>
<p>Processed: {(processedSize / 1024).toFixed(1)} KB</p>
</div>
)}
<button
onClick={handleProcess}
disabled={!hasFile || processing || !text}
className="w-full py-2.5 rounded-lg bg-primary text-primary-foreground font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
>
{processing && <Loader2 className="h-4 w-4 animate-spin" />}
{processing ? "Processing..." : "Add Text"}
</button>
{downloadUrl && (
<a href={downloadUrl} download className="w-full py-2.5 rounded-lg border border-primary text-primary font-medium flex items-center justify-center gap-2 hover:bg-primary/5">
<Download className="h-4 w-4" />
Download
</a>
)}
</div>
);
}
@@ -0,0 +1,139 @@
import { useState, useRef } from "react";
import { useFileStore } from "@/stores/file-store";
import { Download, Loader2, Upload } from "lucide-react";
type Position = "center" | "top-left" | "top-right" | "bottom-left" | "bottom-right";
function getToken(): string {
return localStorage.getItem("stirling-token") || "";
}
export function WatermarkImageSettings() {
const { files, processing, error, setProcessing, setError, setProcessedUrl, setSizes, setJobId } = useFileStore();
const [position, setPosition] = useState<Position>("bottom-right");
const [opacity, setOpacity] = useState(50);
const [scale, setScale] = useState(25);
const [watermarkFile, setWatermarkFile] = useState<File | null>(null);
const [downloadUrl, setDownloadUrl] = useState<string | null>(null);
const [originalSize, setOriginalSize] = useState<number | null>(null);
const [processedSize, setProcessedSize] = useState<number | null>(null);
const watermarkInputRef = useRef<HTMLInputElement>(null);
const handleProcess = async () => {
if (files.length === 0 || !watermarkFile) return;
setProcessing(true);
setError(null);
setDownloadUrl(null);
try {
const formData = new FormData();
formData.append("file", files[0]);
formData.append("watermark", watermarkFile);
formData.append("settings", JSON.stringify({ position, opacity, scale }));
const res = await fetch("/api/v1/tools/watermark-image", {
method: "POST",
headers: { Authorization: `Bearer ${getToken()}` },
body: formData,
});
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.error || `Processing failed: ${res.status}`);
}
const result = await res.json();
setJobId(result.jobId);
setProcessedUrl(result.downloadUrl);
setDownloadUrl(result.downloadUrl);
setOriginalSize(result.originalSize);
setProcessedSize(result.processedSize);
setSizes(result.originalSize, result.processedSize);
} catch (err) {
setError(err instanceof Error ? err.message : "Processing failed");
} finally {
setProcessing(false);
}
};
const hasFile = files.length > 0;
return (
<div className="space-y-4">
<div>
<label className="text-xs text-muted-foreground">Watermark Image</label>
<input
ref={watermarkInputRef}
type="file"
accept="image/*"
onChange={(e) => setWatermarkFile(e.target.files?.[0] ?? null)}
className="hidden"
/>
<button
onClick={() => watermarkInputRef.current?.click()}
className="w-full mt-0.5 px-2 py-2 rounded border border-dashed border-border bg-background text-sm text-muted-foreground hover:text-foreground flex items-center justify-center gap-2"
>
<Upload className="h-4 w-4" />
{watermarkFile ? watermarkFile.name : "Choose watermark image"}
</button>
</div>
<div>
<label className="text-xs text-muted-foreground">Position</label>
<select
value={position}
onChange={(e) => setPosition(e.target.value as Position)}
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
>
<option value="center">Center</option>
<option value="top-left">Top Left</option>
<option value="top-right">Top Right</option>
<option value="bottom-left">Bottom Left</option>
<option value="bottom-right">Bottom Right</option>
</select>
</div>
<div>
<div className="flex justify-between items-center">
<label className="text-xs text-muted-foreground">Opacity</label>
<span className="text-xs font-mono text-foreground">{opacity}%</span>
</div>
<input type="range" min={0} max={100} value={opacity} onChange={(e) => setOpacity(Number(e.target.value))} className="w-full mt-1" />
</div>
<div>
<div className="flex justify-between items-center">
<label className="text-xs text-muted-foreground">Scale</label>
<span className="text-xs font-mono text-foreground">{scale}%</span>
</div>
<input type="range" min={5} max={100} value={scale} onChange={(e) => setScale(Number(e.target.value))} className="w-full mt-1" />
</div>
{error && <p className="text-xs text-red-500">{error}</p>}
{originalSize != null && processedSize != null && (
<div className="text-xs text-muted-foreground space-y-0.5">
<p>Original: {(originalSize / 1024).toFixed(1)} KB</p>
<p>Processed: {(processedSize / 1024).toFixed(1)} KB</p>
</div>
)}
<button
onClick={handleProcess}
disabled={!hasFile || !watermarkFile || processing}
className="w-full py-2.5 rounded-lg bg-primary text-primary-foreground font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
>
{processing && <Loader2 className="h-4 w-4 animate-spin" />}
{processing ? "Processing..." : "Apply Watermark"}
</button>
{downloadUrl && (
<a href={downloadUrl} download className="w-full py-2.5 rounded-lg border border-primary text-primary font-medium flex items-center justify-center gap-2 hover:bg-primary/5">
<Download className="h-4 w-4" />
Download
</a>
)}
</div>
);
}
@@ -0,0 +1,110 @@
import { useState } from "react";
import { useFileStore } from "@/stores/file-store";
import { useToolProcessor } from "@/hooks/use-tool-processor";
import { Download, Loader2 } from "lucide-react";
type Position = "center" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "tiled";
export function WatermarkTextSettings() {
const { files } = useFileStore();
const { processFiles, processing, error, downloadUrl, originalSize, processedSize } =
useToolProcessor("watermark-text");
const [text, setText] = useState("Sample Watermark");
const [fontSize, setFontSize] = useState(48);
const [color, setColor] = useState("#000000");
const [opacity, setOpacity] = useState(50);
const [position, setPosition] = useState<Position>("center");
const [rotation, setRotation] = useState(0);
const handleProcess = () => {
processFiles(files, { text, fontSize, color, opacity, position, rotation });
};
const hasFile = files.length > 0;
return (
<div className="space-y-4">
<div>
<label className="text-xs text-muted-foreground">Watermark Text</label>
<input
type="text"
value={text}
onChange={(e) => setText(e.target.value)}
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
/>
</div>
<div>
<div className="flex justify-between items-center">
<label className="text-xs text-muted-foreground">Font Size</label>
<span className="text-xs font-mono text-foreground">{fontSize}px</span>
</div>
<input type="range" min={8} max={200} value={fontSize} onChange={(e) => setFontSize(Number(e.target.value))} className="w-full mt-1" />
</div>
<div className="flex gap-2">
<div className="flex-1">
<label className="text-xs text-muted-foreground">Color</label>
<input type="color" value={color} onChange={(e) => setColor(e.target.value)} className="w-full mt-0.5 h-8 rounded border border-border" />
</div>
<div className="flex-1">
<div className="flex justify-between items-center">
<label className="text-xs text-muted-foreground">Opacity</label>
<span className="text-xs font-mono text-foreground">{opacity}%</span>
</div>
<input type="range" min={0} max={100} value={opacity} onChange={(e) => setOpacity(Number(e.target.value))} className="w-full mt-1" />
</div>
</div>
<div>
<label className="text-xs text-muted-foreground">Position</label>
<select
value={position}
onChange={(e) => setPosition(e.target.value as Position)}
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
>
<option value="center">Center</option>
<option value="top-left">Top Left</option>
<option value="top-right">Top Right</option>
<option value="bottom-left">Bottom Left</option>
<option value="bottom-right">Bottom Right</option>
<option value="tiled">Tiled (Repeating)</option>
</select>
</div>
<div>
<div className="flex justify-between items-center">
<label className="text-xs text-muted-foreground">Rotation</label>
<span className="text-xs font-mono text-foreground">{rotation}&deg;</span>
</div>
<input type="range" min={-180} max={180} value={rotation} onChange={(e) => setRotation(Number(e.target.value))} className="w-full mt-1" />
</div>
{error && <p className="text-xs text-red-500">{error}</p>}
{originalSize != null && processedSize != null && (
<div className="text-xs text-muted-foreground space-y-0.5">
<p>Original: {(originalSize / 1024).toFixed(1)} KB</p>
<p>Processed: {(processedSize / 1024).toFixed(1)} KB</p>
</div>
)}
<button
onClick={handleProcess}
disabled={!hasFile || processing || !text}
className="w-full py-2.5 rounded-lg bg-primary text-primary-foreground font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
>
{processing && <Loader2 className="h-4 w-4 animate-spin" />}
{processing ? "Processing..." : "Add Watermark"}
</button>
{downloadUrl && (
<a href={downloadUrl} download className="w-full py-2.5 rounded-lg border border-primary text-primary font-medium flex items-center justify-center gap-2 hover:bg-primary/5">
<Download className="h-4 w-4" />
Download
</a>
)}
</div>
);
}