> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/withastro/astro/llms.txt
> Use this file to discover all available pages before exploring further.

# Assets API

> API reference for Astro's built-in image optimization and asset handling

Astro provides built-in image optimization and asset handling through the `astro:assets` module.

## Importing

```typescript theme={null}
import { Image, Picture, getImage } from 'astro:assets';
```

## Image Component

The `<Image />` component optimizes images and generates responsive image markup.

```astro theme={null}
import { Image } from 'astro:assets';
import myImage from '../assets/my-image.png';

<Image src={myImage} alt="A description of my image" />
```

### Props

<ParamField path="src" type="ImageMetadata | string" required>
  Image source. Can be an imported image or a URL string.

  ```astro theme={null}
  import localImage from '../images/photo.jpg';

  <Image src={localImage} alt="Local image" />
  <Image src="/public-image.jpg" alt="Public image" />
  <Image src="https://example.com/remote.jpg" alt="Remote image" />
  ```
</ParamField>

<ParamField path="alt" type="string" required>
  Alternative text for the image. Required for accessibility.

  ```astro theme={null}
  <Image src={img} alt="A beautiful sunset over the ocean" />
  ```
</ParamField>

<ParamField path="width" type="number | string">
  Desired width of the image. Sets the `width` attribute and resizes the image.

  ```astro theme={null}
  <Image src={img} width={300} alt="..." />
  ```
</ParamField>

<ParamField path="height" type="number | string">
  Desired height of the image. Sets the `height` attribute and resizes the image if `width` is not provided.

  ```astro theme={null}
  <Image src={img} height={200} alt="..." />
  ```
</ParamField>

<ParamField path="format" type="'avif' | 'webp' | 'png' | 'jpg' | 'jpeg' | 'svg' | string">
  Output format for the image.

  **Default:** `'webp'`

  ```astro theme={null}
  <Image src={img} format="avif" alt="..." />
  ```
</ParamField>

<ParamField path="quality" type="'low' | 'mid' | 'high' | 'max' | number">
  Quality preset or numeric value (0-100) for the output image.

  ```astro theme={null}
  <Image src={img} quality="high" alt="..." />
  <Image src={img} quality={85} alt="..." />
  ```
</ParamField>

<ParamField path="densities" type="(number | `${number}x`)[]">
  Pixel densities to generate for the image srcset.

  ```astro theme={null}
  <Image src={img} densities={[1, 2, 3]} alt="..." />
  <Image src={img} densities={['1x', '2x']} alt="..." />
  ```
</ParamField>

<ParamField path="widths" type="number[]">
  Widths to generate for the image srcset.

  ```astro theme={null}
  <Image src={img} widths={[400, 800, 1200]} alt="..." />
  ```
</ParamField>

<ParamField path="fit" type="'fill' | 'contain' | 'cover' | 'none' | 'scale-down' | string">
  How the image should be resized to fit the dimensions.

  **Default:** `'cover'`

  ```astro theme={null}
  <Image src={img} width={300} height={200} fit="contain" alt="..." />
  ```
</ParamField>

<ParamField path="position" type="string">
  Position of the image when using `fit`. Similar to CSS `object-position`.

  ```astro theme={null}
  <Image src={img} fit="cover" position="center" alt="..." />
  ```
</ParamField>

<ParamField path="background" type="string">
  Background color to use when converting transparent images to non-transparent formats.

  ```astro theme={null}
  <Image src={img} background="#ffffff" format="jpg" alt="..." />
  ```
</ParamField>

<ParamField path="inferSize" type="boolean">
  Automatically infer width and height from the source image.

  **Default:** `true` for local images

  ```astro theme={null}
  <Image src={img} inferSize alt="..." />
  ```
</ParamField>

### HTML Attributes

All standard HTML `<img>` attributes are also supported:

```astro theme={null}
<Image 
  src={img} 
  alt="Description"
  class="rounded-lg shadow-md"
  loading="lazy"
  decoding="async"
/>
```

### Example

```astro theme={null}
---
import { Image } from 'astro:assets';
import heroImage from '../assets/hero.jpg';
---

<Image 
  src={heroImage}
  width={800}
  height={600}
  format="webp"
  quality="high"
  alt="Hero image showing our product"
  class="hero-image"
/>
```

## Picture Component

The `<Picture />` component generates a `<picture>` element with multiple sources and formats.

```astro theme={null}
import { Picture } from 'astro:assets';
import myImage from '../assets/my-image.png';

<Picture 
  src={myImage} 
  formats={['avif', 'webp']} 
  alt="A description" 
/>
```

### Props

<ParamField path="formats" type="ImageOutputFormat[]">
  Array of image formats to generate. The component will create a `<source>` for each format.

  **Default:** `['webp']`

  ```astro theme={null}
  <Picture src={img} formats={['avif', 'webp', 'png']} alt="..." />
  ```
</ParamField>

<ParamField path="fallbackFormat" type="ImageOutputFormat">
  Format to use for the fallback `<img>` element.

  **Default:** `'png'`

  ```astro theme={null}
  <Picture src={img} formats={['avif', 'webp']} fallbackFormat="jpg" alt="..." />
  ```
</ParamField>

<ParamField path="pictureAttributes" type="HTMLAttributes">
  Attributes to apply to the `<picture>` element.

  ```astro theme={null}
  <Picture 
    src={img} 
    pictureAttributes={{ class: 'picture-wrapper' }}
    alt="..." 
  />
  ```
</ParamField>

All other props from the `Image` component are also supported.

### Example

```astro theme={null}
---
import { Picture } from 'astro:assets';
import headerImage from '../assets/header.png';
---

<Picture
  src={headerImage}
  formats={['avif', 'webp']}
  widths={[400, 800, 1200]}
  sizes="(max-width: 800px) 100vw, 800px"
  alt="Page header image"
  class="header-image"
/>
```

## getImage

Programmatically get optimized image data without rendering an `<img>` tag.

```typescript theme={null}
getImage(options: ImageTransform): Promise<GetImageResult>
```

<ParamField path="options" type="ImageTransform" required>
  Image transformation options.

  <ParamField path="src" type="ImageMetadata | string" required>
    Image source.
  </ParamField>

  <ParamField path="width" type="number">
    Desired width.
  </ParamField>

  <ParamField path="height" type="number">
    Desired height.
  </ParamField>

  <ParamField path="format" type="ImageOutputFormat">
    Output format.
  </ParamField>

  <ParamField path="quality" type="ImageQuality">
    Image quality.
  </ParamField>

  All other Image component props are also supported.
</ParamField>

<ResponseField name="return" type="Promise<GetImageResult>">
  Object containing optimized image data.

  <ResponseField name="src" type="string">
    URL of the optimized image.
  </ResponseField>

  <ResponseField name="srcSet" type="object">
    Srcset data for responsive images.

    <ResponseField name="values" type="SrcSetValue[]">
      Array of srcset values.
    </ResponseField>

    <ResponseField name="attribute" type="string">
      Formatted srcset attribute string.
    </ResponseField>
  </ResponseField>

  <ResponseField name="attributes" type="Record<string, any>">
    HTML attributes for the image (width, height, etc.).
  </ResponseField>

  <ResponseField name="options" type="ImageTransform">
    Processed transformation options.
  </ResponseField>
</ResponseField>

### Example

```astro theme={null}
---
import { getImage } from 'astro:assets';
import myImage from '../assets/my-image.png';

const optimizedImage = await getImage({
  src: myImage,
  width: 800,
  format: 'webp',
  quality: 'high',
});
---

<img src={optimizedImage.src} {...optimizedImage.attributes} alt="..." />

<!-- Use in CSS -->
<style define:vars={{ bgImage: `url(${optimizedImage.src})` }}>
  .hero {
    background-image: var(--bgImage);
  }
</style>

<!-- Generate Open Graph images -->
<meta property="og:image" content={optimizedImage.src} />
```

## ImageMetadata Type

Type for imported images.

```typescript theme={null}
type ImageMetadata = {
  src: string;
  width: number;
  height: number;
  format: 'png' | 'jpg' | 'jpeg' | 'tiff' | 'webp' | 'gif' | 'svg' | 'avif';
};
```

### Example

```astro theme={null}
---
import type { ImageMetadata } from 'astro';
import logo from '../assets/logo.png';

const image: ImageMetadata = logo;
console.log(image.width); // 800
console.log(image.height); // 600
console.log(image.format); // 'png'
---
```

## Remote Images

To use remote images, configure `image.domains` or `image.remotePatterns` in your Astro config:

```javascript theme={null}
// astro.config.mjs
export default defineConfig({
  image: {
    domains: ['example.com'],
    remotePatterns: [{ protocol: 'https' }],
  },
});
```

Then use remote URLs with the Image component:

```astro theme={null}
<Image 
  src="https://example.com/image.jpg"
  width={800}
  height={600}
  alt="Remote image"
/>
```
