> ## 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.

# Asset Handling

> Learn how to optimize and manage images and other assets in Astro

Astro provides powerful built-in asset handling for images and other media. The assets system optimizes images automatically, generates multiple formats, and provides a great developer experience with type safety.

## Image Component

The `Image` component from `astro:assets` optimizes images at build time with automatic format conversion, resizing, and lazy loading.

```astro src/pages/index.astro theme={null}
---
import { Image } from 'astro:assets';
import heroImage from '../assets/hero.png';
---

<Image src={heroImage} alt="Hero image" />
```

<Note>
  Images are optimized during the build process, generating multiple sizes and formats for better performance.
</Note>

### Local Images

Import local images from your project to get type safety and automatic optimization:

```astro theme={null}
---
import { Image } from 'astro:assets';
import profilePic from '../assets/profile.jpg';
import logo from '../assets/logo.png';
---

<Image 
  src={profilePic} 
  alt="Profile picture"
  width={300}
  height={300}
/>

<Image 
  src={logo} 
  alt="Company logo"
  format="webp"
  quality="high"
/>
```

### Remote Images

For remote images, specify dimensions explicitly:

```astro theme={null}
---
import { Image } from 'astro:assets';
---

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

<Warning>
  Remote images require explicit `width` and `height` attributes. Configure allowed remote domains in your config.
</Warning>

## Image Properties

The `Image` component accepts various properties for optimization:

<ParamField path="src" type="ImageMetadata | string" required>
  Image source - local import or remote URL
</ParamField>

<ParamField path="alt" type="string" required>
  Alternative text for accessibility
</ParamField>

<ParamField path="width" type="number">
  Target width in pixels. Required for remote images.
</ParamField>

<ParamField path="height" type="number">
  Target height in pixels. Required for remote images.
</ParamField>

<ParamField path="format" type="'avif' | 'webp' | 'png' | 'jpg' | 'svg'">
  Output format. Defaults to optimized format.
</ParamField>

<ParamField path="quality" type="number | 'low' | 'mid' | 'high' | 'max'">
  Compression quality. Default: 'mid'
</ParamField>

<ParamField path="densities" type="number[]">
  Pixel density descriptors for responsive images
</ParamField>

<ParamField path="widths" type="number[]">
  Generate multiple widths for srcset
</ParamField>

### Example with All Properties

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

<Image
  src={banner}
  alt="Product banner"
  width={1200}
  height={600}
  format="webp"
  quality="high"
  loading="lazy"
  decoding="async"
  class="banner-image"
/>
```

## Picture Component

Use the `Picture` component for art direction and multiple formats:

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

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

This generates:

* Multiple format versions (AVIF, WebP, JPEG)
* Multiple sizes for responsive images
* Automatic `<picture>` element with `<source>` tags

<Tip>
  Use `Picture` for responsive images with art direction. Use `Image` for simple cases.
</Tip>

## Image Service

Astro uses Sharp by default for image optimization. You can configure or replace the image service:

```js astro.config.mjs theme={null}
import { defineConfig } from 'astro/config';

export default defineConfig({
  image: {
    service: {
      entrypoint: 'astro/assets/services/sharp',
      config: {
        limitInputPixels: false
      }
    },
    // Configure remote image domains
    domains: ['images.example.com'],
    remotePatterns: [{ protocol: 'https' }]
  }
});
```

### Available Services

<Tabs>
  <Tab title="Sharp (Default)">
    High-performance Node.js image processing:

    ```js theme={null}
    import { defineConfig } from 'astro/config';

    export default defineConfig({
      image: {
        service: {
          entrypoint: 'astro/assets/services/sharp'
        }
      }
    });
    ```
  </Tab>

  <Tab title="No-op Service">
    Disables image optimization:

    ```js theme={null}
    import { defineConfig } from 'astro/config';

    export default defineConfig({
      image: {
        service: {
          entrypoint: 'astro/assets/services/noop'
        }
      }
    });
    ```
  </Tab>
</Tabs>

## getImage API

For programmatic image optimization, use the `getImage` function:

```astro theme={null}
---
import { getImage } from 'astro:assets';
import background from '../assets/background.jpg';

const optimizedImage = await getImage({
  src: background,
  width: 1920,
  height: 1080,
  format: 'webp',
  quality: 'high'
});
---

<div style={`background-image: url(${optimizedImage.src})`}>
  Content here
</div>
```

### Return Value

The `getImage` function returns an object with:

```typescript theme={null}
interface GetImageResult {
  src: string;              // Optimized image URL
  srcSet: {
    values: string[];       // srcset values
    attribute: string;      // Complete srcset attribute
  };
  attributes: Record<string, any>; // HTML attributes
  options: ImageTransform;  // Transform options used
}
```

## Images in Markdown

Reference images in Markdown and MDX files:

```markdown src/content/blog/post.md theme={null}
---
title: My Post
---

![Alt text](../../assets/image.jpg)
```

Or use the Image component in MDX:

```mdx src/content/blog/post.mdx theme={null}
---
title: My Post
---
import { Image } from 'astro:assets';
import screenshot from '../../assets/screenshot.png';

# My Post

<Image src={screenshot} alt="App screenshot" />
```

## Responsive Images

Generate responsive images with multiple sizes:

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

<Image
  src={product}
  alt="Product image"
  widths={[400, 800, 1200]}
  sizes="(max-width: 600px) 400px, (max-width: 1200px) 800px, 1200px"
/>
```

This generates:

```html theme={null}
<img
  src="/product-800.webp"
  sizes="(max-width: 600px) 400px, (max-width: 1200px) 800px, 1200px"
  alt="Product image"
/>
```

## Image Layouts

Control how images scale and fit:

<CodeGroup>
  ```astro Constrained (Default) theme={null}
  <Image
    src={image}
    alt="Constrained image"
    width={800}
    layout="constrained"
  />
  ```

  ```astro Fixed theme={null}
  <Image
    src={image}
    alt="Fixed size image"
    width={400}
    height={400}
    layout="fixed"
  />
  ```

  ```astro Full Width theme={null}
  <Image
    src={image}
    alt="Full width image"
    layout="full-width"
  />
  ```
</CodeGroup>

<AccordionGroup>
  <Accordion title="Constrained">
    Image scales down on smaller screens but never exceeds its specified width.
    Best for most use cases.
  </Accordion>

  <Accordion title="Fixed">
    Image maintains exact dimensions at all screen sizes.
    Best for avatars, icons, and UI elements.
  </Accordion>

  <Accordion title="Full Width">
    Image always fills its container width.
    Best for hero images and banners.
  </Accordion>
</AccordionGroup>

## Background Images

Optimize background images using `getImage`:

```astro theme={null}
---
import { getImage } from 'astro:assets';
import bg from '../assets/background.jpg';

const backgroundImage = await getImage({
  src: bg,
  format: 'webp',
  quality: 80
});
---

<section style={`background-image: url(${backgroundImage.src})`}>
  <h1>Hero Section</h1>
</section>

<style>
  section {
    background-size: cover;
    background-position: center;
  }
</style>
```

## Other Assets

### Fonts

Import and reference fonts:

```astro theme={null}
---
import fontFile from '../assets/fonts/custom-font.woff2';
---

<style>
  @font-face {
    font-family: 'CustomFont';
    src: url({fontFile}) format('woff2');
  }
</style>
```

### Videos

Reference video files:

```astro theme={null}
---
import video from '../assets/demo.mp4';
---

<video controls>
  <source src={video} type="video/mp4" />
</video>
```

### Public Directory

For assets that shouldn't be processed, use the `public/` directory:

```
public/
  favicon.ico
  robots.txt
  static-image.png
```

Reference them with absolute paths:

```html theme={null}
<img src="/static-image.png" alt="Static image" />
<link rel="icon" href="/favicon.ico" />
```

<Note>
  Files in `public/` are copied as-is to the build output without processing.
</Note>

## Performance Best Practices

<Steps>
  <Step title="Use modern formats">
    Enable AVIF and WebP for better compression:

    ```astro theme={null}
    <Picture
      src={image}
      formats={['avif', 'webp', 'jpg']}
      alt="Optimized image"
    />
    ```
  </Step>

  <Step title="Lazy load off-screen images">
    ```astro theme={null}
    <Image
      src={image}
      alt="Lazy loaded"
      loading="lazy"
    />
    ```
  </Step>

  <Step title="Set appropriate quality">
    ```astro theme={null}
    <Image
      src={image}
      quality="mid" // or 70-80 for custom
      alt="Quality optimized"
    />
    ```
  </Step>

  <Step title="Generate responsive sizes">
    ```astro theme={null}
    <Image
      src={image}
      widths={[400, 800, 1200]}
      sizes="(max-width: 640px) 400px, (max-width: 1024px) 800px, 1200px"
      alt="Responsive image"
    />
    ```
  </Step>
</Steps>

## TypeScript Support

Images imports are fully typed:

```typescript theme={null}
import type { ImageMetadata } from 'astro';
import image from '../assets/image.jpg';

// image is typed as ImageMetadata
const metadata: ImageMetadata = image;

console.log(metadata.width);  // number
console.log(metadata.height); // number
console.log(metadata.format); // string
console.log(metadata.src);    // string
```

## Related Resources

<CardGroup cols={2}>
  <Card title="Markdown" icon="markdown" href="/features/markdown">
    Use images in Markdown content
  </Card>

  <Card title="Content Collections" icon="books" href="/concepts/content-collections">
    Manage images in collections
  </Card>

  <Card title="Configuration" icon="gear" href="/config/astro-config">
    Configure image service
  </Card>

  <Card title="Performance" icon="gauge-high" href="/guides/performance">
    Performance optimization guide
  </Card>
</CardGroup>
