Skip to content

Instantly share code, notes, and snippets.

@jllahi
Last active December 7, 2023 02:28
Show Gist options
  • Select an option

  • Save jllahi/c747e5b7b3dd8c182223d53910ef60ed to your computer and use it in GitHub Desktop.

Select an option

Save jllahi/c747e5b7b3dd8c182223d53910ef60ed to your computer and use it in GitHub Desktop.
Astro Assets - Image and Picture
export default defineConfig({
image: {
remotePatterns: [{ protocol: "https" }],
},
})
---
import { Image } from 'astro:assets';
import myImage from "../assets/my_image.png"; // Image is 1600x900
---
<Image
src={myImage}
widths={[240, 540, 720, myImage.width]}
sizes={`(max-width: 360px) 240px, (max-width: 720px) 540px, (max-width: 1600px) 720px, ${myImage.width}px`}
densities={[1.5, 2]}
alt="A description of my image."
class="object-cover"
/>
---
import ReactComponent from './ReactComponent.jsx';
import { Image } from "astro:assets"
import stars from "~/stars/docline.png";
---
<ReactComponent>
<Image src={stars} alt="A starry night sky." />
</ReactComponent>
---
import { getImage } from "astro:assets";
import myBackground from "../background.png"
const optimizedBackground = await getImage({src: myBackground, format: 'avif'})
---
<div style={`background-image: url(${optimizedBackground.src});`}></div>
---
import { Picture } from 'astro:assets';
import myImage from "../assets/my_image.png"; // Image is 1600x900
---
<Picture
src={myImage}
width={myDog.width}
height={myDog.height}
formats={['avif', 'webp']}
alt="A description of my image."
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment