Unverified Commit 85e143e9 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Rotate image from EXIF data (#18687)

Fixes #18643
parent 2d5dbc48
...@@ -259,6 +259,8 @@ export async function imageOptimizer( ...@@ -259,6 +259,8 @@ export async function imageOptimizer(
try { try {
const transformer = sharp(upstreamBuffer) const transformer = sharp(upstreamBuffer)
transformer.rotate() // auto rotate based on EXIF data
const { width: metaWidth } = await transformer.metadata() const { width: metaWidth } = await transformer.metadata()
if (metaWidth && metaWidth > width) { if (metaWidth && metaWidth > width) {
......
import Image from 'next/image'
import React from 'react'
const Page = () => {
return (
<div>
<p>Hello World</p>
<Image id="exif-rotation-image" src="/exif-rotation.jpg" unsized />
<p id="stubtext">This is the rotated page</p>
</div>
)
}
export default Page
...@@ -10,6 +10,7 @@ import { ...@@ -10,6 +10,7 @@ import {
check, check,
hasRedbox, hasRedbox,
getRedboxHeader, getRedboxHeader,
waitFor,
} from 'next-test-utils' } from 'next-test-utils'
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import fs from 'fs-extra' import fs from 'fs-extra'
...@@ -319,6 +320,44 @@ function runTests(mode) { ...@@ -319,6 +320,44 @@ function runTests(mode) {
) )
}) })
} }
// Tests that use the `unsized` attribute:
if (mode !== 'dev') {
it('should correctly rotate image', async () => {
let browser
try {
browser = await webdriver(appPort, '/rotated')
const id = 'exif-rotation-image'
// Wait for image to load:
await check(async () => {
const result = await browser.eval(
`document.getElementById(${JSON.stringify(id)}).naturalWidth`
)
if (result < 1) {
throw new Error('Image not ready')
}
return 'result-correct'
}, /result-correct/)
await waitFor(1000)
const computedWidth = await getComputed(browser, id, 'width')
const computedHeight = await getComputed(browser, id, 'height')
expect(getRatio(computedWidth, computedHeight) / 1000.0).toBeCloseTo(
1.333,
1
)
} finally {
if (browser) {
await browser.close()
}
}
})
}
} }
describe('Image Component Tests', () => { describe('Image Component Tests', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment