Astro 提供了强大的图片优化功能,可以自动处理图片的响应式、格式转换、压缩等任务,显著提升网站性能。
核心功能:
- 自动响应式图片:自动生成多个尺寸的图片
- 格式转换:自动转换为现代图片格式(WebP、AVIF)
- 懒加载:自动实现图片懒加载
- 压缩优化:自动压缩图片减少文件大小
基本用法:
astro--- import { Image } from 'astro:assets'; import myImage from '../assets/my-image.jpg'; --- <!-- 基本使用 --> <Image src={myImage} alt="描述文字" /> <!-- 指定宽度和高度 --> <Image src={myImage} alt="描述文字" width={800} height={600} /> <!-- 指定格式 --> <Image src={myImage} alt="描述文字" format="webp" /> <!-- 指定质量 --> <Image src={myImage} alt="描述文字" quality={80} />
高级配置:
astro--- import { Image } from 'astro:assets'; import heroImage from '../assets/hero.jpg'; --- <Image src={heroImage} alt="Hero Image" widths={[400, 800, 1200]} sizes="(max-width: 768px) 100vw, 50vw" formats={['avif', 'webp', 'jpeg']} loading="lazy" decoding="async" priority={false} />
配置选项说明:
widths:生成多个宽度的图片版本sizes:指定不同屏幕宽度下的图片显示尺寸formats:指定输出格式(按优先级)loading:加载策略("eager" 或 "lazy")decoding:解码策略("sync" 或 "async")priority:是否为优先加载的图片
远程图片:
astro--- import { Image } from 'astro:assets'; import { getImage } from 'astro:assets'; // 处理远程图片 const remoteImage = await getImage({ src: 'https://example.com/image.jpg', alt: 'Remote Image', width: 800, height: 600, format: 'webp', }); --- <img {...remoteImage.attributes} />
配置远程图片域名:
javascript// astro.config.mjs import { defineConfig } from 'astro/config'; import react from '@astrojs/react'; export default defineConfig({ integrations: [react()], image: { // 允许的远程图片域名 remotePatterns: [ { protocol: 'https', hostname: 'example.com', }, { protocol: 'https', hostname: '**.cdn.example.com', }, ], }, });
图片服务(Image Service):
Astro 支持多种图片服务:
javascript// astro.config.mjs import { defineConfig } from 'astro/config'; import sharp from 'astro/assets/services/sharp'; import imagetools from 'astro/assets/services/imagetools'; export default defineConfig({ image: { // 使用 Sharp(默认,性能最佳) service: sharp, // 或使用 ImageTools(更多功能) // service: imagetools, }, });
背景图片:
astro--- import { Image } from 'astro:assets'; import backgroundImage from '../assets/bg.jpg'; const { src: bgSrc } = await getImage({ src: backgroundImage, format: 'webp', width: 1920, }); --- <div style={`background-image: url('${bgSrc}');`} class="hero-section" > <h1>Hero Section</h1> </div>
图片画廊示例:
astro--- import { Image } from 'astro:assets'; import { getCollection } from 'astro:content'; const gallery = await getCollection('gallery'); --- <div class="gallery"> {gallery.map(item => ( <figure> <Image src={item.data.image} alt={item.data.title} widths={[300, 600, 900]} sizes="(max-width: 600px) 100vw, 50vw" loading="lazy" /> <figcaption>{item.data.title}</figcaption> </figure> ))} </div> <style> .gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 1rem; } figure { margin: 0; } img { width: 100%; height: auto; } </style>
性能优化技巧:
-
使用正确的格式优先级:
astro<Image src={image} formats={['avif', 'webp', 'jpeg']} /> -
合理的尺寸设置:
astro<Image src={image} widths={[400, 800, 1200, 1600]} sizes="(max-width: 768px) 100vw, 50vw" /> -
优先加载关键图片:
astro<Image src={heroImage} loading="eager" priority={true} /> -
懒加载非关键图片:
astro<Image src={image} loading="lazy" decoding="async" /> -
控制图片质量:
astro<Image src={image} quality={75} />
与内容集合集成:
markdown--- title: "我的文章" image: ./hero.jpg --- 文章内容...
astro--- import { Image } from 'astro:assets'; import { getEntry } from 'astro:content'; const post = await getEntry('blog', 'my-post'); const { image } = post.data; --- <Image src={image} alt={post.data.title} widths={[800, 1200, 1600]} />
最佳实践:
- 默认使用
<Image>组件而不是<img>标签 - 为所有图片提供有意义的 alt 文本
- 根据图片用途设置合适的加载策略
- 使用
widths和sizes实现真正的响应式 - 优先使用现代图片格式(AVIF、WebP)
- 为远程图片配置域名白名单
- 在构建时处理图片,避免运行时开销
Astro 的图片优化功能可以显著提升网站性能,改善用户体验,同时保持简单的开发体验。