关于 image-optimization
image-optimization 是面向开发者的 Web 图像优化技能,涵盖格式选择、响应式图像交付和基于 Sharp 的构建示例,并提供体积目标与性能检查清单。
主要功能
- 01按图像类型选择格式
- 02响应式图像技术
- 03基于 Sharp 转换为 WebP
- 04图像体积目标
- 05优化检查清单
适用场景
- 01为网页准备图像
- 02减少图像传输体积
- 03设置响应式图像交付
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ secondsky/ claude-skills --skill image-optimization安装后在 Claude Code 中输入/image-optimization确认已加载。
详细介绍
图像优化
通过现代格式和响应式技术优化图像,提升 Web 性能。
格式选择
| 格式 | 最适用于 | 压缩率 |
|---|---|---|
| JPEG | 照片 | 有损,可减少 50-70% |
| PNG | 图标、透明图像 | 无损,可减少 10-30% |
| WebP | 现代浏览器 | 比 JPEG 好 25-35% |
| AVIF | 新一代格式 | 比 JPEG 好 50% |
| SVG | 徽标、图标 | 矢量、可缩放 |
响应式图像
<picture>
<source srcset="image.avif" type="image/avif">
<source srcset="image.webp" type="image/webp">
<img
src="image.jpg"
srcset="image-400.jpg 400w, image-800.jpg 800w, image-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw, 50vw"
alt="Description"
loading="lazy"
decoding="async"
>
</picture>
延迟加载
<img src="image.jpg" loading="lazy" alt="Description">
<img
src="placeholder-blur.jpg"
data-src="image.jpg"
class="lazy"
alt="Description"
>
构建流水线(Sharp)
const sharp = require('sharp');
async function optimizeImage(input, output) {
await sharp(input)
.resize(1200, null, { withoutEnlargement: true })
.webp({ quality: 80 })
.toFile(output);
}
性能目标
| 资源类型 | 目标大小 |
|---|---|
| 主视觉图像 | <200KB |
| 缩略图 | <30KB |
| 图像总计 | <500KB |
优化清单
- 使用 WebP,并提供 JPEG 回退方案
- 实现响应式 srcset
- 为首屏以下的内容启用延迟加载
- 将压缩质量设为 70-85
- 从 CDN 提供资源
- 设置适当的缓存标头
评论
支持 Markdown