Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): orientation handling for person thumbnails #10382

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server/src/interfaces/media.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export interface ImageDimensions {
height: number;
}

export interface InputDimensions extends ImageDimensions {
inputPath: string;
}

export interface VideoInfo {
format: VideoFormat;
videoStreams: VideoStreamInfo[];
Expand Down
6 changes: 3 additions & 3 deletions server/src/services/person.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -917,9 +917,9 @@ describe(PersonService.name, () => {
colorspace: Colorspace.P3,
crop: {
left: 0,
top: 428,
width: 1102,
height: 1102,
top: 85,
width: 510,
height: 510,
},
},
);
Expand Down
30 changes: 8 additions & 22 deletions server/src/services/person.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ import {
} from 'src/interfaces/job.interface';
import { ILoggerRepository } from 'src/interfaces/logger.interface';
import { BoundingBox, IMachineLearningRepository } from 'src/interfaces/machine-learning.interface';
import { CropOptions, IMediaRepository, ImageDimensions } from 'src/interfaces/media.interface';
import { CropOptions, IMediaRepository, ImageDimensions, InputDimensions } from 'src/interfaces/media.interface';
import { IMoveRepository } from 'src/interfaces/move.interface';
import { IPersonRepository, UpdateFacesData } from 'src/interfaces/person.interface';
import { ISearchRepository } from 'src/interfaces/search.interface';
import { IStorageRepository } from 'src/interfaces/storage.interface';
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
import { Orientation } from 'src/services/metadata.service';
import { CacheControl, ImmichFileResponse } from 'src/utils/file';
import { mimeTypes } from 'src/utils/mime-types';
import { isFacialRecognitionEnabled } from 'src/utils/misc';
Expand Down Expand Up @@ -520,7 +519,7 @@ export class PersonService {
return JobStatus.FAILED;
}

const { width, height, inputPath } = await this.getInputDimensions(asset);
const { width, height, inputPath } = await this.getInputDimensions(asset, { width: oldWidth, height: oldHeight });

const thumbnailPath = StorageCore.getPersonThumbnailPath(person);
this.storageCore.ensureFolders(thumbnailPath);
Expand Down Expand Up @@ -601,7 +600,7 @@ export class PersonService {
return person;
}

private async getInputDimensions(asset: AssetEntity): Promise<ImageDimensions & { inputPath: string }> {
private async getInputDimensions(asset: AssetEntity, oldDims: ImageDimensions): Promise<InputDimensions> {
if (!asset.exifInfo?.exifImageHeight || !asset.exifInfo.exifImageWidth) {
throw new Error(`Asset ${asset.id} dimensions are unknown`);
}
Expand All @@ -611,31 +610,18 @@ export class PersonService {
}

if (asset.type === AssetType.IMAGE) {
const { width, height } = this.withOrientation(asset.exifInfo.orientation as Orientation, {
width: asset.exifInfo.exifImageWidth,
height: asset.exifInfo.exifImageHeight,
});
let { exifImageWidth: width, exifImageHeight: height } = asset.exifInfo;
if (oldDims.height > oldDims.width !== height > width) {
[width, height] = [height, width];
}

return { width, height, inputPath: asset.originalPath };
}

const { width, height } = await this.mediaRepository.getImageDimensions(asset.previewPath);
return { width, height, inputPath: asset.previewPath };
}

private withOrientation(orientation: Orientation, { width, height }: ImageDimensions): ImageDimensions {
switch (orientation) {
case Orientation.MirrorHorizontalRotate270CW:
case Orientation.Rotate90CW:
case Orientation.MirrorHorizontalRotate90CW:
case Orientation.Rotate270CW: {
return { width: height, height: width };
}
default: {
return { width, height };
}
}
}

private getCrop(dims: { old: ImageDimensions; new: ImageDimensions }, { x1, y1, x2, y2 }: BoundingBox): CropOptions {
const widthScale = dims.new.width / dims.old.width;
const heightScale = dims.new.height / dims.old.height;
Expand Down
4 changes: 2 additions & 2 deletions server/test/fixtures/face.stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export const faceStub = {
boundingBoxY1: 5,
boundingBoxX2: 505,
boundingBoxY2: 505,
imageHeight: 1000,
imageWidth: 1000,
imageHeight: 2880,
imageWidth: 2160,
}),
middle: Object.freeze<NonNullableProperty<AssetFaceEntity>>({
id: 'assetFaceId6',
Expand Down
Loading