#import.meta.glob on images

8 messages · Page 1 of 1 (latest)

neat shuttle
#

const images = import.meta.glob("@assets/images/parallax-gallery/*.jpg", { eager: true });
console.log(images);

it returns paths, and not ImageMetadata. Can I use the <Image /> component?

faint crowBOT
#

Hi @neat shuttle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

When using import.meta.glob() with images in Astro, you need to make a small adjustment to work with the <Image /> component. The issue you're encountering is that your current implementation returns paths rather than the ImageMetadata objects required by the <Image /> component.

Here's how to fix this:

  1. You need to specify the type for your glob import to get the proper ImageMetadata objects:
import type { ImageMetadata } from 'astro';
const images = import.meta.glob<{ default: ImageMetadata }>("@assets/images/parallax-gallery/*.jpg", { eager: true });
  1. When using these images with the <Image /> component, you'll need to access the default property:
---
import { Image } from 'astro:assets';
import type { ImageMetadata } from 'astro';

const images = import.meta.glob<{ default: ImageMetadata }>("@assets/images/parallax-gallery/*.jpg", { eager: true });
---

{Object.values(images).map((image) => (
  <Image src={image.default} alt="Gallery image" />
))}

This approach is documented in the Dynamically importing images recipe, which provides a complete example of how to work with dynamically imported images in Astro.

Remember that when using the <Image /> component, you must provide the full image object with metadata (width, height, format) rather than just a string path. The type annotation ensures you get the proper object structure that the <Image /> component expects.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@neat shuttle Kapa.ai is still learning and improving, please let me know how I did by reacting below.

neat shuttle
#

@faint crow
can I access the filename to serve as an alt?

faint crowBOT
#

Let me check the knowledge base for you again. One moment! :books:

#

Yes, you can extract the filename from the image path to use as an alt text. When working with dynamically imported images using import.meta.glob(), you can access the file path and extract the filename from it.

Here's how you can do it:

---
import { Image } from 'astro:assets';
import type { ImageMetadata } from 'astro';

const images = import.meta.glob<{ default: ImageMetadata }>("@assets/images/parallax-gallery/*.jpg", { eager: true });

// Function to extract filename from path
function getFilenameFromPath(path) {
  // Get the filename with extension
  const filenameWithExt = path.split('/').pop();
  // Remove extension if needed
  const filename = filenameWithExt.split('.')[0];
  return filename;
}
---

{Object.entries(images).map(([path, image]) => {
  const altText = getFilenameFromPath(path);
  return (
    <Image src={image.default} alt={altText} />
  );
})}

This approach extracts the filename from the path and uses it as the alt text for each image. Keep in mind that filenames might not always provide meaningful descriptions for accessibility purposes. If possible, consider using more descriptive alt text that explains the content of the image, especially for important images as recommended in the Astro documentation.

Remember that the alt attribute is required for the <Image /> component in Astro for accessibility reasons. If an image is purely decorative, you can use alt="" instead of omitting it entirely.

I'm using a simple string manipulation approach here, but you could make the function more robust depending on your specific naming conventions and requirements.

Let me know if you need any clarification or have any other questions about working with images in Astro!

#

I'm using a simple string manipulation approach here, but you could make the function more robust depending on your specific naming conventions and requirements.

Let me know if you need any clarification or have any other questions about working with images in Astro!

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: