2026-03-23 01:45:58 +08:00
|
|
|
import { Download } from "lucide-react";
|
2026-03-28 16:04:42 +08:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
2026-03-23 01:45:58 +08:00
|
|
|
import { ProgressCard } from "@/components/common/progress-card";
|
2026-03-25 09:27:12 +08:00
|
|
|
import { useToolProcessor } from "@/hooks/use-tool-processor";
|
|
|
|
|
import { useFileStore } from "@/stores/file-store";
|
2026-03-22 04:31:49 +08:00
|
|
|
|
|
|
|
|
const ASPECT_PRESETS = [
|
|
|
|
|
{ label: "1:1 Square", w: 1080, h: 1080 },
|
|
|
|
|
{ label: "16:9 Landscape", w: 1920, h: 1080 },
|
|
|
|
|
{ label: "9:16 Portrait", w: 1080, h: 1920 },
|
|
|
|
|
{ label: "4:3 Standard", w: 1440, h: 1080 },
|
|
|
|
|
{ label: "3:2 Photo", w: 1620, h: 1080 },
|
|
|
|
|
{ label: "Custom", w: 0, h: 0 },
|
|
|
|
|
];
|
|
|
|
|
|
2026-03-28 16:04:42 +08:00
|
|
|
export interface SmartCropControlsProps {
|
|
|
|
|
onChange?: (settings: Record<string, unknown>) => void;
|
|
|
|
|
}
|
2026-03-22 04:31:49 +08:00
|
|
|
|
2026-03-28 16:04:42 +08:00
|
|
|
export function SmartCropControls({ onChange }: SmartCropControlsProps) {
|
2026-03-22 04:31:49 +08:00
|
|
|
const [width, setWidth] = useState("1080");
|
|
|
|
|
const [height, setHeight] = useState("1080");
|
|
|
|
|
const [preset, setPreset] = useState("1:1 Square");
|
|
|
|
|
|
2026-03-28 16:04:42 +08:00
|
|
|
const onChangeRef = useRef(onChange);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
onChangeRef.current = onChange;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
onChangeRef.current?.({ width: Number(width), height: Number(height) });
|
|
|
|
|
}, [width, height]);
|
|
|
|
|
|
2026-03-22 04:31:49 +08:00
|
|
|
const handlePreset = (label: string) => {
|
|
|
|
|
setPreset(label);
|
|
|
|
|
const p = ASPECT_PRESETS.find((a) => a.label === label);
|
|
|
|
|
if (p && p.w > 0) {
|
|
|
|
|
setWidth(String(p.w));
|
|
|
|
|
setHeight(String(p.h));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
{/* Aspect ratio preset */}
|
|
|
|
|
<div>
|
2026-03-26 01:10:13 +08:00
|
|
|
<label htmlFor="smart-crop-preset" className="text-sm font-medium text-muted-foreground">
|
|
|
|
|
Target Aspect Ratio
|
|
|
|
|
</label>
|
2026-03-22 04:31:49 +08:00
|
|
|
<select
|
2026-03-26 01:10:13 +08:00
|
|
|
id="smart-crop-preset"
|
2026-03-22 04:31:49 +08:00
|
|
|
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">
|
2026-03-26 01:10:13 +08:00
|
|
|
<label htmlFor="smart-crop-width" className="text-xs text-muted-foreground">
|
|
|
|
|
Width (px)
|
|
|
|
|
</label>
|
2026-03-22 04:31:49 +08:00
|
|
|
<input
|
2026-03-26 01:10:13 +08:00
|
|
|
id="smart-crop-width"
|
2026-03-22 04:31:49 +08:00
|
|
|
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">
|
2026-03-26 01:10:13 +08:00
|
|
|
<label htmlFor="smart-crop-height" className="text-xs text-muted-foreground">
|
|
|
|
|
Height (px)
|
|
|
|
|
</label>
|
2026-03-22 04:31:49 +08:00
|
|
|
<input
|
2026-03-26 01:10:13 +08:00
|
|
|
id="smart-crop-height"
|
2026-03-22 04:31:49 +08:00
|
|
|
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>
|
|
|
|
|
|
|
|
|
|
{/* Info */}
|
|
|
|
|
<p className="text-[10px] text-muted-foreground">
|
2026-03-25 09:27:12 +08:00
|
|
|
Uses entropy-based attention detection to find the most interesting region of the image and
|
|
|
|
|
crops to it.
|
2026-03-22 04:31:49 +08:00
|
|
|
</p>
|
2026-03-28 16:04:42 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function SmartCropSettings() {
|
|
|
|
|
const { files } = useFileStore();
|
|
|
|
|
const { processFiles, processing, error, downloadUrl, originalSize, processedSize, progress } =
|
|
|
|
|
useToolProcessor("smart-crop");
|
|
|
|
|
|
|
|
|
|
const [settings, setSettings] = useState<Record<string, unknown>>({});
|
|
|
|
|
|
|
|
|
|
const handleProcess = () => {
|
|
|
|
|
const w = Number(settings.width);
|
|
|
|
|
const h = Number(settings.height);
|
|
|
|
|
if (w > 0 && h > 0) {
|
|
|
|
|
processFiles(files, { width: w, height: h });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const hasFile = files.length > 0;
|
|
|
|
|
const canProcess = Number(settings.width) > 0 && Number(settings.height) > 0;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-4">
|
|
|
|
|
<SmartCropControls onChange={setSettings} />
|
2026-03-22 04:31:49 +08:00
|
|
|
|
|
|
|
|
{/* Error */}
|
|
|
|
|
{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 */}
|
2026-03-23 01:45:58 +08:00
|
|
|
{processing ? (
|
|
|
|
|
<ProgressCard
|
|
|
|
|
active={processing}
|
|
|
|
|
phase={progress.phase === "idle" ? "uploading" : progress.phase}
|
|
|
|
|
label="Smart cropping"
|
|
|
|
|
stage={progress.stage}
|
|
|
|
|
percent={progress.percent}
|
|
|
|
|
elapsed={progress.elapsed}
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<button
|
2026-03-26 01:10:13 +08:00
|
|
|
type="button"
|
2026-03-28 11:19:09 +08:00
|
|
|
data-testid="smart-crop-submit"
|
2026-03-23 01:45:58 +08:00
|
|
|
onClick={handleProcess}
|
|
|
|
|
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"
|
|
|
|
|
>
|
|
|
|
|
Smart Crop
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
2026-03-22 04:31:49 +08:00
|
|
|
|
|
|
|
|
{/* Download */}
|
|
|
|
|
{downloadUrl && (
|
|
|
|
|
<a
|
|
|
|
|
href={downloadUrl}
|
|
|
|
|
download
|
2026-03-28 11:19:09 +08:00
|
|
|
data-testid="smart-crop-download"
|
2026-03-22 04:31:49 +08:00
|
|
|
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>
|
|
|
|
|
);
|
|
|
|
|
}
|