File tree 1 file changed +12
-1
lines changed
packages/gatsby-plugin-sharp/src 1 file changed +12
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,8 @@ import { reportError } from "./report-error"
15
15
16
16
const DEFAULT_BLURRED_IMAGE_WIDTH = 20
17
17
18
+ const DOMINANT_COLOR_IMAGE_SIZE = 200
19
+
18
20
const DEFAULT_BREAKPOINTS = [ 750 , 1080 , 1366 , 1920 ]
19
21
20
22
type ImageFormat = "jpg" | "png" | "webp" | "avif" | "" | "auto"
@@ -66,7 +68,16 @@ export async function getImageMetadata(
66
68
67
69
const { width, height, density, format } = await pipeline . metadata ( )
68
70
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
+
70
81
// Fallback in case sharp doesn't support dominant
71
82
const dominantColor = dominant
72
83
? rgbToHex ( dominant . r , dominant . g , dominant . b )
You can’t perform that action at this time.
0 commit comments