Skip to content

Commit f220726

Browse files
feat(sharp): add kernel option to image service config (#15171)
* feat(sharp): add kernel option to image service config * bigger, better changeset * add more succinct docs to image.service.config.kernel * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <[email protected]> --------- Co-authored-by: Sarah Rainsberger <[email protected]>
1 parent b19d816 commit f220726

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

.changeset/four-pots-see.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Adds a new, optional `kernel` configuration option to select a resize algorithm in the Sharp image service
6+
7+
By default, Sharp resizes images with the `lanczos3` kernel. This new config option allows you to set the default resizing algorithm to any resizing option supported by [Sharp](https://sharp.pixelplumbing.com/api-resize/#resize) (e.g. `linear`, `mks2021`).
8+
9+
Kernel selection can produce quite noticeable differences depending on various characteristics of the source image - especially drawn art - so changing the kernel gives you more control over the appearance of images on your site:
10+
11+
```js
12+
export default defineConfig({
13+
image: {
14+
service: {
15+
entrypoint: 'astro/assets/services/sharp',
16+
config: {
17+
kernel: "mks2021"
18+
}
19+
}
20+
})
21+
```
22+
23+
This selection will apply to all images on your site, and is not yet configurable on a per-image basis. For more information, see [Sharps documentation on resizing images](https://sharp.pixelplumbing.com/api-resize/#resize).

packages/astro/src/assets/services/sharp.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FitEnum, FormatEnum, SharpOptions } from 'sharp';
1+
import type { FitEnum, FormatEnum, ResizeOptions, SharpOptions } from 'sharp';
22
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
33
import type { ImageFit, ImageOutputFormat, ImageQualityPreset } from '../types.js';
44
import {
@@ -13,6 +13,11 @@ export interface SharpImageServiceConfig {
1313
* The `limitInputPixels` option passed to Sharp. See https://sharp.pixelplumbing.com/api-constructor for more information
1414
*/
1515
limitInputPixels?: SharpOptions['limitInputPixels'];
16+
17+
/**
18+
* The `kernel` option is passed to resize calls. See https://sharp.pixelplumbing.com/api-resize/ for more information
19+
*/
20+
kernel?: ResizeOptions['kernel'];
1621
}
1722

1823
let sharp: typeof import('sharp');
@@ -57,6 +62,7 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
5762
async transform(inputBuffer, transformOptions, config) {
5863
if (!sharp) sharp = await loadSharp();
5964
const transform: BaseServiceTransform = transformOptions as BaseServiceTransform;
65+
const kernel = config.service.config.kernel;
6066

6167
// Return SVGs as-is
6268
// TODO: Sharp has some support for SVGs, we could probably support this once Sharp is the default and only service.
@@ -84,18 +90,21 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
8490
result.resize({
8591
width: Math.round(transform.width),
8692
height: Math.round(transform.height),
93+
kernel: kernel,
8794
fit,
8895
position: transform.position,
8996
withoutEnlargement,
9097
});
9198
} else if (transform.height && !transform.width) {
9299
result.resize({
93100
height: Math.round(transform.height),
101+
kernel: kernel,
94102
withoutEnlargement,
95103
});
96104
} else if (transform.width) {
97105
result.resize({
98106
width: Math.round(transform.width),
107+
kernel: kernel,
99108
withoutEnlargement,
100109
});
101110
}

packages/astro/src/types/public/config.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,6 +1327,20 @@ export interface AstroUserConfig<
13271327
* Set `false` to bypass the default image size limit for the Sharp image service and process large images.
13281328
*/
13291329

1330+
/**
1331+
* @docs
1332+
* @name image.service.config.kernel
1333+
* @kind h4
1334+
* @type {string | undefined}
1335+
* @default `undefined`
1336+
* @version 5.17.0
1337+
* @description
1338+
*
1339+
* The default [kernel used for resizing images](https://sharp.pixelplumbing.com/api-resize/#resize) in the Sharp image service.
1340+
*
1341+
* By default this is `undefined`, which maps to Sharp's default kernel of `lanczos3`.
1342+
*/
1343+
13301344
/**
13311345
* @docs
13321346
* @name image.domains

0 commit comments

Comments
 (0)