mirror of
https://github.com/withastro/astro.git
synced 2025-02-17 22:44:24 -05:00
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>
This commit is contained in:
parent
d239e2cd7c
commit
08402ad584
5 changed files with 46 additions and 8 deletions
21
.changeset/poor-apes-cheat.md
Normal file
21
.changeset/poor-apes-cheat.md
Normal file
|
@ -0,0 +1,21 @@
|
|||
---
|
||||
'astro': minor
|
||||
---
|
||||
|
||||
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,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
```
|
3
packages/astro/config.d.ts
vendored
3
packages/astro/config.d.ts
vendored
|
@ -2,6 +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;
|
||||
type SharpImageServiceConfig = import('./dist/assets/services/sharp.js').SharpImageServiceConfig;
|
||||
|
||||
/**
|
||||
* See the full Astro Configuration API Documentation
|
||||
|
@ -17,7 +18,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
|
||||
|
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -1096,8 +1096,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,
|
||||
* },
|
||||
* },
|
||||
* },
|
||||
* }
|
||||
* ```
|
||||
|
|
|
@ -8,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<ImageQualityPreset, number> = {
|
||||
|
@ -28,13 +35,13 @@ async function loadSharp() {
|
|||
return sharpImport;
|
||||
}
|
||||
|
||||
const sharpService: LocalImageService = {
|
||||
const sharpService: LocalImageService<SharpImageServiceConfig> = {
|
||||
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 +50,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();
|
||||
|
|
Loading…
Add table
Reference in a new issue