From 0b9c58c8cf69cbb8d237dca5194141c981ed05d7 Mon Sep 17 00:00:00 2001 From: casi-3 <47931895+casi-3@users.noreply.github.com> Date: Tue, 21 Jul 2026 19:05:13 +0200 Subject: [PATCH] Apply EXIF orientation when converting images with ImageMagick Photos taken on phones store pixels unrotated with an EXIF Orientation tag. Converting them to formats where the tag is lost or ignored (png, webp, ...) produced sideways images. Add -auto-orient so the pixels are rotated according to the tag before writing the output. --- src/converters/imagemagick.ts | 4 ++++ tests/converters/imagemagick.test.ts | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/converters/imagemagick.ts b/src/converters/imagemagick.ts index 69eb3591..e3fc53c2 100644 --- a/src/converters/imagemagick.ts +++ b/src/converters/imagemagick.ts @@ -468,6 +468,10 @@ export function convert( outputArgs.push("-background", "white", "-alpha", "remove"); } + // Apply EXIF orientation so photos (e.g. from phones) don't end up sideways + // when converted to formats where the orientation tag is lost or ignored + outputArgs.push("-auto-orient"); + return new Promise((resolve, reject) => { execFile( "magick", diff --git a/tests/converters/imagemagick.test.ts b/tests/converters/imagemagick.test.ts index e7b17aa3..f6940459 100644 --- a/tests/converters/imagemagick.test.ts +++ b/tests/converters/imagemagick.test.ts @@ -163,3 +163,19 @@ test("convert respects emf as input filetype", async () => { ); expect(loggedMessage).toBe("stdout: Fake stdout"); }); + +test("convert applies EXIF auto-orient", async () => { + const mockExecFile: ExecFileFn = ( + _cmd: string, + _args: string[], + callback: (err: ExecFileException | null, stdout: string, stderr: string) => void, + ) => { + calls.push(_args); + callback(null, "", ""); + }; + + const result = await convert("input.jpg", "jpg", "png", "output.png", undefined, mockExecFile); + + expect(result).toBe("Done"); + expect(calls[0]).toEqual(expect.arrayContaining(["input.jpg", "-auto-orient", "output.png"])); +});