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 <Bristopher@users.noreply.github.com>
This commit is contained in:
Christopher Buzicky
2026-10-10 15:41:49 +08:00
committed by GitHub
co-authored by Bristopher
parent 4b1b64fe2b
commit e894457275
7 changed files with 457 additions and 14 deletions
+73 -5
View File
@@ -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))