From 1430af109ed9b6526d4257ae0e671a87eda769f7 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 29 Dec 2023 17:22:50 +0800 Subject: [PATCH 1/3] Add limitInputPixels option for sharp image service --- .changeset/poor-apes-cheat.md | 5 +++++ packages/astro/config.d.ts | 9 ++++++++- packages/astro/config.mjs | 4 ++-- packages/astro/src/assets/services/sharp.ts | 11 ++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 .changeset/poor-apes-cheat.md diff --git a/.changeset/poor-apes-cheat.md b/.changeset/poor-apes-cheat.md new file mode 100644 index 000000000000..6b8dddb32c10 --- /dev/null +++ b/.changeset/poor-apes-cheat.md @@ -0,0 +1,5 @@ +--- +'astro': minor +--- + +Adds `limitInputPixels` option for the Sharp image service to be passed on to Sharp. diff --git a/packages/astro/config.d.ts b/packages/astro/config.d.ts index a12f862d651b..7d32419b147e 100644 --- a/packages/astro/config.d.ts +++ b/packages/astro/config.d.ts @@ -3,6 +3,13 @@ type ViteUserConfigFn = import('vite').UserConfigFn; type AstroUserConfig = import('./dist/@types/astro.js').AstroUserConfig; type ImageServiceConfig = import('./dist/@types/astro.js').ImageServiceConfig; +export interface SharpImageServiceConfig { + /** + * The `limitInputPixels` option passed to Sharp. See https://sharp.pixelplumbing.com/api-constructor for more information + */ + limitInputPixels?: number; +} + /** * See the full Astro Configuration API Documentation * https://astro.build/config @@ -17,7 +24,7 @@ export function getViteConfig(config: ViteUserConfig): ViteUserConfigFn; /** * Return the configuration needed to use the Sharp-based image service */ -export function sharpImageService(): ImageServiceConfig; +export function sharpImageService(config?: SharpImageServiceConfig): ImageServiceConfig; /** * Return the configuration needed to use the Squoosh-based image service diff --git a/packages/astro/config.mjs b/packages/astro/config.mjs index 208313287817..389387bddf02 100644 --- a/packages/astro/config.mjs +++ b/packages/astro/config.mjs @@ -1,9 +1,9 @@ export { defineConfig, getViteConfig } from './dist/config/index.js'; -export function sharpImageService() { +export function sharpImageService(config = {}) { return { entrypoint: 'astro/assets/services/sharp', - config: {}, + config, }; } diff --git a/packages/astro/src/assets/services/sharp.ts b/packages/astro/src/assets/services/sharp.ts index 21529913892c..bbb07994db7a 100644 --- a/packages/astro/src/assets/services/sharp.ts +++ b/packages/astro/src/assets/services/sharp.ts @@ -1,4 +1,5 @@ import type { FormatEnum } from 'sharp'; +import type { SharpImageServiceConfig } from '../../../config.js'; import { AstroError, AstroErrorData } from '../../core/errors/index.js'; import type { ImageOutputFormat, ImageQualityPreset } from '../types.js'; import { @@ -28,13 +29,13 @@ async function loadSharp() { return sharpImport; } -const sharpService: LocalImageService = { +const sharpService: LocalImageService = { validateOptions: baseService.validateOptions, getURL: baseService.getURL, parseURL: baseService.parseURL, getHTMLAttributes: baseService.getHTMLAttributes, getSrcSet: baseService.getSrcSet, - async transform(inputBuffer, transformOptions) { + async transform(inputBuffer, transformOptions, config) { if (!sharp) sharp = await loadSharp(); const transform: BaseServiceTransform = transformOptions as BaseServiceTransform; @@ -43,7 +44,11 @@ const sharpService: LocalImageService = { // TODO: Sharp has some support for SVGs, we could probably support this once Sharp is the default and only service. if (transform.format === 'svg') return { data: inputBuffer, format: 'svg' }; - let result = sharp(inputBuffer, { failOnError: false, pages: -1 }); + const result = sharp(inputBuffer, { + failOnError: false, + pages: -1, + limitInputPixels: config.service.config.limitInputPixels, + }); // always call rotate to adjust for EXIF data orientation result.rotate(); From 15c8980b5c1e0fb5e6183e9f4dd12547c316e8e9 Mon Sep 17 00:00:00 2001 From: bluwy Date: Fri, 29 Dec 2023 18:02:26 +0800 Subject: [PATCH 2/3] Fix types --- packages/astro/config.d.ts | 8 +------- packages/astro/src/assets/services/sharp.ts | 8 +++++++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/astro/config.d.ts b/packages/astro/config.d.ts index 7d32419b147e..b859f0b055fc 100644 --- a/packages/astro/config.d.ts +++ b/packages/astro/config.d.ts @@ -2,13 +2,7 @@ type ViteUserConfig = import('vite').UserConfig; type ViteUserConfigFn = import('vite').UserConfigFn; type AstroUserConfig = import('./dist/@types/astro.js').AstroUserConfig; type ImageServiceConfig = import('./dist/@types/astro.js').ImageServiceConfig; - -export interface SharpImageServiceConfig { - /** - * The `limitInputPixels` option passed to Sharp. See https://sharp.pixelplumbing.com/api-constructor for more information - */ - limitInputPixels?: number; -} +type SharpImageServiceConfig = import('./dist/assets/services/sharp.js').SharpImageServiceConfig; /** * See the full Astro Configuration API Documentation diff --git a/packages/astro/src/assets/services/sharp.ts b/packages/astro/src/assets/services/sharp.ts index bbb07994db7a..74bf921d9ce2 100644 --- a/packages/astro/src/assets/services/sharp.ts +++ b/packages/astro/src/assets/services/sharp.ts @@ -1,5 +1,4 @@ import type { FormatEnum } from 'sharp'; -import type { SharpImageServiceConfig } from '../../../config.js'; import { AstroError, AstroErrorData } from '../../core/errors/index.js'; import type { ImageOutputFormat, ImageQualityPreset } from '../types.js'; import { @@ -9,6 +8,13 @@ import { type LocalImageService, } from './service.js'; +export interface SharpImageServiceConfig { + /** + * The `limitInputPixels` option passed to Sharp. See https://sharp.pixelplumbing.com/api-constructor for more information + */ + limitInputPixels?: number; +} + let sharp: typeof import('sharp'); const qualityTable: Record = { From 5cc90072bc29a8e9171cf756947a6bc726e9d2e6 Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 3 Jan 2024 15:39:24 +0800 Subject: [PATCH 3/3] Update docs Co-authored-by: sarah11918 --- .changeset/poor-apes-cheat.md | 18 +++++++++++++++++- packages/astro/src/@types/astro.ts | 9 +++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/.changeset/poor-apes-cheat.md b/.changeset/poor-apes-cheat.md index 6b8dddb32c10..1d8c380d0b86 100644 --- a/.changeset/poor-apes-cheat.md +++ b/.changeset/poor-apes-cheat.md @@ -2,4 +2,20 @@ 'astro': minor --- -Adds `limitInputPixels` option for the Sharp image service to be passed on to Sharp. +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: + +```js +// astro.config.mjs +import { defineConfig } from 'astro/config'; + +export default defineConfig({ + image: { + service: { + entrypoint: 'astro/assets/services/sharp', + config: { + limitInputPixels: false, + }, + }, + }, +}); +``` diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 6a255c5734b7..259ae5fdf6f8 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1090,8 +1090,13 @@ export interface AstroUserConfig { * ```js * { * image: { - * // Example: Enable the Sharp-based image service - * service: { entrypoint: 'astro/assets/services/sharp' }, + * // Example: Enable the Sharp-based image service with a custom config + * service: { + * entrypoint: 'astro/assets/services/sharp', + * config: { + * limitInputPixels: false, + * }, + * }, * }, * } * ```