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
+44 -9
View File
@@ -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<SharpMetadata, 'width' | 'height'> {
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<void> {
await sharp(inputPath, { limitInputPixels: false })
await sharp(inputPath, orientedSharpOptions())
.toColorspace('srgb')
.resize(width, height, { fit: 'inside' })
.composite([{ input: overlayPngBuffer }])