mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: add "Crop to Content" mode to smart crop tool
Adds a new mode that trims uniform-color borders around the subject, like GIMP's "Crop to Content." Includes configurable tolerance threshold and optional pad-to-square with target size for e-commerce workflows. The original attention-based crop is preserved as "Focus Crop" mode. Closes #7
This commit is contained in:
@@ -4,27 +4,67 @@ import { z } from "zod";
|
|||||||
import { createToolRoute } from "../tool-factory.js";
|
import { createToolRoute } from "../tool-factory.js";
|
||||||
|
|
||||||
const settingsSchema = z.object({
|
const settingsSchema = z.object({
|
||||||
width: z.number().int().positive(),
|
mode: z.enum(["attention", "content"]).default("attention"),
|
||||||
height: z.number().int().positive(),
|
// Attention mode: resize to target dimensions using subject detection
|
||||||
|
width: z.number().int().positive().optional(),
|
||||||
|
height: z.number().int().positive().optional(),
|
||||||
|
// Content mode: trim uniform borders, optionally pad to square
|
||||||
|
threshold: z.number().int().min(0).max(255).default(30),
|
||||||
|
padToSquare: z.boolean().default(false),
|
||||||
|
padColor: z.string().default("#ffffff"),
|
||||||
|
targetSize: z.number().int().positive().optional(),
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Smart crop using Sharp's attention-based strategy.
|
* Smart crop with two modes:
|
||||||
* Uses entropy/saliency detection to find the most interesting region.
|
* - "attention": Sharp's entropy/saliency detection to crop to the most interesting region
|
||||||
* No Python needed.
|
* - "content": Trims uniform-color borders (like GIMP's "Crop to Content"),
|
||||||
|
* optionally pads to a square at a target size
|
||||||
*/
|
*/
|
||||||
export function registerSmartCrop(app: FastifyInstance) {
|
export function registerSmartCrop(app: FastifyInstance) {
|
||||||
createToolRoute(app, {
|
createToolRoute(app, {
|
||||||
toolId: "smart-crop",
|
toolId: "smart-crop",
|
||||||
settingsSchema,
|
settingsSchema,
|
||||||
process: async (inputBuffer, settings, filename) => {
|
process: async (inputBuffer, settings, filename) => {
|
||||||
const result = await sharp(inputBuffer)
|
let result: Buffer;
|
||||||
.resize(settings.width, settings.height, {
|
|
||||||
fit: "cover",
|
if (settings.mode === "content") {
|
||||||
position: sharp.strategy.attention,
|
// Crop to content: trim uniform borders
|
||||||
})
|
const pipeline = sharp(inputBuffer).trim({ threshold: settings.threshold });
|
||||||
.png()
|
let trimmed = await pipeline.toBuffer({ resolveWithObject: true });
|
||||||
.toBuffer();
|
|
||||||
|
if (settings.padToSquare || settings.targetSize) {
|
||||||
|
const meta = await sharp(trimmed.data).metadata();
|
||||||
|
const w = meta.width ?? 1;
|
||||||
|
const h = meta.height ?? 1;
|
||||||
|
const target = settings.targetSize || Math.max(w, h);
|
||||||
|
const padR = Math.round(parseInt(settings.padColor.slice(1, 3), 16));
|
||||||
|
const padG = Math.round(parseInt(settings.padColor.slice(3, 5), 16));
|
||||||
|
const padB = Math.round(parseInt(settings.padColor.slice(5, 7), 16));
|
||||||
|
|
||||||
|
trimmed = await sharp(trimmed.data)
|
||||||
|
.resize({
|
||||||
|
width: target,
|
||||||
|
height: target,
|
||||||
|
fit: "contain",
|
||||||
|
background: { r: padR, g: padG, b: padB, alpha: 1 },
|
||||||
|
})
|
||||||
|
.toBuffer({ resolveWithObject: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
result = trimmed.data;
|
||||||
|
} else {
|
||||||
|
// Attention mode: resize to target using subject detection
|
||||||
|
const w = settings.width ?? 1080;
|
||||||
|
const h = settings.height ?? 1080;
|
||||||
|
result = await sharp(inputBuffer)
|
||||||
|
.resize(w, h, {
|
||||||
|
fit: "cover",
|
||||||
|
position: sharp.strategy.attention,
|
||||||
|
})
|
||||||
|
.png()
|
||||||
|
.toBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
const outputFilename = `${filename.replace(/\.[^.]+$/, "")}_smartcrop.png`;
|
const outputFilename = `${filename.replace(/\.[^.]+$/, "")}_smartcrop.png`;
|
||||||
return { buffer: result, filename: outputFilename, contentType: "image/png" };
|
return { buffer: result, filename: outputFilename, contentType: "image/png" };
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { Download } from "lucide-react";
|
import { useState } from "react";
|
||||||
import { useEffect, useRef, useState } from "react";
|
|
||||||
import { ProgressCard } from "@/components/common/progress-card";
|
import { ProgressCard } from "@/components/common/progress-card";
|
||||||
import { useToolProcessor } from "@/hooks/use-tool-processor";
|
import { useToolProcessor } from "@/hooks/use-tool-processor";
|
||||||
import { useFileStore } from "@/stores/file-store";
|
import { useFileStore } from "@/stores/file-store";
|
||||||
|
|
||||||
|
type Mode = "content" | "attention";
|
||||||
|
|
||||||
const ASPECT_PRESETS = [
|
const ASPECT_PRESETS = [
|
||||||
{ label: "1:1 Square", w: 1080, h: 1080 },
|
{ label: "1:1 Square", w: 1080, h: 1080 },
|
||||||
{ label: "16:9 Landscape", w: 1920, h: 1080 },
|
{ label: "16:9 Landscape", w: 1920, h: 1080 },
|
||||||
@@ -18,18 +19,47 @@ export interface SmartCropControlsProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function SmartCropControls({ onChange }: SmartCropControlsProps) {
|
export function SmartCropControls({ onChange }: SmartCropControlsProps) {
|
||||||
|
const [mode, setMode] = useState<Mode>("content");
|
||||||
|
|
||||||
|
// Attention mode state
|
||||||
const [width, setWidth] = useState("1080");
|
const [width, setWidth] = useState("1080");
|
||||||
const [height, setHeight] = useState("1080");
|
const [height, setHeight] = useState("1080");
|
||||||
const [preset, setPreset] = useState("1:1 Square");
|
const [preset, setPreset] = useState("1:1 Square");
|
||||||
|
|
||||||
const onChangeRef = useRef(onChange);
|
// Content mode state
|
||||||
useEffect(() => {
|
const [threshold, setThreshold] = useState(30);
|
||||||
onChangeRef.current = onChange;
|
const [padToSquare, setPadToSquare] = useState(false);
|
||||||
});
|
const [padColor, setPadColor] = useState("#ffffff");
|
||||||
|
const [targetSize, setTargetSize] = useState("1000");
|
||||||
|
|
||||||
useEffect(() => {
|
const emit = (overrides: Record<string, unknown> = {}) => {
|
||||||
onChangeRef.current?.({ width: Number(width), height: Number(height) });
|
if (mode === "content") {
|
||||||
}, [width, height]);
|
onChange?.({
|
||||||
|
mode: "content",
|
||||||
|
threshold,
|
||||||
|
padToSquare,
|
||||||
|
padColor,
|
||||||
|
...(padToSquare ? { targetSize: Number(targetSize) } : {}),
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
onChange?.({
|
||||||
|
mode: "attention",
|
||||||
|
width: Number(width),
|
||||||
|
height: Number(height),
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleModeChange = (m: Mode) => {
|
||||||
|
setMode(m);
|
||||||
|
if (m === "content") {
|
||||||
|
onChange?.({ mode: "content", threshold, padToSquare, padColor });
|
||||||
|
} else {
|
||||||
|
onChange?.({ mode: "attention", width: Number(width), height: Number(height) });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handlePreset = (label: string) => {
|
const handlePreset = (label: string) => {
|
||||||
setPreset(label);
|
setPreset(label);
|
||||||
@@ -37,109 +67,236 @@ export function SmartCropControls({ onChange }: SmartCropControlsProps) {
|
|||||||
if (p && p.w > 0) {
|
if (p && p.w > 0) {
|
||||||
setWidth(String(p.w));
|
setWidth(String(p.w));
|
||||||
setHeight(String(p.h));
|
setHeight(String(p.h));
|
||||||
|
emit({ width: p.w, height: p.h });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<div className="space-y-4">
|
||||||
{/* Aspect ratio preset */}
|
{/* Mode toggle */}
|
||||||
<div>
|
<div>
|
||||||
<label htmlFor="smart-crop-preset" className="text-sm font-medium text-muted-foreground">
|
<label className="text-sm font-medium text-muted-foreground">Mode</label>
|
||||||
Target Aspect Ratio
|
<div className="flex gap-1 mt-1">
|
||||||
</label>
|
<button
|
||||||
<select
|
type="button"
|
||||||
id="smart-crop-preset"
|
onClick={() => handleModeChange("content")}
|
||||||
value={preset}
|
className={`flex-1 py-2 text-sm rounded-lg font-medium transition-colors ${
|
||||||
onChange={(e) => handlePreset(e.target.value)}
|
mode === "content"
|
||||||
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
|
? "bg-primary text-primary-foreground"
|
||||||
>
|
: "bg-muted text-muted-foreground hover:text-foreground"
|
||||||
{ASPECT_PRESETS.map((p) => (
|
}`}
|
||||||
<option key={p.label} value={p.label}>
|
>
|
||||||
{p.label}
|
Crop to Content
|
||||||
</option>
|
</button>
|
||||||
))}
|
<button
|
||||||
</select>
|
type="button"
|
||||||
</div>
|
onClick={() => handleModeChange("attention")}
|
||||||
|
className={`flex-1 py-2 text-sm rounded-lg font-medium transition-colors ${
|
||||||
{/* Width / Height */}
|
mode === "attention"
|
||||||
<div className="flex gap-2">
|
? "bg-primary text-primary-foreground"
|
||||||
<div className="flex-1">
|
: "bg-muted text-muted-foreground hover:text-foreground"
|
||||||
<label htmlFor="smart-crop-width" className="text-xs text-muted-foreground">
|
}`}
|
||||||
Width (px)
|
>
|
||||||
</label>
|
Focus Crop
|
||||||
<input
|
</button>
|
||||||
id="smart-crop-width"
|
|
||||||
type="number"
|
|
||||||
value={width}
|
|
||||||
onChange={(e) => {
|
|
||||||
setWidth(e.target.value);
|
|
||||||
setPreset("Custom");
|
|
||||||
}}
|
|
||||||
min={1}
|
|
||||||
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 htmlFor="smart-crop-height" className="text-xs text-muted-foreground">
|
|
||||||
Height (px)
|
|
||||||
</label>
|
|
||||||
<input
|
|
||||||
id="smart-crop-height"
|
|
||||||
type="number"
|
|
||||||
value={height}
|
|
||||||
onChange={(e) => {
|
|
||||||
setHeight(e.target.value);
|
|
||||||
setPreset("Custom");
|
|
||||||
}}
|
|
||||||
min={1}
|
|
||||||
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>
|
||||||
|
|
||||||
{/* Info */}
|
{mode === "content" ? (
|
||||||
<p className="text-[10px] text-muted-foreground">
|
<>
|
||||||
Uses entropy-based attention detection to find the most interesting region of the image and
|
{/* Threshold */}
|
||||||
crops to it.
|
<div>
|
||||||
</p>
|
<div className="flex justify-between items-center">
|
||||||
|
<label htmlFor="trim-threshold" className="text-xs text-muted-foreground">
|
||||||
|
Tolerance
|
||||||
|
</label>
|
||||||
|
<span className="text-xs text-muted-foreground tabular-nums">{threshold}</span>
|
||||||
|
</div>
|
||||||
|
<input
|
||||||
|
id="trim-threshold"
|
||||||
|
type="range"
|
||||||
|
min={0}
|
||||||
|
max={128}
|
||||||
|
value={threshold}
|
||||||
|
onChange={(e) => {
|
||||||
|
const v = Number(e.target.value);
|
||||||
|
setThreshold(v);
|
||||||
|
emit({ threshold: v });
|
||||||
|
}}
|
||||||
|
className="w-full mt-1"
|
||||||
|
/>
|
||||||
|
<p className="text-[10px] text-muted-foreground mt-0.5">
|
||||||
|
How different a border pixel can be from the edge color and still be trimmed. Higher =
|
||||||
|
more aggressive.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Pad to square */}
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<input
|
||||||
|
id="pad-square"
|
||||||
|
type="checkbox"
|
||||||
|
checked={padToSquare}
|
||||||
|
onChange={(e) => {
|
||||||
|
setPadToSquare(e.target.checked);
|
||||||
|
emit({ padToSquare: e.target.checked });
|
||||||
|
}}
|
||||||
|
className="rounded border-border"
|
||||||
|
/>
|
||||||
|
<label htmlFor="pad-square" className="text-sm text-foreground">
|
||||||
|
Pad to square
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{padToSquare && (
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<div className="flex-1">
|
||||||
|
<label htmlFor="target-size" className="text-xs text-muted-foreground">
|
||||||
|
Target size (px)
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="target-size"
|
||||||
|
type="number"
|
||||||
|
value={targetSize}
|
||||||
|
onChange={(e) => {
|
||||||
|
setTargetSize(e.target.value);
|
||||||
|
emit({ targetSize: Number(e.target.value) });
|
||||||
|
}}
|
||||||
|
min={1}
|
||||||
|
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label htmlFor="pad-color" className="text-xs text-muted-foreground">
|
||||||
|
Pad color
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="pad-color"
|
||||||
|
type="color"
|
||||||
|
value={padColor}
|
||||||
|
onChange={(e) => {
|
||||||
|
setPadColor(e.target.value);
|
||||||
|
emit({ padColor: e.target.value });
|
||||||
|
}}
|
||||||
|
className="w-12 h-[34px] mt-0.5 rounded border border-border bg-background cursor-pointer"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<p className="text-[10px] text-muted-foreground">
|
||||||
|
Trims uniform-color borders around the subject, like GIMP's "Crop to Content." Enable
|
||||||
|
"Pad to square" to produce e-commerce ready images.
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
{/* Aspect ratio preset */}
|
||||||
|
<div>
|
||||||
|
<label
|
||||||
|
htmlFor="smart-crop-preset"
|
||||||
|
className="text-sm font-medium text-muted-foreground"
|
||||||
|
>
|
||||||
|
Target Aspect Ratio
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
id="smart-crop-preset"
|
||||||
|
value={preset}
|
||||||
|
onChange={(e) => handlePreset(e.target.value)}
|
||||||
|
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
|
||||||
|
>
|
||||||
|
{ASPECT_PRESETS.map((p) => (
|
||||||
|
<option key={p.label} value={p.label}>
|
||||||
|
{p.label}
|
||||||
|
</option>
|
||||||
|
))}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Width / Height */}
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<div className="flex-1">
|
||||||
|
<label htmlFor="smart-crop-width" className="text-xs text-muted-foreground">
|
||||||
|
Width (px)
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="smart-crop-width"
|
||||||
|
type="number"
|
||||||
|
value={width}
|
||||||
|
onChange={(e) => {
|
||||||
|
setWidth(e.target.value);
|
||||||
|
setPreset("Custom");
|
||||||
|
emit({ width: Number(e.target.value) });
|
||||||
|
}}
|
||||||
|
min={1}
|
||||||
|
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 htmlFor="smart-crop-height" className="text-xs text-muted-foreground">
|
||||||
|
Height (px)
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
id="smart-crop-height"
|
||||||
|
type="number"
|
||||||
|
value={height}
|
||||||
|
onChange={(e) => {
|
||||||
|
setHeight(e.target.value);
|
||||||
|
setPreset("Custom");
|
||||||
|
emit({ height: Number(e.target.value) });
|
||||||
|
}}
|
||||||
|
min={1}
|
||||||
|
className="w-full mt-0.5 px-2 py-1.5 rounded border border-border bg-background text-sm text-foreground"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p className="text-[10px] text-muted-foreground">
|
||||||
|
Uses entropy-based attention detection to find the most interesting region and crops to
|
||||||
|
it. Good for thumbnails and social media images.
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function SmartCropSettings() {
|
export function SmartCropSettings() {
|
||||||
const { files } = useFileStore();
|
const { files } = useFileStore();
|
||||||
const { processFiles, processing, error, downloadUrl, originalSize, processedSize, progress } =
|
const { processFiles, processAllFiles, processing, error, progress } =
|
||||||
useToolProcessor("smart-crop");
|
useToolProcessor("smart-crop");
|
||||||
|
|
||||||
const [settings, setSettings] = useState<Record<string, unknown>>({});
|
const [settings, setSettings] = useState<Record<string, unknown>>({
|
||||||
|
mode: "content",
|
||||||
|
threshold: 30,
|
||||||
|
padToSquare: false,
|
||||||
|
padColor: "#ffffff",
|
||||||
|
});
|
||||||
|
|
||||||
const handleProcess = () => {
|
const handleProcess = () => {
|
||||||
const w = Number(settings.width);
|
if (files.length > 1) {
|
||||||
const h = Number(settings.height);
|
processAllFiles(files, settings);
|
||||||
if (w > 0 && h > 0) {
|
} else {
|
||||||
processFiles(files, { width: w, height: h });
|
processFiles(files, settings);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const hasFile = files.length > 0;
|
const hasFile = files.length > 0;
|
||||||
const canProcess = Number(settings.width) > 0 && Number(settings.height) > 0;
|
const mode = settings.mode as string;
|
||||||
|
const canProcess =
|
||||||
|
mode === "content" || (Number(settings.width) > 0 && Number(settings.height) > 0);
|
||||||
|
|
||||||
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (hasFile && canProcess && !processing) handleProcess();
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-4">
|
<form onSubmit={handleSubmit} className="space-y-4">
|
||||||
<SmartCropControls onChange={setSettings} />
|
<SmartCropControls onChange={setSettings} />
|
||||||
|
|
||||||
{/* Error */}
|
|
||||||
{error && <p className="text-xs text-red-500">{error}</p>}
|
{error && <p className="text-xs text-red-500">{error}</p>}
|
||||||
|
|
||||||
{/* Size info */}
|
|
||||||
{originalSize != null && processedSize != null && (
|
|
||||||
<div className="text-xs text-muted-foreground space-y-0.5">
|
|
||||||
<p>Original: {(originalSize / 1024).toFixed(1)} KB</p>
|
|
||||||
<p>Cropped: {(processedSize / 1024).toFixed(1)} KB</p>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{/* Process button */}
|
|
||||||
{processing ? (
|
{processing ? (
|
||||||
<ProgressCard
|
<ProgressCard
|
||||||
active={processing}
|
active={processing}
|
||||||
@@ -151,28 +308,14 @@ export function SmartCropSettings() {
|
|||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="submit"
|
||||||
data-testid="smart-crop-submit"
|
data-testid="smart-crop-submit"
|
||||||
onClick={handleProcess}
|
|
||||||
disabled={!hasFile || !canProcess || processing}
|
disabled={!hasFile || !canProcess || 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"
|
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"
|
||||||
>
|
>
|
||||||
Smart Crop
|
{mode === "content" ? "Crop to Content" : "Smart Crop"}
|
||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
|
</form>
|
||||||
{/* Download */}
|
|
||||||
{downloadUrl && (
|
|
||||||
<a
|
|
||||||
href={downloadUrl}
|
|
||||||
download
|
|
||||||
data-testid="smart-crop-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>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user