#Guys Why next image cannot take height

1 messages ยท Page 1 of 1 (latest)

quartz bluff
cobalt tiger
#

I have tried layout responsive, maybe I am using it wrong.

worldly halo
#

Send a screenshot of your usecase

cobalt tiger
#

Sure!

#

Next/Image approach.

#
     <section className="flex flex-col">
        {images.map((img) => (
          <div className="mx-auto" key={img}>
            <Image alt={img} src={img} width={'768px'} height={'1000'} />
          </div>
        ))}
      </section>
#

Output:

#

2.Plain html img tag with fixed width and height auto which is by default.

#
      <section className="flex flex-col">
        {images.map((img) => (
          <div className="mx-auto" key={img}>
            <img alt={img} src={img} width={'768px'} loading={'lazy'} />
          </div>
        ))}
      </section>
#

This is what I want, the images look not distorted as the previous image

quartz bluff
#

what is the original width and height of the image?

#

looks like 1000px is not enough

cobalt tiger
#

Here is the list of image if you want to try it.

#

If I use the images like this:

            <Image alt={img} src={img} width={'768px'} height={18000} />
#

They are distorted again

quartz bluff
#

use the layout responsive prop

cobalt tiger
#

They look way too tall

quartz bluff
#

or try layout="fill" with objectFit="cover"

cobalt tiger
#
            <Image alt={img} src={img} width={'768px'} height={18000} layout={'responsive'} />
#

I tried with layout responsive, they are not showin in screen

#

I have tried layout=fill and object fit cover.

#
           <Image
              alt={img}
              src={img}
              width={'768px'}
              height={18000}
              layout={'fill'}
              objectFit={'cover'}
            />
#

There is only one image that shows on the screen, its not scrollable

quartz bluff
#

so you want them to all the same width but different heights?

cobalt tiger
#

Yes, yes

quartz bluff
#

style prop could also work, to overwrite the height with auto

#

also just a sidenote but you're using a string for the width and int for the height, it probably doesn't matter but I would use the same type just to be sure ๐Ÿ‘€

cobalt tiger
#

Yeah, I am using ts, it showed it can accept string and number as types

#

So I thought it could take auto height as well.

cobalt tiger
#
            <Image alt={img} src={img} width={'768px'} height={'14000'} style={'height':'auto'}/>
quartz bluff
#
<Image style={{height: "auto"}} />
#

needs extra brackets

cobalt tiger
#

Oh!

cobalt tiger
#
    <div className="mx-auto" key={img}>
            <Image alt={img} src={img} layout={'fill'} objectFit={'cover'} style={imageStyle} />
          </div>
#
  const imageStyle = {
    height: 'auto',
    width: '768px',
  };
#

Am I missing something?

quartz bluff
#

I don't think you need to layout and objectFit anymore when height auto works ๐Ÿค”

cobalt tiger
#

So, I need to pass height and width and let the custom style ovrride it.

#

Okay, let me try that

#

I am able to see the image as expected now with fixed width. But it doesnt seem to be taking the custom height.

#
      <div className="mx-auto" key={img}>
            <Image alt={img} src={img} width={'768px'} height={'14000'} style={imageStyle} />
          </div>
quartz bluff
#

can you see if it's there in devtools? maybe it's just getting overwritten

cobalt tiger
#

Yes sure

#

Looks like the second image has different size but the first span tag, is overriting it

quartz bluff
#

there's also layout="raw" but it's still experimental

#

but I guess that's pretty much just a img tag then

cobalt tiger
#

Yeah, then I wouldnt be able to use the benefits of the Next js image performance

#

๐Ÿ˜ญ

quartz bluff
#

maybe you can get the size of each image with js somehow and insert it into the Image as props?

#

or put a width and height attribute into your image object

cobalt tiger
#

At this point I am ready to take the risk of experimental features. But ig, 12.2 doesnt have height=auto

quartz bluff
#

what if you do your image array like this?

const images = [
    {
        url: 'https://firebasest...',
        width: 1280,
        height: 15311
    },
    {
        url: 'https://firebasest...',
        width: 1280,
        height: 15311
    }
]
#

and then insert the height and width for each one like

<Image alt={img} src={img.url} width={img.width} height={img.height} style={imageStyle} />
cobalt tiger
#

I could do that.

#

Let me try this one

#

But then my question is shouldnt be height auto allowed in next js.

#
const url = require('url')
const http = require('http')

const sizeOf = require('image-size')

const imgUrl = 'http://my-amazing-website.com/image.jpeg'
const options = url.parse(imgUrl)

http.get(options, function (response) {
  const chunks = []
  response.on('data', function (chunk) {
    chunks.push(chunk)
  }).on('end', function() {
    const buffer = Buffer.concat(chunks)
    console.log(sizeOf(buffer))
  })
})
#

I am using this package

quartz bluff
cobalt tiger
#

Which is why they got 12.2!

quartz bluff
#

that looks way better

#

but height and width are still required

cobalt tiger
#

Yeah! lolsob

#
error - ./node_modules/image-size/dist/index.js:13:0
Module not found: Can't resolve 'fs'

Import trace for requested module:
#

Let me fix this, oh boy!

#

just to add height=auto in nextjs!

cobalt tiger
#
Error: The "next/future/image" component is experimental and may be subject to breaking changes. To enable this experiment, please include `experimental: { images: { allowFutureImage: true } }` in your next.config.js file.
#
/** @type {import('next').NextConfig} */
const nextConfig = {
    strictMode: true,
    experimental: {
        images: {
            allowFutureImage: true,
        },
    },
    images: {
        domains: ['firebasestorage.googleapis.com',],
    },
};

module.exports = nextConfig;
#

OMG! @quartz bluff It worked it worked

#
            <Image alt={img} src={img} />
quartz bluff
#

I guess I gotta start using future image now aswell ๐Ÿ˜‚ @cobalt tiger

cobalt tiger
#

Yeah I didnt pass any height and width

twilit iris
cobalt tiger