Skip to content

Commit 08402ad

Browse files
bluwyematipicosarah11918
authoredJan 3, 2024
Add limitInputPixels option for sharp image service (#9546)
* Add limitInputPixels option for sharp image service * Fix types * Update docs Co-authored-by: sarah11918 <sarah11918@users.noreply.github.com> --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> Co-authored-by: sarah11918 <sarah11918@users.noreply.github.com>
1 parent d239e2c commit 08402ad

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed
 

‎.changeset/poor-apes-cheat.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'astro': minor
3+
---
4+
5+
Adds an option for the Sharp image service to allow large images to be processed. Set `limitInputPixels: false` to bypass the default image size limit:
6+
7+
```js
8+
// astro.config.mjs
9+
import { defineConfig } from 'astro/config';
10+
11+
export default defineConfig({
12+
image: {
13+
service: {
14+
entrypoint: 'astro/assets/services/sharp',
15+
config: {
16+
limitInputPixels: false,
17+
},
18+
},
19+
},
20+
});
21+
```

‎packages/astro/config.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ type ViteUserConfig = import('vite').UserConfig;
22
type ViteUserConfigFn = import('vite').UserConfigFn;
33
type AstroUserConfig = import('./dist/@types/astro.js').AstroUserConfig;
44
type ImageServiceConfig = import('./dist/@types/astro.js').ImageServiceConfig;
5+
type SharpImageServiceConfig = import('./dist/assets/services/sharp.js').SharpImageServiceConfig;
56

67
/**
78
* See the full Astro Configuration API Documentation
@@ -17,7 +18,7 @@ export function getViteConfig(config: ViteUserConfig): ViteUserConfigFn;
1718
/**
1819
* Return the configuration needed to use the Sharp-based image service
1920
*/
20-
export function sharpImageService(): ImageServiceConfig;
21+
export function sharpImageService(config?: SharpImageServiceConfig): ImageServiceConfig;
2122

2223
/**
2324
* Return the configuration needed to use the Squoosh-based image service

‎packages/astro/config.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
export { defineConfig, getViteConfig } from './dist/config/index.js';
22

3-
export function sharpImageService() {
3+
export function sharpImageService(config = {}) {
44
return {
55
entrypoint: 'astro/assets/services/sharp',
6-
config: {},
6+
config,
77
};
88
}
99

‎packages/astro/src/@types/astro.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1096,8 +1096,13 @@ export interface AstroUserConfig {
10961096
* ```js
10971097
* {
10981098
* image: {
1099-
* // Example: Enable the Sharp-based image service
1100-
* service: { entrypoint: 'astro/assets/services/sharp' },
1099+
* // Example: Enable the Sharp-based image service with a custom config
1100+
* service: {
1101+
* entrypoint: 'astro/assets/services/sharp',
1102+
* config: {
1103+
* limitInputPixels: false,
1104+
* },
1105+
* },
11011106
* },
11021107
* }
11031108
* ```

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import {
88
type LocalImageService,
99
} from './service.js';
1010

11+
export interface SharpImageServiceConfig {
12+
/**
13+
* The `limitInputPixels` option passed to Sharp. See https://sharp.pixelplumbing.com/api-constructor for more information
14+
*/
15+
limitInputPixels?: number;
16+
}
17+
1118
let sharp: typeof import('sharp');
1219

1320
const qualityTable: Record<ImageQualityPreset, number> = {
@@ -28,13 +35,13 @@ async function loadSharp() {
2835
return sharpImport;
2936
}
3037

31-
const sharpService: LocalImageService = {
38+
const sharpService: LocalImageService<SharpImageServiceConfig> = {
3239
validateOptions: baseService.validateOptions,
3340
getURL: baseService.getURL,
3441
parseURL: baseService.parseURL,
3542
getHTMLAttributes: baseService.getHTMLAttributes,
3643
getSrcSet: baseService.getSrcSet,
37-
async transform(inputBuffer, transformOptions) {
44+
async transform(inputBuffer, transformOptions, config) {
3845
if (!sharp) sharp = await loadSharp();
3946

4047
const transform: BaseServiceTransform = transformOptions as BaseServiceTransform;
@@ -43,7 +50,11 @@ const sharpService: LocalImageService = {
4350
// TODO: Sharp has some support for SVGs, we could probably support this once Sharp is the default and only service.
4451
if (transform.format === 'svg') return { data: inputBuffer, format: 'svg' };
4552

46-
let result = sharp(inputBuffer, { failOnError: false, pages: -1 });
53+
const result = sharp(inputBuffer, {
54+
failOnError: false,
55+
pages: -1,
56+
limitInputPixels: config.service.config.limitInputPixels,
57+
});
4758

4859
// always call rotate to adjust for EXIF data orientation
4960
result.rotate();

0 commit comments

Comments
 (0)
Please sign in to comment.