Skip to content

Commit 5e6c808

Browse files
authored
perf(gatsby-plugin-image): downsize image before extracting dominant color (#35814)
* perf(gatsby-plugin-image): downsize image before extracting dominant color * chore: use larger image size
1 parent b0db9a0 commit 5e6c808

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

packages/gatsby-plugin-sharp/src/image-data.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { reportError } from "./report-error"
1515

1616
const DEFAULT_BLURRED_IMAGE_WIDTH = 20
1717

18+
const DOMINANT_COLOR_IMAGE_SIZE = 200
19+
1820
const DEFAULT_BREAKPOINTS = [750, 1080, 1366, 1920]
1921

2022
type ImageFormat = "jpg" | "png" | "webp" | "avif" | "" | "auto"
@@ -66,7 +68,16 @@ export async function getImageMetadata(
6668

6769
const { width, height, density, format } = await pipeline.metadata()
6870

69-
const { dominant } = await pipeline.stats()
71+
// Downsize the image before calculating the dominant color
72+
const buffer = await pipeline
73+
.resize(DOMINANT_COLOR_IMAGE_SIZE, DOMINANT_COLOR_IMAGE_SIZE, {
74+
fit: `inside`,
75+
withoutEnlargement: true,
76+
})
77+
.toBuffer()
78+
79+
const { dominant } = await sharp(buffer).stats()
80+
7081
// Fallback in case sharp doesn't support dominant
7182
const dominantColor = dominant
7283
? rgbToHex(dominant.r, dominant.g, dominant.b)

0 commit comments

Comments
 (0)