From 9aecf7c7c7211f34236d8dde624ca388310d3727 Mon Sep 17 00:00:00 2001
From: Chris Swithinbank <swithinbank@gmail.com>
Date: Fri, 22 Jul 2022 21:14:00 +0200
Subject: [PATCH] Handle EXIF orientation flag (#4021)

* Handle EXIF orientation flag

* Create gentle-mails-mate.md
---
 .changeset/gentle-mails-mate.md             | 5 +++++
 packages/integrations/image/src/metadata.ts | 7 ++++---
 2 files changed, 9 insertions(+), 3 deletions(-)
 create mode 100644 .changeset/gentle-mails-mate.md

diff --git a/.changeset/gentle-mails-mate.md b/.changeset/gentle-mails-mate.md
new file mode 100644
index 000000000000..eb3d561f229a
--- /dev/null
+++ b/.changeset/gentle-mails-mate.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/image": patch
+---
+
+Handle EXIF orientation flag
diff --git a/packages/integrations/image/src/metadata.ts b/packages/integrations/image/src/metadata.ts
index 020d9461fdc3..823862ea7938 100644
--- a/packages/integrations/image/src/metadata.ts
+++ b/packages/integrations/image/src/metadata.ts
@@ -5,7 +5,8 @@ import { ImageMetadata, InputFormat } from './types';
 export async function metadata(src: string): Promise<ImageMetadata | undefined> {
 	const file = await fs.readFile(src);
 
-	const { width, height, type } = await sizeOf(file);
+	const { width, height, type, orientation } = await sizeOf(file);
+	const isPortrait = (orientation || 0) >= 5;
 
 	if (!width || !height || !type) {
 		return undefined;
@@ -13,8 +14,8 @@ export async function metadata(src: string): Promise<ImageMetadata | undefined>
 
 	return {
 		src,
-		width,
-		height,
+		width: isPortrait ? height : width,
+		height: isPortrait ? width : height,
 		format: type as InputFormat,
 	};
 }