Last active
December 7, 2023 02:28
-
-
Save jllahi/c747e5b7b3dd8c182223d53910ef60ed to your computer and use it in GitHub Desktop.
Astro Assets - Image and Picture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default defineConfig({ | |
| image: { | |
| remotePatterns: [{ protocol: "https" }], | |
| }, | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| 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" | |
| /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| 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