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

fix(videogen): honour EXIF orientation when fitting a Reactor starting frame

Adam Eivye66e9a49418a
diff --git a/server/lib/reactorStartingFrame.js b/server/lib/reactorStartingFrame.js
index 190d3dea76..7cdf782ad1 100644
--- a/server/lib/reactorStartingFrame.js
+++ b/server/lib/reactorStartingFrame.js
@@ -24,6 +24,16 @@
* default canvas — reactor accepts more container formats than sharp does, and
* refusing a render PortOS merely failed to measure would be worse than letting
* reactor fit it.
+ *
+ * EXIF orientation is load-bearing here, not a refinement. A phone camera
+ * writes its sensor buffer unrotated and records the display rotation in an
+ * EXIF tag, so a portrait iPhone photo is stored 4032x3024 — LANDSCAPE — with
+ * `orientation: 6`. `sharp.metadata()` reports those raw dimensions no matter
+ * how it was constructed, so measuring without consulting the tag derives a
+ * landscape canvas for a portrait photo (the exact failure this module exists
+ * to prevent), and cropping without applying it centre-crops a sideways
+ * buffer. Both halves must honour it: `readImageSize` swaps the axes, and the
+ * resize decodes through `autoOrient`.
*/
import { rm } from 'node:fs/promises';
import sharp from 'sharp';
@@ -31,14 +41,26 @@ import {
REACTOR_DEFAULT_ASPECT, nearestReactorAspect, reactorCanvas,
} from './reactorVideoClip.js';
-/** `{ width, height }` of an image, or `null` when it could not be measured. */
+/** EXIF orientations 5-8 transpose the image, so its displayed axes are swapped. */
+const EXIF_SWAPS_AXES = (orientation) => Number(orientation) >= 5 && Number(orientation) <= 8;
+
+/**
+ * An image's DISPLAYED size — `{ width, height, oriented }` — or `null` when it
+ * could not be measured. `oriented` is true when EXIF asks for a transform, so
+ * a caller knows the bytes on disk are not what a viewer sees.
+ */
export const readImageSize = async (path) => {
const meta = await sharp(path).metadata().catch(() => null);
- const width = Number(meta?.width);
- const height = Number(meta?.height);
- return Number.isFinite(width) && Number.isFinite(height) && width > 0 && height > 0
- ? { width, height }
- : null;
+ const raw = { width: Number(meta?.width), height: Number(meta?.height) };
+ if (!Number.isFinite(raw.width) || !Number.isFinite(raw.height) || raw.width <= 0 || raw.height <= 0) return null;
+ const swap = EXIF_SWAPS_AXES(meta?.orientation);
+ return {
+ width: swap ? raw.height : raw.width,
+ height: swap ? raw.width : raw.height,
+ // Orientation 1 is the identity; anything else (including the flips, 2-4)
+ // still needs the re-encode, so this is not just the axis-swap set.
+ oriented: Number(meta?.orientation) > 1,
+ };
};
/**
@@ -61,10 +83,13 @@ export async function prepareReactorStartingFrame(sourceImagePath, requestedAspe
|| (size ? nearestReactorAspect(size.width, size.height) : REACTOR_DEFAULT_ASPECT);
const canvas = reactorCanvas(aspect);
const passthrough = { aspect, canvas, framePath: sourceImagePath, fittedPath: null };
- // Unmeasurable, or already exactly the canvas: nothing to gain from a re-encode.
- if (!size || (size.width === canvas.width && size.height === canvas.height)) return passthrough;
+ // Unmeasurable, or already exactly the canvas: nothing to gain from a
+ // re-encode. An image awaiting an EXIF transform is NOT "already the canvas"
+ // even when its displayed size matches — uploading those bytes hands reactor
+ // a sideways frame.
+ if (!size || (!size.oriented && size.width === canvas.width && size.height === canvas.height)) return passthrough;
const fittedPath = `${outputPath}.start.png`;
- const written = await sharp(sourceImagePath)
+ const written = await sharp(sourceImagePath, { autoOrient: true })
.resize({ width: canvas.width, height: canvas.height, fit: 'cover', position: 'centre' })
.png()
.toFile(fittedPath)
diff --git a/server/lib/reactorStartingFrame.test.js b/server/lib/reactorStartingFrame.test.js
index 2d1a5ea2c0..ab4f6b77e3 100644
--- a/server/lib/reactorStartingFrame.test.js
+++ b/server/lib/reactorStartingFrame.test.js
@@ -11,6 +11,15 @@ const writeImage = async (name, width, height) => {
await sharp({ create: { width, height, channels: 3, background: '#204080' } }).png().toFile(path);
return path;
};
+// A phone camera writes its sensor buffer UNROTATED and records the display
+// rotation in EXIF, so a portrait photo lands on disk as landscape pixels plus
+// `orientation: 6`.
+const writePhonePhoto = async (name, rawWidth, rawHeight, orientation) => {
+ const path = join(dir, name);
+ await sharp({ create: { width: rawWidth, height: rawHeight, channels: 3, background: '#204080' } })
+ .withMetadata({ orientation }).jpeg().toFile(path);
+ return path;
+};
beforeAll(async () => { dir = await mkdtemp(join(tmpdir(), 'reactor-frame-')); });
afterAll(async () => { await rm(dir, { recursive: true, force: true }); });
@@ -25,7 +34,29 @@ describe('prepareReactorStartingFrame', () => {
expect(frame.aspect).toBe('9:16');
expect(frame.framePath).toBe(`${out}.start.png`);
expect(frame.fittedPath).toBe(frame.framePath);
- await expect(readImageSize(frame.framePath)).resolves.toEqual({ width: 768, height: 1344 });
+ await expect(readImageSize(frame.framePath)).resolves.toEqual({ width: 768, height: 1344, oriented: false });
+ });
+
+ // `sharp.metadata()` reports RAW dimensions regardless of how it is
+ // constructed, so measuring without reading the tag derives a landscape
+ // canvas for a portrait photo — the exact failure this module prevents.
+ it('reads a phone photo at its displayed size, not its sensor size', async () => {
+ const source = await writePhonePhoto('iphone.jpg', 4032, 3024, 6);
+ await expect(readImageSize(source)).resolves.toEqual({ width: 3024, height: 4032, oriented: true });
+ const out = join(dir, 'iphone-render.mp4');
+ const frame = await prepareReactorStartingFrame(source, undefined, out);
+ expect(frame.aspect).toBe('9:16');
+ await expect(readImageSize(frame.framePath)).resolves.toMatchObject({ width: 768, height: 1344 });
+ });
+
+ // Matching the canvas on DISPLAYED size is not enough — uploading bytes that
+ // still owe an EXIF transform hands reactor a sideways frame.
+ it('re-encodes a frame whose pixels still owe an EXIF transform', async () => {
+ const source = await writePhonePhoto('sideways.jpg', 1344, 768, 3);
+ const frame = await prepareReactorStartingFrame(source, undefined, join(dir, 'sideways-render.mp4'));
+ expect(frame.aspect).toBe('16:9');
+ expect(frame.fittedPath).toBe(frame.framePath);
+ await expect(readImageSize(frame.framePath)).resolves.toEqual({ width: 1344, height: 768, oriented: false });
});
it('honours an explicitly requested canvas over the frame it was given', async () => {
@@ -33,7 +64,7 @@ describe('prepareReactorStartingFrame', () => {
const out = join(dir, 'explicit-render.mp4');
const frame = await prepareReactorStartingFrame(source, '1:1', out);
expect(frame.aspect).toBe('1:1');
- await expect(readImageSize(frame.framePath)).resolves.toEqual({ width: 768, height: 768 });
+ await expect(readImageSize(frame.framePath)).resolves.toEqual({ width: 768, height: 768, oriented: false });
});
// A re-encode of an already-correct frame is a lossy no-op plus a temp file