Skip to content

Commit 9d7d609

Browse files
authored
docs: add image optimization documentation for theme development (#405)
添加主题开发的图片优化文档。 /kind documentation ```release-note None ```
1 parent 587f252 commit 9d7d609

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: 图片优化
3+
description: 本文档介绍如何使用 Halo 的缩略图特性来优化图片。
4+
---
5+
6+
从 Halo 2.19 开始,Halo 支持了附件图片缩略图生成功能。通过缩略图功能,可以在不改变原图的情况下,生成预设尺寸的缩略图,以减少图片的大小,提高页面加载速度。
7+
8+
此文档将介绍如何在 Halo 的主题模板中利用此功能结合浏览器 [响应式图片](https://developer.mozilla.org/zh-CN/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images) 的特性来优化图片资源。
9+
10+
## 为什么使用响应式图片?
11+
12+
- 响应式设计:支持根据不同设备的分辨率和屏幕大小,自动选择最佳的图片尺寸。这种机制确保了在高分辨率设备上显示高质量图片的同时,也能够在低分辨率设备上节省带宽。
13+
- 提升加载性能:通过为图片提供多个尺寸的缩略图,浏览器可以选择最适合当前视窗的图片进行加载,从而减少不必要的带宽使用,提升页面加载速度,改善用户体验。
14+
- 兼容性好:响应式图片是基于 HTML 标准的实现,不需要额外的 JavaScript 或 CSS,因此兼容性非常好。
15+
16+
:::info
17+
建议详细阅读 [响应式图片](https://developer.mozilla.org/zh-CN/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images) 文档,以了解更多关于响应式图片的知识,以及如何在不同场景下使用。
18+
:::
19+
20+
## Finder API
21+
22+
### `thumbnail.gen(uri, size)`
23+
24+
获取指定图片和尺寸的缩略图地址。
25+
26+
#### 参数
27+
28+
- `uri:string` - 图片地址。
29+
- `size:string` - 缩略图尺寸,支持以下值:
30+
- `s` - 宽度 400px
31+
- `m` - 宽度 800px
32+
- `l` - 宽度 1200px
33+
- `xl` - 宽度 1600px
34+
35+
#### 示例
36+
37+
```html
38+
<img
39+
th:src="${post.spec.cover}"
40+
th:srcset="|${thumbnail.gen(post.spec.cover, 's')} 400w,
41+
${thumbnail.gen(post.spec.cover, 'm')} 800w,
42+
${thumbnail.gen(post.spec.cover, 'l')} 1200w,
43+
${thumbnail.gen(post.spec.cover, 'xl')} 1600w|"
44+
sizes="(max-width: 1600px) 100vw, 1600px"
45+
/>
46+
```
47+
48+
:::info
49+
文章内容无需在主题模板中处理,Halo 会自动为文章内容中的图片生成响应式图片的 HTML 代码。
50+
:::
51+
52+
## HTTP API
53+
54+
如果你需要在主题模板之外的地方生成缩略图地址,比如异步加载图片的场景下,可以使用 HTTP API。
55+
56+
### 接口地址
57+
58+
`GET /apis/api.storage.halo.run/v1alpha1/thumbnails/-/via-uri?uri=${uri}&size={size}`
59+
60+
#### 参数
61+
62+
- `uri:string` - 图片地址。
63+
- `size:string` - 缩略图尺寸,支持以下值:
64+
- `s` - 宽度 400px
65+
- `m` - 宽度 800px
66+
- `l` - 宽度 1200px
67+
- `xl` - 宽度 1600px
68+
69+
#### 示例
70+
71+
```html
72+
<img
73+
src="/upload/post-cover.png"
74+
srcset="|/apis/api.storage.halo.run/v1alpha1/thumbnails/-/via-uri?uri=/upload/post-cover.png&size=s 400w,
75+
/apis/api.storage.halo.run/v1alpha1/thumbnails/-/via-uri?uri=/upload/post-cover.png&size=m 800w,
76+
/apis/api.storage.halo.run/v1alpha1/thumbnails/-/via-uri?uri=/upload/post-cover.png&size=l 1200w,
77+
/apis/api.storage.halo.run/v1alpha1/thumbnails/-/via-uri?uri=/upload/post-cover.png&size=xl 1600w|"
78+
sizes="(max-width: 1600px) 100vw, 1600px"
79+
/>
80+
```

sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ module.exports = {
338338
"developer-guide/theme/finder-apis/plugin",
339339
],
340340
},
341+
"developer-guide/theme/image-optimization",
341342
"developer-guide/theme/global-variables",
342343
"developer-guide/theme/template-tag",
343344
"developer-guide/theme/code-snippets",

0 commit comments

Comments
 (0)