Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex): clamp uploaded init-image dimensions to server caps

[._.]/ Adam Eivy27a26d5a64f4
diff --git a/client/src/lib/imageGenResolutions.js b/client/src/lib/imageGenResolutions.js
index b771cccac4..95e59c70ac 100644
--- a/client/src/lib/imageGenResolutions.js
+++ b/client/src/lib/imageGenResolutions.js
@@ -45,6 +45,31 @@ export const RESOLUTIONS = [
{ label: '2880×2880 (4K square)', w: 2880, h: 2880, compatible: ['codex'] },
];
+// Mirror of server/lib/validation.js MAX_IMAGE_EDGE / MAX_IMAGE_PIXELS — the
+// gpt-image-2 / image-gen route ceiling (each edge ≤ 3840, total ≤ 8,294,400).
+// Keep in sync with that module.
+export const MAX_IMAGE_EDGE = 3840;
+export const MAX_IMAGE_PIXELS = 8_294_400;
+
+// Clamp arbitrary source dimensions (e.g. an uploaded photo) down to the
+// server's per-edge and total-pixel caps, preserving aspect ratio and snapping
+// to a multiple of 8 (runner-friendly). Returns integer { width, height } ≥ 64,
+// or null for non-finite/non-positive input. Already-valid sizes pass through
+// unchanged. Use this before seeding the resolution from any user-supplied image
+// so Generate doesn't 400 on imageEdgeSchema / refineImagePixelCap.
+export function clampImageDimensions(width, height) {
+ let w = Number(width);
+ let h = Number(height);
+ if (!Number.isFinite(w) || !Number.isFinite(h) || w < 1 || h < 1) return null;
+ const edgeScale = Math.min(1, MAX_IMAGE_EDGE / Math.max(w, h));
+ w *= edgeScale; h *= edgeScale;
+ const pixelScale = Math.min(1, Math.sqrt(MAX_IMAGE_PIXELS / (w * h)));
+ w *= pixelScale; h *= pixelScale;
+ // Floor to a multiple of 8 so the snap never pushes us back over a cap.
+ const snap = (n) => Math.max(64, Math.floor(n / 8) * 8);
+ return { width: snap(w), height: snap(h) };
+}
+
// Flux 1 (mflux/diffusers, `dev` / `schnell`) has no `runner` field — it's
// the fallback for local mode when nothing more specific matches.
const NATIVE_RUNNER_KEYS = new Set([
diff --git a/client/src/lib/imageGenResolutions.test.js b/client/src/lib/imageGenResolutions.test.js
new file mode 100644
index 0000000000..547df33155
--- /dev/null
+++ b/client/src/lib/imageGenResolutions.test.js
@@ -0,0 +1,40 @@
+import { describe, it, expect } from 'vitest';
+import { clampImageDimensions, MAX_IMAGE_EDGE, MAX_IMAGE_PIXELS } from './imageGenResolutions';
+
+describe('clampImageDimensions', () => {
+ const underCaps = (d) =>
+ d.width <= MAX_IMAGE_EDGE && d.height <= MAX_IMAGE_EDGE && d.width * d.height <= MAX_IMAGE_PIXELS;
+
+ it('passes already-valid sizes through (snapped to multiples of 8)', () => {
+ expect(clampImageDimensions(1024, 1024)).toEqual({ width: 1024, height: 1024 });
+ expect(clampImageDimensions(1216, 832)).toEqual({ width: 1216, height: 832 });
+ });
+
+ it('clamps a large phone photo under the edge AND pixel caps, preserving aspect', () => {
+ // 4032×3024 (12MP, 4:3) — over the 8.29MP pixel cap.
+ const d = clampImageDimensions(4032, 3024);
+ expect(underCaps(d)).toBe(true);
+ // aspect ratio preserved within rounding tolerance
+ expect(Math.abs(d.width / d.height - 4032 / 3024)).toBeLessThan(0.02);
+ expect(d.width % 8).toBe(0);
+ expect(d.height % 8).toBe(0);
+ });
+
+ it('caps the long edge at MAX_IMAGE_EDGE for an extreme aspect ratio', () => {
+ const d = clampImageDimensions(8000, 1000);
+ expect(d.width).toBeLessThanOrEqual(MAX_IMAGE_EDGE);
+ expect(underCaps(d)).toBe(true);
+ });
+
+ it('never returns a dimension below 64', () => {
+ const d = clampImageDimensions(10000, 5);
+ expect(d.width).toBeGreaterThanOrEqual(64);
+ expect(d.height).toBeGreaterThanOrEqual(64);
+ });
+
+ it('returns null for non-finite or non-positive input', () => {
+ expect(clampImageDimensions(0, 100)).toBeNull();
+ expect(clampImageDimensions(NaN, 100)).toBeNull();
+ expect(clampImageDimensions(undefined, undefined)).toBeNull();
+ });
+});
diff --git a/client/src/pages/ImageGen.jsx b/client/src/pages/ImageGen.jsx
index c898877b39..a872025394 100644
--- a/client/src/pages/ImageGen.jsx
+++ b/client/src/pages/ImageGen.jsx
@@ -37,6 +37,7 @@ import {
} from 'lucide-react';
import { composeStyledPrompt } from '../lib/composeStyledPrompt';
import { deriveAvailableBackends, IMAGE_GEN_MODE, isI2iCapableMode, pickI2iMode } from '../lib/imageGenBackends';
+import { clampImageDimensions } from '../lib/imageGenResolutions';
import { DEFAULT_NEGATIVE_PROMPT } from '../lib/imageGenDefaults';
import { resolveCleanersFromConfig } from '../lib/imageCleaners';
import toast from '../components/ui/Toast';
@@ -493,9 +494,11 @@ export default function ImageGen() {
const file = await normalizeImageOrientation(raw);
revokeIfBlob(initImage.previewUrl);
setInitImage({ source: 'upload', file, name: file.name, previewUrl: URL.createObjectURL(file) });
- // Default the output resolution to the uploaded image's dimensions.
+ // Default the output resolution to the uploaded image's dimensions, clamped
+ // to the server's edge/pixel caps so a large phone photo doesn't 400 on Generate.
const dims = await readImageDimensions(file);
- if (dims) { setWidth(dims.width); setHeight(dims.height); }
+ const clamped = dims && clampImageDimensions(dims.width, dims.height);
+ if (clamped) { setWidth(clamped.width); setHeight(clamped.height); }
};
const handleClearInitImage = () => {
revokeIfBlob(initImage.previewUrl);