1
1
---
2
- source-updated-at : 2025-05-22T15:18:56 .000Z
3
- translation-updated-at : 2025-05-23T16:44:15.624Z
2
+ source-updated-at : 2025-06-02T15:30:01 .000Z
3
+ translation-updated-at : 2025-06-02T19:01:22.562Z
4
4
title : 如何优化图片
5
5
nav_title : 图片
6
- description : 学习如何在 Next.js 中优化图片
6
+ description : 了解如何在 Next.js 中优化图片
7
7
related :
8
8
title : API 参考
9
- description : 查看 Next.js Image 完整功能集的 API 参考文档。
9
+ description : 查看 Next.js Image 完整功能的 API 参考文档。
10
10
links :
11
11
- app/api-reference/components/image
12
12
---
13
13
14
14
Next.js 的 [ ` <Image> ` ] ( /docs/app/api-reference/components/image ) 组件扩展了 HTML ` <img> ` 元素,提供以下功能:
15
15
16
- - ** 尺寸优化** :自动为不同设备提供正确尺寸的图片,并使用 WebP 等现代图片格式。
17
- - ** 视觉稳定性** :在图片加载时自动防止[ 布局偏移 (layout shift )] ( https://web.dev/articles/cls ) 。
18
- - ** 更快页面加载** :仅当图片进入视口时通过原生浏览器懒加载加载图片,并支持可选的模糊占位图 。
16
+ - ** 尺寸优化** :自动为不同设备提供正确尺寸的图片,使用 WebP 等现代图片格式。
17
+ - ** 视觉稳定性** :在图片加载时自动防止[ 布局偏移 (CLS )] ( https://web.dev/articles/cls ) 。
18
+ - ** 更快页面加载** :仅当图片进入视口时通过原生浏览器懒加载进行加载,可选模糊占位图 。
19
19
- ** 资源灵活性** :按需调整图片尺寸,即使是远程服务器存储的图片。
20
20
21
21
要开始使用 ` <Image> ` ,请从 ` next/image ` 导入并在组件中渲染它。
@@ -36,16 +36,16 @@ export default function Page() {
36
36
}
37
37
```
38
38
39
- ` src ` 属性可以是[ 本地图片] ( #local-images ) 或[ 远程图片] ( #remote-images ) 。
39
+ ` src ` 属性可以是[ 本地图片] ( #本地图片 ) 或[ 远程图片] ( #远程图片 ) 。
40
40
41
- > ** 🎥 观看视频** :了解更多关于如何使用 ` next/image ` → [ YouTube (9 分钟 )] ( https://youtu.be/IU_qq_c_lKA ) 。
41
+ > ** 🎥 观看视频** :了解更多关于如何使用 ` next/image ` → [ YouTube (9分钟 )] ( https://youtu.be/IU_qq_c_lKA ) 。
42
42
43
43
## 本地图片
44
44
45
- 您可以将静态文件(如图片和字体)存储在根目录下的 [ ` public ` ] ( /docs/app/api-reference/file-conventions/public-folder ) 文件夹中。` public ` 中的文件可以通过代码从基础 URL (` / ` ) 开始引用。
45
+ 您可以将静态文件(如图片和字体)存储在根目录下的 [ ` public ` ] ( /docs/app/api-reference/file-conventions/public-folder ) 文件夹中。` public ` 内的文件可以通过代码从基础 URL (` / ` ) 开始引用。
46
46
47
47
<Image
48
- alt = " 显示 app 和 public 文件夹的目录结构 "
48
+ alt = " 展示 app 和 public 文件夹结构的示意图 "
49
49
srcLight = " /docs/light/public-folder.png"
50
50
srcDark = " /docs/dark/public-folder.png"
51
51
width = " 1600"
@@ -59,34 +59,70 @@ export default function Page() {
59
59
return (
60
60
<Image
61
61
src = " /profile.png"
62
- alt = " 作者的照片"
62
+ alt = " 作者照片"
63
+ width = { 500 }
64
+ height = { 500 }
65
+ />
66
+ )
67
+ }
68
+ ```
69
+
70
+ ``` jsx filename="app/page.js" switcher
71
+ import Image from ' next/image'
72
+
73
+ export default function Page () {
74
+ return (
75
+ < Image
76
+ src= " /profile.png"
77
+ alt= " 作者照片"
78
+ width= {500 }
79
+ height= {500 }
80
+ / >
81
+ )
82
+ }
83
+ ```
84
+
85
+ ### 静态导入图片
86
+
87
+ 您也可以导入并使用本地图片文件。Next.js 会根据导入的文件自动确定图片的固有 [ ` width ` ] ( /docs/app/api-reference/components/image#width-and-height ) 和 [ ` height ` ] ( /docs/app/api-reference/components/image#width-and-height ) 。这些值用于确定图片比例并在加载时防止[ 累积布局偏移 (CLS)] ( https://web.dev/articles/cls ) 。
88
+
89
+ ``` tsx filename="app/page.tsx" switcher
90
+ import Image from ' next/image'
91
+ import ProfileImage from ' ./profile.png'
92
+
93
+ export default function Page() {
94
+ return (
95
+ <Image
96
+ src = { ProfileImage }
97
+ alt = " 作者照片"
63
98
// width={500} 自动提供
64
99
// height={500} 自动提供
65
100
// blurDataURL="data:..." 自动提供
66
- // placeholder="blur" // 可选加载时的模糊效果
101
+ // placeholder="blur" // 加载时可选的模糊效果
67
102
/>
68
103
)
69
104
}
70
105
```
71
106
72
107
``` jsx filename="app/page.js" switcher
73
108
import Image from ' next/image'
109
+ import ProfileImage from ' ./profile.png'
74
110
75
111
export default function Page () {
76
112
return (
77
113
< Image
78
- src= " /profile.png "
79
- alt= " 作者的照片 "
114
+ src= {ProfileImage}
115
+ alt= " 作者照片 "
80
116
// width={500} 自动提供
81
117
// height={500} 自动提供
82
118
// blurDataURL="data:..." 自动提供
83
- // placeholder="blur" // 可选加载时的模糊效果
119
+ // placeholder="blur" // 加载时可选的模糊效果
84
120
/ >
85
121
)
86
122
}
87
123
```
88
124
89
- 使用本地图片时 ,Next.js 会根据导入的文件自动确定图片的固有 [ ` width ` ] ( /docs/ app/api-reference/components/image#width-and-height ) 和 [ ` height ` ] ( /docs/app/api-reference/components/image#width-and-height ) 。这些值用于确定图片比例,并在图片加载时防止 [ 累积布局偏移 (Cumulative Layout Shift) ] ( https://web.dev/articles/cls ) 。
125
+ 在这种情况下 ,Next.js 期望 ` app/profile.png ` 文件是可用的 。
90
126
91
127
## 远程图片
92
128
@@ -99,7 +135,7 @@ export default function Page() {
99
135
return (
100
136
<Image
101
137
src = " https://s3.amazonaws.com/my-bucket/profile.png"
102
- alt = " 作者的照片 "
138
+ alt = " 作者照片 "
103
139
width = { 500 }
104
140
height = { 500 }
105
141
/>
@@ -114,7 +150,7 @@ export default function Page() {
114
150
return (
115
151
< Image
116
152
src= " https://s3.amazonaws.com/my-bucket/profile.png"
117
- alt= " 作者的照片 "
153
+ alt= " 作者照片 "
118
154
width= {500 }
119
155
height= {500 }
120
156
/ >
0 commit comments