Skip to content

Commit c14c429

Browse files
authored
fix(payload): calculates correct aspect ratio dimensions on sharp based files (#8537)
v2 PR [here](#8510)
1 parent 9a0568c commit c14c429

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

packages/payload/src/uploads/imageResizer.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,18 @@ export async function resizeAndTransformImageSizes({
291291
const sharpBase: Sharp | undefined = sharp(file.tempFilePath || file.data, sharpOptions).rotate() // pass rotate() to auto-rotate based on EXIF data. https://github.com/payloadcms/payload/pull/3081
292292
const originalImageMeta = await sharpBase.metadata()
293293

294+
let adjustedDimensions = { ...dimensions }
295+
296+
// Images with an exif orientation of 5, 6, 7, or 8 are auto-rotated by sharp
297+
// Need to adjust the dimensions to match the original image
298+
if ([5, 6, 7, 8].includes(originalImageMeta.orientation)) {
299+
adjustedDimensions = {
300+
...dimensions,
301+
height: dimensions.width,
302+
width: dimensions.height,
303+
}
304+
}
305+
294306
const resizeImageMeta = {
295307
height: extractHeightFromImage(originalImageMeta),
296308
width: originalImageMeta.width,
@@ -315,7 +327,7 @@ export async function resizeAndTransformImageSizes({
315327
if (resizeAction === 'resizeWithFocalPoint') {
316328
let { height: resizeHeight, width: resizeWidth } = imageResizeConfig
317329

318-
const originalAspectRatio = dimensions.width / dimensions.height
330+
const originalAspectRatio = adjustedDimensions.width / adjustedDimensions.height
319331

320332
// Calculate resizeWidth based on original aspect ratio if it's undefined
321333
if (resizeHeight && !resizeWidth) {

test/uploads/e2e.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ describe('uploads', () => {
141141
await saveDocAndAssert(page)
142142
})
143143

144+
test('should properly create IOS file upload', async () => {
145+
await page.goto(mediaURL.create)
146+
147+
await page.setInputFiles('input[type="file"]', path.resolve(dirname, './ios-image.jpeg'))
148+
149+
const filename = page.locator('.file-field__filename')
150+
151+
await expect(filename).toHaveValue('ios-image.jpeg')
152+
153+
await saveDocAndAssert(page)
154+
})
155+
144156
test('should create animated file upload', async () => {
145157
await page.goto(animatedTypeMediaURL.create)
146158

test/uploads/ios-image.jpeg

1.08 MB
Loading

0 commit comments

Comments
 (0)