11---
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
44title : 如何优化图片
55nav_title : 图片
6- description : 学习如何在 Next.js 中优化图片
6+ description : 了解如何在 Next.js 中优化图片
77related :
88 title : API 参考
9- description : 查看 Next.js Image 完整功能集的 API 参考文档。
9+ description : 查看 Next.js Image 完整功能的 API 参考文档。
1010 links :
1111 - app/api-reference/components/image
1212---
1313
1414Next.js 的 [ ` <Image> ` ] ( /docs/app/api-reference/components/image ) 组件扩展了 HTML ` <img> ` 元素,提供以下功能:
1515
16- - ** 尺寸优化** :自动为不同设备提供正确尺寸的图片,并使用 WebP 等现代图片格式。
17- - ** 视觉稳定性** :在图片加载时自动防止[ 布局偏移 (layout shift )] ( https://web.dev/articles/cls ) 。
18- - ** 更快页面加载** :仅当图片进入视口时通过原生浏览器懒加载加载图片,并支持可选的模糊占位图 。
16+ - ** 尺寸优化** :自动为不同设备提供正确尺寸的图片,使用 WebP 等现代图片格式。
17+ - ** 视觉稳定性** :在图片加载时自动防止[ 布局偏移 (CLS )] ( https://web.dev/articles/cls ) 。
18+ - ** 更快页面加载** :仅当图片进入视口时通过原生浏览器懒加载进行加载,可选模糊占位图 。
1919- ** 资源灵活性** :按需调整图片尺寸,即使是远程服务器存储的图片。
2020
2121要开始使用 ` <Image> ` ,请从 ` next/image ` 导入并在组件中渲染它。
@@ -36,16 +36,16 @@ export default function Page() {
3636}
3737```
3838
39- ` src ` 属性可以是[ 本地图片] ( #local-images ) 或[ 远程图片] ( #remote-images ) 。
39+ ` src ` 属性可以是[ 本地图片] ( #本地图片 ) 或[ 远程图片] ( #远程图片 ) 。
4040
41- > ** 🎥 观看视频** :了解更多关于如何使用 ` next/image ` → [ YouTube (9 分钟 )] ( https://youtu.be/IU_qq_c_lKA ) 。
41+ > ** 🎥 观看视频** :了解更多关于如何使用 ` next/image ` → [ YouTube (9分钟 )] ( https://youtu.be/IU_qq_c_lKA ) 。
4242
4343## 本地图片
4444
45- 您可以将静态文件(如图片和字体)存储在根目录下的 [ ` public ` ] ( /docs/app/api-reference/file-conventions/public-folder ) 文件夹中。` public ` 中的文件可以通过代码从基础 URL (` / ` ) 开始引用。
45+ 您可以将静态文件(如图片和字体)存储在根目录下的 [ ` public ` ] ( /docs/app/api-reference/file-conventions/public-folder ) 文件夹中。` public ` 内的文件可以通过代码从基础 URL (` / ` ) 开始引用。
4646
4747<Image
48- alt = " 显示 app 和 public 文件夹的目录结构 "
48+ alt = " 展示 app 和 public 文件夹结构的示意图 "
4949 srcLight = " /docs/light/public-folder.png"
5050 srcDark = " /docs/dark/public-folder.png"
5151 width = " 1600"
@@ -59,34 +59,70 @@ export default function Page() {
5959 return (
6060 <Image
6161 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 = " 作者照片"
6398 // width={500} 自动提供
6499 // height={500} 自动提供
65100 // blurDataURL="data:..." 自动提供
66- // placeholder="blur" // 可选加载时的模糊效果
101+ // placeholder="blur" // 加载时可选的模糊效果
67102 />
68103 )
69104}
70105```
71106
72107``` jsx filename="app/page.js" switcher
73108import Image from ' next/image'
109+ import ProfileImage from ' ./profile.png'
74110
75111export default function Page () {
76112 return (
77113 < Image
78- src= " /profile.png "
79- alt= " 作者的照片 "
114+ src= {ProfileImage}
115+ alt= " 作者照片 "
80116 // width={500} 自动提供
81117 // height={500} 自动提供
82118 // blurDataURL="data:..." 自动提供
83- // placeholder="blur" // 可选加载时的模糊效果
119+ // placeholder="blur" // 加载时可选的模糊效果
84120 / >
85121 )
86122}
87123```
88124
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 ` 文件是可用的 。
90126
91127## 远程图片
92128
@@ -99,7 +135,7 @@ export default function Page() {
99135 return (
100136 <Image
101137 src = " https://s3.amazonaws.com/my-bucket/profile.png"
102- alt = " 作者的照片 "
138+ alt = " 作者照片 "
103139 width = { 500 }
104140 height = { 500 }
105141 />
@@ -114,7 +150,7 @@ export default function Page() {
114150 return (
115151 < Image
116152 src= " https://s3.amazonaws.com/my-bucket/profile.png"
117- alt= " 作者的照片 "
153+ alt= " 作者照片 "
118154 width= {500 }
119155 height= {500 }
120156 / >
0 commit comments