diff --git a/packages/next/client/future/image.tsx b/packages/next/client/future/image.tsx index 7dbf0807d745c96..bef68231bd7ce57 100644 --- a/packages/next/client/future/image.tsx +++ b/packages/next/client/future/image.tsx @@ -548,6 +548,8 @@ export default function Image({ } let staticSrc = '' + let widthInt = getInt(width) + let heightInt = getInt(height) let blurWidth: number | undefined let blurHeight: number | undefined if (isStaticImport(src)) { @@ -560,16 +562,6 @@ export default function Image({ )}` ) } - blurWidth = staticImageData.blurWidth - blurHeight = staticImageData.blurHeight - blurDataURL = blurDataURL || staticImageData.blurDataURL - staticSrc = staticImageData.src - - // Ignore width and height (come from the bundler) when "fill" is used - if (!fill) { - height = height || staticImageData.height - width = width || staticImageData.width - } if (!staticImageData.height || !staticImageData.width) { throw new Error( `An object should only be passed to the image component src parameter if it comes from a static image import. It must include height and width. Received ${JSON.stringify( @@ -577,6 +569,24 @@ export default function Image({ )}` ) } + + blurWidth = staticImageData.blurWidth + blurHeight = staticImageData.blurHeight + blurDataURL = blurDataURL || staticImageData.blurDataURL + staticSrc = staticImageData.src + + if (!fill) { + if (!widthInt && !heightInt) { + widthInt = staticImageData.width + heightInt = staticImageData.height + } else if (widthInt && !heightInt) { + const ratio = widthInt / staticImageData.width + heightInt = Math.round(staticImageData.height * ratio) + } else if (!widthInt && heightInt) { + const ratio = heightInt / staticImageData.height + widthInt = Math.round(staticImageData.width * ratio) + } + } } src = typeof src === 'string' ? src : staticSrc @@ -593,8 +603,7 @@ export default function Image({ const [blurComplete, setBlurComplete] = useState(false) const [showAltText, setShowAltText] = useState(false) - let widthInt = getInt(width) - let heightInt = getInt(height) + const qualityInt = getInt(quality) if (process.env.NODE_ENV !== 'production') { diff --git a/test/integration/image-future/base-path/pages/static-img.js b/test/integration/image-future/base-path/pages/static-img.js index d5ce76c1289809b..da76ca22f358273 100644 --- a/test/integration/image-future/base-path/pages/static-img.js +++ b/test/integration/image-future/base-path/pages/static-img.js @@ -10,6 +10,7 @@ import testSVG from '../public/test.svg' import testGIF from '../public/test.gif' import testBMP from '../public/test.bmp' import testICO from '../public/test.ico' +import widePNG from '../public/wide.png' import TallImage from '../components/TallImage' @@ -19,7 +20,14 @@ const Page = () => {

Static Image

- + + + { expect(img.attr('width')).toBe('400') expect(img.attr('height')).toBe('300') }) - it('Should allow provided width and height to override intrinsic', async () => { - const img = $('#defined-size-static') + it('should use width and height prop to override import', async () => { + const img = $('#defined-width-and-height') expect(img.attr('width')).toBe('150') expect(img.attr('height')).toBe('150') }) + it('should use height prop to adjust both width and height', async () => { + const img = $('#defined-height-only') + expect(img.attr('width')).toBe('600') + expect(img.attr('height')).toBe('350') + }) + it('should use width prop to adjust both width and height', async () => { + const img = $('#defined-width-only') + expect(img.attr('width')).toBe('400') + expect(img.attr('height')).toBe('233') + }) it('Should add a blur placeholder a statically imported jpg', async () => { const style = $('#basic-static').attr('style') diff --git a/test/integration/image-future/default/pages/static-img.js b/test/integration/image-future/default/pages/static-img.js index 472c5906387e4a8..26c2e5a55fbc575 100644 --- a/test/integration/image-future/default/pages/static-img.js +++ b/test/integration/image-future/default/pages/static-img.js @@ -10,6 +10,7 @@ import testSVG from '../public/test.svg' import testGIF from '../public/test.gif' import testBMP from '../public/test.bmp' import testICO from '../public/test.ico' +import widePNG from '../public/wide.png' import TallImage from '../components/TallImage' @@ -19,7 +20,14 @@ const Page = () => {

Static Image

- + + + { expect(img.attr('width')).toBe('400') expect(img.attr('height')).toBe('300') }) - it('Should allow provided width and height to override intrinsic', async () => { - const img = $('#defined-size-static') + it('should use width and height prop to override import', async () => { + const img = $('#defined-width-and-height') expect(img.attr('width')).toBe('150') expect(img.attr('height')).toBe('150') }) + it('should use height prop to adjust both width and height', async () => { + const img = $('#defined-height-only') + expect(img.attr('width')).toBe('600') + expect(img.attr('height')).toBe('350') + }) + it('should use width prop to adjust both width and height', async () => { + const img = $('#defined-width-only') + expect(img.attr('width')).toBe('400') + expect(img.attr('height')).toBe('233') + }) it('Should add a blur placeholder a statically imported jpg', async () => { const style = $('#basic-static').attr('style')