From e894457275c46aff2d5db3babaef0530d8a45db1 Mon Sep 17 00:00:00 2001 From: Christopher Buzicky <65269617+Bristopher@users.noreply.github.com> Date: Sat, 10 Oct 2026 03:41:49 -0400 Subject: [PATCH] fix(transcode): apply EXIF orientation to non-RAW image transcodes and reported size (#461) * fix(transcode): apply EXIF orientation to image transcodes and reported size Cameras and phones store portrait shots as landscape pixels plus an EXIF orientation tag. sharp ignores the tag and the webp output drops it, so thumbnails, previews, watermarked share-link images and annotated snapshots came out sideways, and getImageInfo reported width and height swapped (a 5152x7728 portrait recorded as 7728x5152). RAW previews keep their existing handling: transcodeImage still rotates them from the RAW container orientation and only auto-orients inputs without one, so RAW and non-RAW images are never rotated twice. - transcodeImage: autoOrient non-RAW input and size previews from the oriented dimensions. - getImageInfo: report metadata.autoOrient dimensions for non-RAW images. - compositeOverlayToWebpFile and overlayAnnotationsOnBuffer: autoOrient the original, and draw annotations in the displayed coordinate space. - Tests: service tests that reproduce the bug and guard against double rotation of RAW, and a workflow e2e case with a real 600x400 JPEG tagged orientation 6 that must come out 400x600. * refactor(transcode): share orientation helper and test all 8 EXIF orientations Move the autoOrient decision and the displayed-size lookup into orientedSharpOptions and displayedDimensions so getImageInfo, transcodeImage, compositeOverlayToWebpFile and overlayAnnotationsOnBuffer share one rule: auto-orient unless a RAW container orientation was already applied. Behavior is unchanged. Add transcode.orientation.test.ts, which runs real sharp over a quadrant-colored JPEG tagged with each of the 8 EXIF orientations and checks the reported and transcoded size plus the top-left and bottom-right corner colors. * fix(transcode): never auto-orient dcraw output The dcraw_emu fallback reports orientation undefined because LibRaw already rotates upright, so transcodeImage applied autoOrient to the TIFF and could rotate twice if it kept an Orientation tag. extractAndValidateRawPreview now flags the fallback result as orientationApplied and transcodeImage treats it as already oriented (autoOrient false). Tests cover both the dcraw path and an embedded preview with no container orientation. --------- Co-authored-by: Bristopher --- CHANGELOG.md | 2 + packages/core/src/transcode/raw-extract.ts | 6 + .../transcode/transcode.orientation.test.ts | 252 ++++++++++++++++++ packages/core/src/transcode/transcode.test.ts | 78 +++++- packages/core/src/transcode/transcode.ts | 53 +++- packages/e2e/fixtures/portrait-rotated.jpg | Bin 0 -> 4226 bytes packages/e2e/workflow/transcode-image.test.ts | 80 ++++++ 7 files changed, 457 insertions(+), 14 deletions(-) create mode 100644 packages/core/src/transcode/transcode.orientation.test.ts create mode 100644 packages/e2e/fixtures/portrait-rotated.jpg diff --git a/CHANGELOG.md b/CHANGELOG.md index 40add3bb..bf719254 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ### Fixed +* **transcode**: Fix an issue where portrait photos from cameras and phones appeared sideways in thumbnails, previews, watermarked share links and annotated snapshots, and were listed with their width and height swapped + ### Changed ## [0.5.1] - 2026-10-06 diff --git a/packages/core/src/transcode/raw-extract.ts b/packages/core/src/transcode/raw-extract.ts index 54d51012..3b4077a4 100644 --- a/packages/core/src/transcode/raw-extract.ts +++ b/packages/core/src/transcode/raw-extract.ts @@ -25,6 +25,11 @@ export interface RawPreviewResult { width: number height: number orientation?: number + /** + * True when the file is already upright (dcraw_emu output), so downstream sharp must not + * auto-orient it even though `orientation` is undefined. + */ + orientationApplied?: boolean rawWidth?: number rawHeight?: number } @@ -382,6 +387,7 @@ export async function extractAndValidateRawPreview( previewPath: decoded.previewPath, cleanup: decoded.cleanup, orientation: decoded.orientation, + orientationApplied: true, rawWidth: decoded.rawWidth, rawHeight: decoded.rawHeight, ...dims, diff --git a/packages/core/src/transcode/transcode.orientation.test.ts b/packages/core/src/transcode/transcode.orientation.test.ts new file mode 100644 index 00000000..85db3dd4 --- /dev/null +++ b/packages/core/src/transcode/transcode.orientation.test.ts @@ -0,0 +1,252 @@ +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' +import * as fs from 'fs' +import * as path from 'path' +import sharp from 'sharp' +import { transcodeService } from './transcode' + +// Only the storage layer is mocked: these tests run the real sharp pipeline end to end. +vi.mock('@shumai/core/src/s3/s3', () => ({ + s3Service: { + downloadToFile: vi.fn(), + putObject: vi.fn(), + resolveInput: vi.fn(), + presign: vi.fn(), + }, +})) + +// Only the RAW preview extraction is replaced, so the RAW branch of transcodeImage can be driven +// with a prepared file (the dcraw_emu fallback result) without ExifTool or dcraw_emu installed. +const extractRawMock = vi.hoisted(() => vi.fn()) +vi.mock('./raw-extract', async (importOriginal) => ({ + ...(await importOriginal()), + extractAndValidateRawPreview: extractRawMock, +})) + +// Windows keeps cached files open, which would make the temp dir cleanup fail with EBUSY. +sharp.cache(false) + +type Corner = 'TL' | 'TR' | 'BL' | 'BR' + +// Stored (sensor) pixels are landscape. Each quadrant has its own color so the direction of the +// rotation or flip can be read back from the corners of the output. +const STORED_WIDTH = 64 +const STORED_HEIGHT = 48 +const QUADRANT_COLORS: Record = { + TL: [220, 30, 30], // red + TR: [30, 200, 30], // green + BL: [30, 30, 220], // blue + BR: [230, 230, 30], // yellow +} + +/** + * For each EXIF orientation: the displayed size and which stored corner ends up at the displayed + * top-left and bottom-right. Orientations 5 to 8 swap width and height. + */ +const ORIENTATIONS: Array<{ + orientation: number + name: string + swapped: boolean + topLeftIs: Corner + bottomRightIs: Corner +}> = [ + { orientation: 1, name: 'normal', swapped: false, topLeftIs: 'TL', bottomRightIs: 'BR' }, + { + orientation: 2, + name: 'mirror horizontal', + swapped: false, + topLeftIs: 'TR', + bottomRightIs: 'BL', + }, + { orientation: 3, name: 'rotate 180', swapped: false, topLeftIs: 'BR', bottomRightIs: 'TL' }, + { orientation: 4, name: 'mirror vertical', swapped: false, topLeftIs: 'BL', bottomRightIs: 'TR' }, + { orientation: 5, name: 'transpose', swapped: true, topLeftIs: 'TL', bottomRightIs: 'BR' }, + { orientation: 6, name: 'rotate 90 CW', swapped: true, topLeftIs: 'BL', bottomRightIs: 'TR' }, + { orientation: 7, name: 'transverse', swapped: true, topLeftIs: 'BR', bottomRightIs: 'TL' }, + { orientation: 8, name: 'rotate 270 CW', swapped: true, topLeftIs: 'TR', bottomRightIs: 'BL' }, +] + +async function makeJpeg(orientation: number): Promise { + const halfW = STORED_WIDTH / 2 + const halfH = STORED_HEIGHT / 2 + const block = (corner: Corner) => ({ + create: { + width: halfW, + height: halfH, + channels: 3 as const, + background: { + r: QUADRANT_COLORS[corner][0], + g: QUADRANT_COLORS[corner][1], + b: QUADRANT_COLORS[corner][2], + }, + }, + }) + const tile = async (corner: Corner) => sharp(block(corner)).png().toBuffer() + return sharp({ + create: { + width: STORED_WIDTH, + height: STORED_HEIGHT, + channels: 3, + background: { r: 0, g: 0, b: 0 }, + }, + }) + .composite([ + { input: await tile('TL'), left: 0, top: 0 }, + { input: await tile('TR'), left: halfW, top: 0 }, + { input: await tile('BL'), left: 0, top: halfH }, + { input: await tile('BR'), left: halfW, top: halfH }, + ]) + .jpeg({ quality: 100, chromaSubsampling: '4:4:4' }) + .withMetadata({ orientation }) + .toBuffer() +} + +/** Which quadrant color is nearest to the pixel a few pixels inside the given output corner. */ +async function cornerColor(file: string, where: 'TL' | 'BR'): Promise { + const { data, info } = await sharp(fs.readFileSync(file)) + .removeAlpha() + .raw() + .toBuffer({ resolveWithObject: true }) + const inset = 3 + const x = where === 'TL' ? inset : info.width - 1 - inset + const y = where === 'TL' ? inset : info.height - 1 - inset + const offset = (y * info.width + x) * info.channels + const pixel = [data[offset], data[offset + 1], data[offset + 2]] + let best: Corner = 'TL' + let bestDistance = Infinity + for (const corner of Object.keys(QUADRANT_COLORS) as Corner[]) { + const distance = QUADRANT_COLORS[corner].reduce( + (sum, channel, i) => sum + (channel - pixel[i]) ** 2, + 0, + ) + if (distance < bestDistance) { + best = corner + bestDistance = distance + } + } + return best +} + +describe('EXIF orientation (real sharp)', () => { + let tempDir: string + + beforeEach(() => { + tempDir = transcodeService.createTempDir('orientation-test-') + }) + + afterEach(() => { + transcodeService.removeDir(tempDir) + }) + + const expectedSize = (swapped: boolean) => + swapped + ? { width: STORED_HEIGHT, height: STORED_WIDTH } + : { width: STORED_WIDTH, height: STORED_HEIGHT } + + it.each(ORIENTATIONS)( + 'getImageInfo reports the displayed size for orientation $orientation ($name)', + async ({ orientation, swapped }) => { + const inputPath = path.join(tempDir, `in-${orientation}.jpg`) + fs.writeFileSync(inputPath, await makeJpeg(orientation)) + + const info = await transcodeService.getImageInfo(inputPath) + const expected = expectedSize(swapped) + + expect(info.originalWidth).toBe(expected.width) + expect(info.originalHeight).toBe(expected.height) + }, + ) + + it.each(ORIENTATIONS)( + 'transcodeImage outputs an upright image for orientation $orientation ($name)', + async ({ orientation, swapped, topLeftIs, bottomRightIs }) => { + const inputPath = path.join(tempDir, `in-${orientation}.jpg`) + const outputPath = path.join(tempDir, `out-${orientation}.webp`) + fs.writeFileSync(inputPath, await makeJpeg(orientation)) + + await transcodeService.transcodeImage(inputPath, outputPath, 1000, 100) + + const meta = await sharp(fs.readFileSync(outputPath)).metadata() + const expected = expectedSize(swapped) + expect(meta.width).toBe(expected.width) + expect(meta.height).toBe(expected.height) + // Direction, not just size: the stored corner that lands top-left and bottom-right. + expect(await cornerColor(outputPath, 'TL')).toBe(topLeftIs) + expect(await cornerColor(outputPath, 'BR')).toBe(bottomRightIs) + }, + ) + + it('transcodeImage sizes a rotated preview from the displayed dimensions', async () => { + // Stored 64x48 with orientation 6 is displayed 48x64 (portrait): the short edge is the width. + const inputPath = path.join(tempDir, 'preview-in.jpg') + const outputPath = path.join(tempDir, 'preview-out.webp') + fs.writeFileSync(inputPath, await makeJpeg(6)) + + await transcodeService.transcodeImage(inputPath, outputPath, 24, 80, { isPreview: true }) + + const meta = await sharp(fs.readFileSync(outputPath)).metadata() + expect(meta.width).toBeLessThan(meta.height ?? 0) + }) + + describe('RAW inputs', () => { + const rawPreview = (previewPath: string, extra: Record) => ({ + previewPath, + cleanup: () => {}, + width: STORED_WIDTH, + height: STORED_HEIGHT, + ...extra, + }) + + it('never auto-orients dcraw_emu output that still carries an Orientation tag', async () => { + // The dcraw fallback reports orientation undefined (already upright) but flags it as applied. + const tiffLike = path.join(tempDir, 'dcraw.jpg') + const outputPath = path.join(tempDir, 'dcraw-out.webp') + fs.writeFileSync(tiffLike, await makeJpeg(6)) + extractRawMock.mockResolvedValueOnce( + rawPreview(tiffLike, { orientation: undefined, orientationApplied: true }), + ) + + await transcodeService.transcodeImage(path.join(tempDir, 'photo.cr2'), outputPath, 1000, 100) + + const meta = await sharp(fs.readFileSync(outputPath)).metadata() + // Stored landscape stays landscape and the stored corners stay put: no second rotation. + expect(meta.width).toBe(STORED_WIDTH) + expect(meta.height).toBe(STORED_HEIGHT) + expect(await cornerColor(outputPath, 'TL')).toBe('TL') + expect(await cornerColor(outputPath, 'BR')).toBe('BR') + }) + + it('still auto-orients an embedded preview that has no container orientation', async () => { + const embedded = path.join(tempDir, 'embedded.jpg') + const outputPath = path.join(tempDir, 'embedded-out.webp') + fs.writeFileSync(embedded, await makeJpeg(6)) + extractRawMock.mockResolvedValueOnce(rawPreview(embedded, { orientation: undefined })) + + await transcodeService.transcodeImage(path.join(tempDir, 'photo.cr2'), outputPath, 1000, 100) + + const meta = await sharp(fs.readFileSync(outputPath)).metadata() + expect(meta.width).toBe(STORED_HEIGHT) + expect(meta.height).toBe(STORED_WIDTH) + }) + }) + + it('overlayAnnotationsOnBuffer keeps the displayed orientation', async () => { + const annotations = [ + { + type: 'box', + color: '#ffffff', + points: [ + [0.1, 0.1], + [0.4, 0.4], + ], + }, + ] as unknown as PrismaJson.AnnotationList + const annotated = await transcodeService.overlayAnnotationsOnBuffer( + await makeJpeg(6), + annotations, + ) + + const meta = await sharp(annotated).metadata() + expect(meta.width).toBe(STORED_HEIGHT) + expect(meta.height).toBe(STORED_WIDTH) + }) +}) diff --git a/packages/core/src/transcode/transcode.test.ts b/packages/core/src/transcode/transcode.test.ts index c6a48168..802c5086 100644 --- a/packages/core/src/transcode/transcode.test.ts +++ b/packages/core/src/transcode/transcode.test.ts @@ -330,7 +330,7 @@ describe('TranscodeService', () => { const outputFile = path.join(tempDir, 'output.webp') await transcodeService.transcodeImage('input.png', outputFile, 300, 80, { isPreview: true }) - expect(sharp).toHaveBeenCalledWith('input.png', { limitInputPixels: false }) + expect(sharp).toHaveBeenCalledWith('input.png', { limitInputPixels: false, autoOrient: true }) const mockSharp = vi.mocked(sharp).mock.results[0].value expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb') expect(mockSharp.resize).toHaveBeenCalledWith(400, 300, expect.any(Object)) @@ -544,7 +544,11 @@ describe('TranscodeService', () => { await transcodeService.transcodeImage(rawPath, outputFile, 1920, 85) - expect(sharp).toHaveBeenCalledWith('/tmp/extracted-raw.jpg', { limitInputPixels: false }) + // The RAW container orientation (1 here) is authoritative, so no auto-orient on top of it. + expect(sharp).toHaveBeenCalledWith('/tmp/extracted-raw.jpg', { + limitInputPixels: false, + autoOrient: false, + }) const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb') expect(mockSharp.webp).toHaveBeenCalledWith({ quality: 85 }) @@ -568,7 +572,10 @@ describe('TranscodeService', () => { await transcodeService.transcodeImage(rawPath, outputFile, 1920, 85) - expect(sharp).toHaveBeenCalledWith('/tmp/decoded-dcraw.tiff', { limitInputPixels: false }) + expect(sharp).toHaveBeenCalledWith('/tmp/decoded-dcraw.tiff', { + limitInputPixels: false, + autoOrient: true, + }) const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value expect(mockSharp.rotate).not.toHaveBeenCalled() expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb') @@ -593,9 +600,67 @@ describe('TranscodeService', () => { const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value expect(mockSharp.rotate).toHaveBeenCalledWith(90) + // Rotated from the container EXIF only, never also auto-oriented (would double-rotate). + expect(sharp).toHaveBeenCalledWith('/tmp/extracted-rotated.jpg', { + limitInputPixels: false, + autoOrient: false, + }) expect(cleanupSpy).toHaveBeenCalled() }) + it('should apply EXIF orientation when transcoding a rotated photo', async () => { + // A camera portrait: stored 7728x5152 (landscape) with EXIF orientation 8, shown 5152x7728. + const mockRotated = { + resize: vi.fn().mockReturnThis(), + toColorspace: vi.fn().mockReturnThis(), + webp: vi.fn().mockReturnThis(), + toFile: vi.fn().mockResolvedValue({}), + metadata: vi.fn().mockResolvedValue({ + width: 7728, + height: 5152, + orientation: 8, + autoOrient: { width: 5152, height: 7728 }, + format: 'jpeg', + }), + } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + vi.mocked(sharp).mockReturnValueOnce(mockRotated as any) + + await transcodeService.transcodeImage( + 'DSCF5056.JPG', + path.join(tempDir, 'rotated.webp'), + 300, + 80, + { isPreview: true }, + ) + + expect(sharp).toHaveBeenCalledWith( + 'DSCF5056.JPG', + expect.objectContaining({ autoOrient: true }), + ) + // Portrait preview: 300 wide on the short edge, not a 450x300 landscape one. + expect(mockRotated.resize).toHaveBeenCalledWith(300, 450, expect.any(Object)) + }) + + it('should report the displayed size of a rotated photo', async () => { + const mockRotated = { + metadata: vi.fn().mockResolvedValue({ + width: 7728, + height: 5152, + orientation: 8, + autoOrient: { width: 5152, height: 7728 }, + format: 'jpeg', + }), + } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + vi.mocked(sharp).mockReturnValueOnce(mockRotated as any) + + const info = await transcodeService.getImageInfo('DSCF5056.JPG') + + expect(info.originalWidth).toBe(5152) + expect(info.originalHeight).toBe(7728) + }) + it('should throw error when transcoding RAW without usable preview', async () => { const rawPath = path.join(tempDir, 'corrupt.raf') const outputFile = path.join(tempDir, 'output-fail.webp') @@ -618,7 +683,10 @@ describe('TranscodeService', () => { await transcodeService.transcodeImage('http://example.com/image.png', outputFile, 480, 80) expect(global.fetch).toHaveBeenCalledWith('http://example.com/image.png') - expect(sharp).toHaveBeenCalledWith(expect.any(Buffer), { limitInputPixels: false }) + expect(sharp).toHaveBeenCalledWith(expect.any(Buffer), { + limitInputPixels: false, + autoOrient: true, + }) }) it('should support Buffer as input for image transcoding', async () => { @@ -626,7 +694,7 @@ describe('TranscodeService', () => { const outputFile = path.join(tempDir, 'output-buffer.webp') await transcodeService.transcodeImage(inputBuffer, outputFile, 480, 80) - expect(sharp).toHaveBeenCalledWith(inputBuffer, { limitInputPixels: false }) + expect(sharp).toHaveBeenCalledWith(inputBuffer, { limitInputPixels: false, autoOrient: true }) const mockSharp = vi.mocked(sharp).mock.results[vi.mocked(sharp).mock.results.length - 1].value expect(mockSharp.toColorspace).toHaveBeenCalledWith('srgb') expect(mockSharp.resize).toHaveBeenCalledWith(400, 300, expect.any(Object)) diff --git a/packages/core/src/transcode/transcode.ts b/packages/core/src/transcode/transcode.ts index 729e6d68..0d81e716 100644 --- a/packages/core/src/transcode/transcode.ts +++ b/packages/core/src/transcode/transcode.ts @@ -8,7 +8,7 @@ import * as fs from 'fs' import * as os from 'os' import * as path from 'path' import PDFDocument from 'pdfkit' -import sharp from 'sharp' +import sharp, { type Metadata as SharpMetadata } from 'sharp' import { ulid } from 'ulid' import { promisify } from 'util' import { mapConcurrent } from '../utils/async' @@ -419,6 +419,28 @@ function normalizeRotation(rotation: number): number { return ((Math.round(rotation) % 360) + 360) % 360 } +interface OrientationOptions { + /** True when a RAW container orientation is applied by hand, so sharp must not also auto-orient. */ + rawOrientationApplied?: boolean +} + +/** + * Sharp input options. The webp output drops the EXIF orientation tag, so camera portraits would + * come out sideways unless sharp applies it; skip that when the RAW orientation is already applied, + * otherwise the image would rotate twice. + */ +function orientedSharpOptions({ rawOrientationApplied = false }: OrientationOptions = {}) { + return { limitInputPixels: false, autoOrient: !rawOrientationApplied } as const +} + +/** Width and height as displayed: sharp reports the EXIF-oriented size under `autoOrient`. */ +function displayedDimensions( + metadata: SharpMetadata, + { rawOrientationApplied = false }: OrientationOptions = {}, +): Pick { + return rawOrientationApplied ? metadata : (metadata.autoOrient ?? metadata) +} + export function parseBitrateKbps(bitrate: string | number): number { if (typeof bitrate === 'number') { return Math.round(bitrate / 1000) @@ -955,9 +977,11 @@ export class TranscodeService { } const metadata = await sharp(input, { limitInputPixels: false }).metadata() + // Cameras store portraits sideways plus an EXIF orientation; report the size as displayed. + const shown = displayedDimensions(metadata) return { - originalWidth: metadata.width || 0, - originalHeight: metadata.height || 0, + originalWidth: shown.width || 0, + originalHeight: shown.height || 0, duration: 0, bitRate: 0, frameRate: 0, @@ -1922,6 +1946,7 @@ export class TranscodeService { // RAW branch — extract embedded preview or decode RAW to temporary file + orientation, then treat as normal image let rawOrientation: number | undefined + let rawOutputUpright = false let rawCleanup: (() => void) | null = null if (typeof input === 'string' && isRawImage(input)) { const extracted = await extractAndValidateRawPreview(input) @@ -1932,6 +1957,7 @@ export class TranscodeService { } input = extracted.previewPath rawOrientation = extracted.orientation + rawOutputUpright = extracted.orientationApplied === true rawCleanup = extracted.cleanup } @@ -1940,11 +1966,17 @@ export class TranscodeService { let targetW = width > 0 ? Math.min(width, WEBP_MAX_DIMENSION) : WEBP_MAX_DIMENSION let targetH = height && height > 0 ? Math.min(height, WEBP_MAX_DIMENSION) : WEBP_MAX_DIMENSION - const sharpInstance = sharp(input, { limitInputPixels: false }) + // RAW previews are oriented from the container EXIF below, so only auto-orient other + // inputs: the webp output drops the tag, which would leave camera portraits sideways. + // dcraw_emu output is already upright, so it is never auto-oriented (no double rotation). + const rawOrientationApplied = rawOrientation !== undefined || rawOutputUpright + const sharpInstance = sharp(input, orientedSharpOptions({ rawOrientationApplied })) if (isPreview) { try { - const meta = await sharpInstance.metadata() + const meta = displayedDimensions(await sharpInstance.metadata(), { + rawOrientationApplied, + }) if (meta.width && meta.height) { const isSwapped = rawOrientation !== undefined && rawOrientation >= 5 && rawOrientation <= 8 @@ -2003,7 +2035,7 @@ export class TranscodeService { // Apply EXIF orientation from the RAW container to the extracted buffer. // The buffer itself often lacks orientation EXIF, so Sharp won't auto-rotate. - // For non-RAW images, Sharp auto-rotates from the image's own EXIF. + // For non-RAW images, autoOrient above applies the image's own EXIF. if (rawOrientation && EXIF_ORIENTATION_TO_ROTATION[rawOrientation]) { const { angle, flip, flop } = EXIF_ORIENTATION_TO_ROTATION[rawOrientation] if (angle) sharpInstance.rotate(angle) @@ -3019,12 +3051,15 @@ export class TranscodeService { return imageBuffer } - const meta = await sharp(imageBuffer, { limitInputPixels: false }).metadata() + // Annotations are drawn in the displayed (EXIF-oriented) coordinate space. + const meta = displayedDimensions( + await sharp(imageBuffer, { limitInputPixels: false }).metadata(), + ) const width = meta.width || 1920 const height = meta.height || 1080 const svgStr = renderAnnotationsToSvg(width, height, annotations) - return await sharp(imageBuffer, { limitInputPixels: false }) + return await sharp(imageBuffer, orientedSharpOptions()) .composite([{ input: Buffer.from(svgStr), top: 0, left: 0 }]) .toColorspace('srgb') .resize(16383, 16383, { fit: 'inside', withoutEnlargement: true }) @@ -3111,7 +3146,7 @@ export class TranscodeService { width: number, height: number, ): Promise { - await sharp(inputPath, { limitInputPixels: false }) + await sharp(inputPath, orientedSharpOptions()) .toColorspace('srgb') .resize(width, height, { fit: 'inside' }) .composite([{ input: overlayPngBuffer }]) diff --git a/packages/e2e/fixtures/portrait-rotated.jpg b/packages/e2e/fixtures/portrait-rotated.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e7c7a2755c74904f3789d4d718dd7c791c0435fe GIT binary patch literal 4226 zcmb7`e{54#6vxk7*$>)ntfNkTj`X1T=iF{npcYx_?<6!09sseX%{Ey7-Zfy3$#2 zfCrXCC9F?$_V>0Q*nN;Z^e4{tcMkOR&{#cx-Q^_U&X(@Pnd9$0_~vj&i+26|#^FP| z|K9h7nz!i4dAzG1vxa~1SMTiY8^D`f9X)yK|jP`4}_MOgU5n% z!uS&aAieN^e3TCTC$=~7E3JGNK=H5n{GaszrD=f2nSB1AY(D>Z9M9($fE(|q^>2^i z$v?vPAF6lBj{&~C0I=%5dZ**O6~h46Z}%qp61)#y2q-#n3gFS_02N2@mQ4dVU(f%H zF*-|2%Sua}Wo2b9r_)urs;aW0qH_IHHSSd#H*C^<8+_jSXLmkV@899~`U0)ncD&Tw z($cbNYnu@cMt8o_(o9Y?r^{7YQMssxAk zneeSk(Rj+g!d>j0iRKz3`pD3^^qePF>&t}DG?>~#res4dXqk9bE1Gy!B5InZCwUb) zM3YN63`RV|qtapcRx9qs4rdI#zI^<_r{7Li#cDmN@a_H4_$c~|cxU~3qwX0Q9Z%1Z z>0$^?gJ^QjGu4PyrYY&ygL)0EQV!AN603qbMu84J8FVQ3avI)=uBY2;w_|({bCj@% z52G+h3laLXFoiI5GvS#!lnDdmRW&}0olKaO;Z4-hD$B$USqpS%nI>`_a<78GGHXj( z2Y&oLPN7GL9iewV2;%35FjwF*=#CT;ahbR+k zvC!mAM)ViT&k+=eBosQHl@(the;6_{bdi;%1zs7&AEgDAqm*f80qaD4R~z2I5JKVk z(eY{K4~8!IL-&FpBhggApR6)b5nE`Q&!4L{)-6C&wUhVCv{wv8zv`a*#3Bi^*yInB z#Ll0Oh*~TY@&}L6TBR_JHHZ9}3P#+>ViRa~*70>GOiR{fx_f*Pgo4OQqSK78fW^bU zX{4%x3}ISnVK;42T8KMsu@1}8Dt+9G9M*a=p+xmogaU_7Sy}Pn?WE5L-V4r3X@TWv zLz9g^geJs3Q%h(eWn$K`L#}UACQ;uxgu;G;0`kYKxdkjvwVRvLZOg9q=m^J&PvwYJT!65382l^Oj-F6j@DjNjj~g?Yu22EjB0WybircmAh~N=$RE)u$o}I0_F7Tl(9k#MfBFw) C(J8C| literal 0 HcmV?d00001 diff --git a/packages/e2e/workflow/transcode-image.test.ts b/packages/e2e/workflow/transcode-image.test.ts index deaf952c..1f1a2f12 100644 --- a/packages/e2e/workflow/transcode-image.test.ts +++ b/packages/e2e/workflow/transcode-image.test.ts @@ -295,5 +295,85 @@ describe.each(['local', 'temporal'] as const)( expect(mediaInfo.imageTranscodes.length).toBeGreaterThan(0) expect(mediaInfo.thumbnail).toBeDefined() }, 50000) + + it('should transcode a rotated camera photo upright', async () => { + // 1. Seed Database + const team = await prisma.team.create({ + data: { name: 'E2E Rotated Transcode Team' }, + }) + + const project = await prisma.project.create({ + data: { name: 'E2E Rotated Transcode Project', teamId: team.id }, + }) + + const storageKey = await prisma.storageKey.create({ + data: { + key: 'projects/e2e/portrait-rotated.jpg', + }, + }) + + const asset = await prisma.asset.create({ + data: { + name: 'portrait-rotated.jpg', + type: 'file', + status: 'uploaded', + mediaType: 'image/jpeg', + projectId: project.id, + storageKeyId: storageKey.id, + }, + }) + + // 2. Seed S3 Storage from fixture portrait-rotated.jpg: stored 600x400 with EXIF + // orientation 6, the way cameras save portrait shots, so it is displayed 400x600. + const jpgPath = path.join(fixturesDir, 'portrait-rotated.jpg') + const jpgBuffer = fs.readFileSync(jpgPath) + await s3Service.putObject( + 'shumai-e2e-test-bucket-transcode', + 'projects/e2e/portrait-rotated.jpg', + jpgBuffer, + jpgBuffer.length, + 'image/jpeg', + ) + + // 3. Create Workflow Task + const task = await prisma.workflowTask.create({ + data: { + type: 'transcode_image', + status: 'pending', + assetId: asset.id, + projectId: project.id, + teamId: team.id, + payload: { + projectId: project.id, + transcode: { + thumbnail: true, + }, + }, + }, + }) + + // 4. Wait for workflow to complete + console.log( + `Submitted E2E Rotated Image Transcode Workflow Task. ID: ${task.id}. Awaiting completion...`, + ) + const completedTask = await workflowService.executeWait(task, 45000) + + // 5. Verification: the asset and its transcode are portrait, not the stored landscape. + expect(completedTask.status).toBe('completed') + + const updatedAsset = await prisma.asset.findUnique({ + where: { id: asset.id }, + }) + expect(updatedAsset?.status).toBe(AssetStatus.processed) + + const mediaInfo = updatedAsset?.media as unknown as { + imageTranscodes: Array<{ width: number; height: number }> + metadata: { originalWidth: number; originalHeight: number } + } + expect(mediaInfo.metadata.originalWidth).toBe(400) + expect(mediaInfo.metadata.originalHeight).toBe(600) + expect(mediaInfo.imageTranscodes[0].width).toBe(400) + expect(mediaInfo.imageTranscodes[0].height).toBe(600) + }, 50000) }, )