Fallback orientation calc for Immich 2.x

This commit is contained in:
Alan Grainger
2026-07-27 11:27:16 +12:00
parent fd42ab61da
commit 74c09acc27
3 changed files with 50 additions and 23 deletions
+2 -11
View File
@@ -14,7 +14,7 @@ import { Gallery, GalleryItem, GalleryProps } from '../view/gallery'
import type { GroupByDateMode } from '../shared/types' import type { GroupByDateMode } from '../shared/types'
import { downloadFilename } from './filename' import { downloadFilename } from './filename'
import { requiresOriginal } from './sizing' import { requiresOriginal } from './sizing'
import { metadataGroupActive, pickExif } from './exif' import { displayDimensions, metadataGroupActive, pickExif } from './exif'
/** /**
* Render a gallery page for a given SharedLink. * Render a gallery page for a given SharedLink.
@@ -85,16 +85,7 @@ export async function gallery (res: Response, share: SharedLink, openItem?: numb
? asset.exifInfo.description ? asset.exifInfo.description
: '' : ''
let width = asset.width const { width, height } = displayDimensions(asset)
let height = asset.height
if (!width || !height) {
width = asset.exifInfo?.exifImageWidth
height = asset.exifInfo?.exifImageHeight
const orientation = asset.exifInfo?.orientation
if (orientation && ['5', '6', '7', '8'].includes(orientation) && width && height) {
[width, height] = [height, width]
}
}
return { return {
id: asset.id, id: asset.id,
+32 -8
View File
@@ -26,11 +26,36 @@ function copy (key: keyof ExifInfo & keyof GalleryExif): FieldRule {
} }
} }
/*
EXIF orientation values meaning the image is stored rotated 90/270 degrees,
so its displayed width and height are the stored ones swapped. 5-8 are the
standard EXIF set (see https://magnushoff.com/articles/jpeg-orientation/);
90 / -90 are degree-style values Immich also emits - see `isFlipped` in its
`web/src/lib/utils/asset-utils.ts`.
*/
const FLIPPED_ORIENTATIONS = [5, 6, 90, 7, 8, -90]
/**
* An asset's dimensions as displayed. Prefers the asset-level width/height,
* which Immich provides orientation-corrected from 3.0.0
*/
export function displayDimensions (asset: Asset): { width?: number, height?: number } {
if (asset.width && asset.height) return { width: asset.width, height: asset.height }
// Fallback for Immich 2.x
const info = asset.exifInfo
const width = info?.exifImageWidth
const height = info?.exifImageHeight
if (!width || !height) return {}
return FLIPPED_ORIENTATIONS.includes(Number(info?.orientation))
? { width: height, height: width }
: { width, height }
}
/* /*
Fields whose config flag, ExifInfo key, and GalleryExif key are all the Fields whose config flag, ExifInfo key, and GalleryExif key are all the
same string. `copy` handles the whole if/copy dance for each. Fields same string, so `copy` handles them. Anything needing special handling is
that need special handling (fileName, dimensions, fileSize, gps) appear written inline in the `*_RULES` arrays below.
inline in the `*_RULES` arrays below.
Adding a new metadata field: this rules table is the server-side source Adding a new metadata field: this rules table is the server-side source
of truth. Two other places carry parallel knowledge of the field set: of truth. Two other places carry parallel knowledge of the field set:
@@ -59,11 +84,10 @@ const EXIF_RULES: FieldRule[] = [
{ {
flag: 'dimensions', flag: 'dimensions',
emit: (out, _info, asset) => { emit: (out, _info, asset) => {
// Immich has already applied EXIF orientation to the asset-level const { width, height } = displayDimensions(asset)
// dimensions; `exifImage*` holds the raw stored size. if (width && height) {
if (asset.width && asset.height) { out.width = width
out.width = asset.width out.height = height
out.height = asset.height
} }
} }
}, },
+16 -4
View File
@@ -55,9 +55,9 @@ describe('buildAssetMetadata', () => {
expect(meta.exif?.city).toBeUndefined() expect(meta.exif?.city).toBeUndefined()
}) })
// Immich applies EXIF orientation before storing asset.width / asset.height, // Immich 3.x applies EXIF orientation before storing asset.width / height,
// so a rotated photo reports portrait dimensions there while the exifImage* // so a rotated photo reports portrait dimensions there while the exifImage*
// pair stays landscape. We must report the former. // pair stays landscape. The asset-level pair must win.
it('reports dimensions in display orientation', () => { it('reports dimensions in display orientation', () => {
setConfig({ ipp: { showMetadata: { exif: { dimensions: true } } } }) setConfig({ ipp: { showMetadata: { exif: { dimensions: true } } } })
const photo = asset() const photo = asset()
@@ -73,13 +73,25 @@ describe('buildAssetMetadata', () => {
}) })
}) })
it('omits dimensions when the asset has none', () => { // Immich 2.x has no asset-level width / height, so the raw EXIF pair is
// used, swapped when the orientation marks the image as stored rotated.
const exifOrientationCases: Array<[string | null, number, number]> = [
['6', 3000, 4000],
['8', 3000, 4000],
['90', 3000, 4000],
['-90', 3000, 4000],
['1', 4000, 3000],
[null, 4000, 3000]
]
it.each(exifOrientationCases)('falls back to swapped exif dimensions without asset dimensions (orientation %s)', (orientation, width, height) => {
setConfig({ ipp: { showMetadata: { exif: { dimensions: true } } } }) setConfig({ ipp: { showMetadata: { exif: { dimensions: true } } } })
const photo = asset() const photo = asset()
photo.exifInfo!.exifImageWidth = 4000 photo.exifInfo!.exifImageWidth = 4000
photo.exifInfo!.exifImageHeight = 3000 photo.exifInfo!.exifImageHeight = 3000
photo.exifInfo!.orientation = orientation
expect(buildAssetMetadata(photo, share()).exif).toBeUndefined() expect(buildAssetMetadata(photo, share()).exif).toMatchObject({ width, height })
}) })
it('returns description when a description surface is enabled', () => { it('returns description when a description surface is enabled', () => {