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

Keep only the aspect crop math the fal lane uses

Claude12089cf8e4e2
diff --git a/server/services/imageGen/aspectConform.js b/server/services/imageGen/aspectConform.js
index cbb6f39682..b6c351a6a3 100644
--- a/server/services/imageGen/aspectConform.js
+++ b/server/services/imageGen/aspectConform.js
@@ -1,26 +1,15 @@
/**
- * Image Gen — conform a provider's output to the requested aspect ratio.
+ * Image Gen — the crop box that conforms a frame to a requested aspect ratio.
*
- * Codex's built-in image_gen tool takes no size argument: the requested canvas
- * rides the prompt as a hint and the model picks one of its native sizes. Most
- * of the time that lands on the asked-for shape, but not always — a 16:9 music
- * video production came back with 1254x1254 squares (mostly performance
- * close-ups), near-4:3 frames and 1536x1024 3:2 frames mixed in with its
- * 1672x941 widescreen stills, and every clip lane downstream inherited the
- * wrong shape (an OmniHuman lip-sync clip from a square still is 1080x1080).
- *
- * So the provider's completion handler runs the written frame through here
- * before the sidecar is written: a frame within `ASPECT_TOLERANCE` of the
- * requested ratio is left byte-for-byte alone; anything else is cropped to the
- * largest rectangle of the requested ratio, positioned by sharp's attention
- * strategy so a close-up keeps the face rather than a geometric centre. The
- * crop never upscales — it keeps the provider's native resolution on the long
- * side it can afford.
+ * Image backends do not always return the asked-for shape (Codex answered a
+ * 16:9 music video request with 1254x1254 close-ups, near-4:3 and 3:2 frames),
+ * and every clip lane downstream inherits that shape: an OmniHuman lip-sync clip
+ * from a square still is 1080x1080. A frame within `ASPECT_TOLERANCE` of the
+ * requested ratio is left alone; anything else is cropped to the largest
+ * rectangle of that ratio, never upscaled. The fal clip lane applies it to the
+ * frames it uploads (videoGen/falFrameAspect.js).
*/
-import sharp from 'sharp';
-import { atomicWrite } from '../../lib/fileUtils.js';
-
// 2% — 1672x941 (1.7768) is Codex's own 16:9 and must pass untouched against a
// 1.7778 request; the nearest wrong shape it returns (3:2, 1.5) is ~16% off.
export const ASPECT_TOLERANCE = 0.02;
@@ -44,21 +33,3 @@ export function aspectCropSize(srcW, srcH, targetW, targetH, tolerance = ASPECT_
const height = actual > target ? srcH : Math.round(srcW / target);
return { width: Math.max(1, width), height: Math.max(1, height) };
}
-
-/**
- * Crop the PNG at `pngPath` in place to the requested aspect ratio when it is
- * off. Returns `{ from, to }` (`"WxH"` strings) when it cropped, else `null`.
- * A frame sharp cannot read is left for the existing invalid-image paths.
- */
-export async function conformImageAspect(pngPath, targetW, targetH) {
- if (!positive(targetW) || !positive(targetH)) return null;
- const meta = await sharp(pngPath).metadata().catch(() => null);
- const crop = aspectCropSize(meta?.width, meta?.height, Number(targetW), Number(targetH));
- if (!crop) return null;
- const buffer = await sharp(pngPath)
- .resize(crop.width, crop.height, { fit: 'cover', position: sharp.strategy.attention })
- .png()
- .toBuffer();
- await atomicWrite(pngPath, buffer);
- return { from: `${meta.width}x${meta.height}`, to: `${crop.width}x${crop.height}` };
-}
diff --git a/server/services/imageGen/aspectConform.test.js b/server/services/imageGen/aspectConform.test.js
index 2d953d60b0..0204ac86e1 100644
--- a/server/services/imageGen/aspectConform.test.js
+++ b/server/services/imageGen/aspectConform.test.js
@@ -2,8 +2,8 @@ import { describe, it, expect } from 'vitest';
import { aspectCropSize } from './aspectConform.js';
// The crop-box arithmetic, over the shapes Codex actually returned for 16:9
-// production stills. The end-to-end crop (sharp + sidecar) is covered in
-// codex.test.js; this pins the tolerance boundary and both crop directions.
+// production stills. The end-to-end crop is covered in videoGen/fal.test.js;
+// this pins the tolerance boundary and both crop directions.
describe('aspectCropSize', () => {
it.each([
['Codex 16:9 passes untouched', [1672, 941, 1920, 1080], null],