1
1
---
2
- source-updated-at : 2025-05-22T15:18:56 .000Z
3
- translation-updated-at : 2025-05-25T20:43:27.368Z
2
+ source-updated-at : 2025-06-02T15:30:01 .000Z
3
+ translation-updated-at : 2025-06-02T19:01:25.908Z
4
4
title : 如何優化圖片
5
5
nav_title : 圖片
6
- description : 了解如何在 Next.js 中優化圖片
6
+ description : 學習如何在 Next.js 中優化圖片
7
7
related :
8
8
title : API 參考
9
9
description : 查看 Next.js Image 完整功能的 API 參考文件。
10
10
links :
11
11
- app/api-reference/components/image
12
12
---
13
13
14
- Next.js 的 [ ` <Image> ` ] ( /docs/app/api-reference/components/image ) 元件延伸了 HTML ` <img> ` 元素,提供以下功能:
14
+ Next.js 的 [ ` <Image> ` ] ( /docs/app/api-reference/components/image ) 元件擴展了 HTML ` <img> ` 元素,提供以下功能:
15
15
16
- - ** 尺寸優化:** 自動為每種裝置提供正確尺寸的圖片,並使用 WebP 等現代圖片格式。
16
+ - ** 尺寸優化:** 自動為每種裝置提供正確尺寸的圖片,使用 WebP 等現代圖片格式。
17
17
- ** 視覺穩定性:** 在圖片載入時自動防止[ 版面位移 (layout shift)] ( https://web.dev/articles/cls ) 。
18
- - ** 更快的頁面載入:** 僅在圖片進入視窗時才載入 ,使用瀏覽器原生的延遲載入,並可選擇模糊預覽圖。
19
- - ** 資源靈活性:** 按需調整圖片大小,即使是儲存在遠端伺服器上的圖片也能處理 。
18
+ - ** 更快的頁面載入:** 僅在圖片進入視窗時載入 ,使用瀏覽器原生的延遲載入,並可選擇模糊預覽圖。
19
+ - ** 資源靈活性:** 按需調整圖片大小,即使是遠端伺服器上的圖片也能處理 。
20
20
21
21
要開始使用 ` <Image> ` ,請從 ` next/image ` 導入並在元件中渲染它。
22
22
@@ -42,7 +42,7 @@ export default function Page() {
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
48
alt = " 顯示 app 和 public 資料夾的目錄結構"
@@ -60,6 +60,41 @@ export default function Page() {
60
60
<Image
61
61
src = " /profile.png"
62
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 ) 。這些值用於確定圖片比例,並在圖片載入時防止[ 累積版面位移 (Cumulative Layout Shift)] ( 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:..." 自動提供
@@ -71,11 +106,12 @@ export default function Page() {
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 "
114
+ src= {ProfileImage}
79
115
alt= " 作者的照片"
80
116
// width={500} 自動提供
81
117
// height={500} 自動提供
@@ -86,11 +122,11 @@ export default function Page() {
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
93
- 要使用遠端圖片,可以為 ` src ` 屬性提供一個 URL 字串。
129
+ 要使用遠端圖片,您可以為 ` src ` 屬性提供一個 URL 字串。
94
130
95
131
``` tsx filename="app/page.tsx" switcher
96
132
import Image from ' next/image'
@@ -122,9 +158,9 @@ export default function Page() {
122
158
}
123
159
```
124
160
125
- 由於 Next.js 在構建過程中無法訪問遠端檔案 ,您需要手動提供 [ ` width ` ] ( /docs/app/api-reference/components/image#width-and-height ) 、[ ` height ` ] ( /docs/app/api-reference/components/image#width-and-height ) 和可選的 [ ` blurDataURL ` ] ( /docs/app/api-reference/components/image#blurdataurl ) 屬性。` width ` 和 ` height ` 用於推斷正確的圖片長寬比,並避免圖片載入時的版面位移 。
161
+ 由於 Next.js 在建置過程中無法存取遠端檔案 ,您需要手動提供 [ ` width ` ] ( /docs/app/api-reference/components/image#width-and-height ) 、[ ` height ` ] ( /docs/app/api-reference/components/image#width-and-height ) 和可選的 [ ` blurDataURL ` ] ( /docs/app/api-reference/components/image#blurdataurl ) 屬性。` width ` 和 ` height ` 用於推斷正確的圖片比例,避免圖片載入時的版面位移 。
126
162
127
- 為了安全地允許來自遠端伺服器的圖片,您需要在 [ ` next.config.js ` ] ( /docs/app/api-reference/config/next-config-js ) 中定義支持的 URL 模式列表。請盡可能具體以防止惡意使用。例如,以下配置僅允許來自特定 AWS S3 儲存桶的圖片:
163
+ 為了安全地允許來自遠端伺服器的圖片,您需要在 [ ` next.config.js ` ] ( /docs/app/api-reference/config/next-config-js ) 中定義支援的 URL 模式列表。請盡可能具體以防止惡意使用。例如,以下配置僅允許來自特定 AWS S3 儲存桶的圖片:
128
164
129
165
``` ts filename="next.config.ts" switcher
130
166
import type { NextConfig } from ' next'
0 commit comments